<commit_before>"""
@author <NAME> <@paked/@hcwool>

The r/WatchPeopleCode sidebar updater script/bot!
"""

from praw import *

import random
import os
import requests
import json

# Open up the bots config.
config_data = open('config.json')
config = json.load(config_data)

class Bot:
	def __init__(self, username, password, subreddit="watchpeoplecode", debug=False):
		self._username = username
		self._password = password
		self._subreddit = subreddit
		self.debug = debug

		self.r = Reddit("{0} a bot for /r/{1} to set top WPC streams!".format(self._username, self._password))

		self.login()

	def login(self):
		self.log("I'm logging in now!")
		self.r.login(self._username, self._password)

	def log(self, message):
		if not self.debug:
			return
		print("[{}] {}".format(self._username, message))


class SidebarBot(Bot):
	def __init__(self, username, password, description, subreddit="watchpeoplecode", mode="live", debug=False):
		super().__init__(username, password, subreddit, debug)

		self.description = description
		self.mode = mode

	def update(self):
		self.log("Just about to start making that sweet description of yours!")
		description = self.generate_description(self.choose_streams())
		subreddit = self.r.get_subreddit(self._subreddit)
		subreddit.update_settings(description=description.encode('utf8'))
		self.log("Whew! Just updated your subreddits sidebar!")

	def generate_description(self, streams):
		output = self.description + "\n\n"
		for stream in streams:
			output += "You should check out [{0}](http://reddit.com/r/{0}) streaming [{1}]({2})\n\n".format(stream["username"], stream["title"], stream["url"])

		return output

	def choose_streams(self):
		streams = self._get_streams()
		live_streams = streams[self.mode]
		top_streams = []
		if len(live_streams) > 3:
			chosen = []
			for z in range(0, 3):
				i = 0
				while i in chosen:
					i = random.randint(0, len(live_streams))
				
				top_streams.append(live_streams[i])
				chosen.append(i)
		else:
			top_streams = live_streams

		return top_streams

	def _get_streams(self):
		return requests.get("http://www.watchpeoplecode.com/json").json()

sb = SidebarBot(config["bot"]["username"],
				config["bot"]["password"],
				config["description"],
				subreddit=config["subreddit"],
				mode=config["mode"],
				debug=True)

sb.update()
config_data.close()
<commit_msg>bot: add minor documentation<commit_after>"""
@author <NAME> <@paked/@hcwool>

The r/WatchPeopleCode sidebar updater script/bot!
"""

from praw import *

import random
import os
import requests
import json

# Open up the bots config.
config_data = open('config.json')
config = json.load(config_data)

class Bot:
	"""
	Bot represents a basic bot, with a reddit username, password and subreddit
	"""
	def __init__(self, username, password, subreddit="watchpeoplecode", debug=False):
		self._username = username
		self._password = password
		self._subreddit = subreddit
		self.debug = debug

		self.r = Reddit("{0} a bot for /r/{1} to set top WPC streams!".format(self._username, self._password))

		self.login()

	def login(self):
		self.log("I'm logging in now!")
		self.r.login(self._username, self._password)

	def log(self, message):
		if not self.debug:
			return
		print("[{}] {}".format(self._username, message))


class SidebarBot(Bot):
	"""
	Sidebar bot, a bot to update your sidebar!
	"""
	def __init__(self, username, password, description, subreddit="watchpeoplecode", mode="live", debug=False):
		super().__init__(username, password, subreddit, debug)

		self.description = description
		self.mode = mode

	def update(self):
		self.log("Just about to start making that sweet description of yours!")
		description = self.generate_description(self.choose_streams())
		subreddit = self.r.get_subreddit(self._subreddit)
		subreddit.update_settings(description=description.encode('utf8'))
		self.log("Whew! Just updated your subreddits sidebar!")

	def generate_description(self, streams):
		output = self.description + "\n\n"
		for stream in streams:
			output += "You should check out [{0}](http://reddit.com/r/{0}) streaming [{1}]({2})\n\n".format(stream["username"], stream["title"], stream["url"])

		return output

	def choose_streams(self):
		streams = self._get_streams()
		live_streams = streams[self.mode]
		top_streams = []
		if len(live_streams) > 3:
			chosen = []
			for z in range(0, 3):
				i = 0
				while i in chosen:
					i = random.randint(0, len(live_streams))
				
				top_streams.append(live_streams[i])
				chosen.append(i)
		else:
			top_streams = live_streams

		return top_streams

	def _get_streams(self):
		return requests.get("http://www.watchpeoplecode.com/json").json()

sb = SidebarBot(config["bot"]["username"],
				config["bot"]["password"],
				config["description"],
				subreddit=config["subreddit"],
				mode=config["mode"],
				debug=True)

sb.update()
config_data.close()

{'fraction_non_alphanumeric': 0.14478804013925864, 'fraction_numerical': 0.004915011263567479, 'mean_word_length': 4.60091743119266, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '837702', 'n_tokens_mistral': 1762, 'n_tokens_neox': 1610, 'n_words': 391}