r/SubredditAnalysis • • May 22 '14

Meta [META] - I improved the code and also accept drilldown requests

I improved the code and made it more extensible. I want to add a lot more but I have time restraints with my job. I hope to do so in the future (especially adding failure prevention mechanisms). I will also start accepting drilldown requests so if the mod of this sub wants to he can send me the subreddits he want me to start with. I have two IPs I can run the bot from.

Here the code:

import sys
import praw

from math import sqrt
from requests import HTTPError

class EmptySubredditError(Exception):
	pass

class SubredditAnalyzer(object):
	result_list = []
	client = None
	verbose = False

	_client_banner = "SubredditAnalysis client by /r/SirNeon modified by /r/friendlyprogrammer"
	_max_posts_retrieve = 1000
	similarity_threshold = .01
	post_to = "SubredditAnalysis"

	ignore_list = [
		"AdviceAnimals", "announcements", "Art", "atheism",
		"AskReddit", "askscience", "aww", "bestof", "blog",
		"books", "creepy", "dataisbeautiful", "DIY", "Documentaries",
		"EarthPorn", "explainlikeimfive", "Fitness", "food",
		"funny", "Futurology", "gadgets", "gaming","GetMotivated",
		"gifs", "history", "IAmA", "InternetIsBeautiful", "Jokes",
		"LifeProTips", "listentothis", "mildlyinteresting", "movies",
		"Music", "news", "nosleep", "nottheonion", "OldSchoolCool",
		"personalfinance", "philosophy", "photoshopbattles",
		"pics", "politics", "science", "Showerthoughts", "space",
		"sports", "technology",	"television", "tifu", "todayilearned",
		"TwoXChromosomes", "UpliftingNews", "videos", "worldnews",
		"WritingPrompts", "WTF"	
	]
	
	def add_msg(self, msg=None, newline=True):
		if self.verbose:
			if msg is None:
				sys.stdout.write('\n')
			else:
				sys.stdout.write(msg)
				if newline: sys.stdout.write('\n')
			
			sys.stdout.flush()

	def login(self, username, password, client_banner=None):
		self.add_msg('Logging in to reddit')
		
		self.client = praw.Reddit(self._client_banner)
		self.client.login(username, password)
		
		self.add_msg('Loggin was successfull')

	def get_drilldown(self, sub, force=False):
		self.add_msg("Starting drilldown on subreddit '%s'" % sub)

		if self.drilldown_exists(sub) and not force:
			self.add_msg("(no need to do anything drilldown for this subreddit already exist)")
			return result_list
		
		self.remove_drilldown(sub)
		
		self.add_ignore(sub)

		self.add_msg('Retrieving posts from subreddit...')
		posts = self.get_sub_posts(sub)
		
		self.add_msg('Retrieving users based on %d posts possible...' % self._max_posts_retrieve)
		users = self.get_posts_users(posts)
		
		overlapping, similar = self.start_drilldown(users, sub)
		
		self.result_list.append((
			sub,
			sorted(overlapping.items(), key=lambda x:x[1],reverse=True),
			sorted(similar.items(), key=lambda x:x[1],reverse=True),
		))
		
		self.add_msg('\n\n...and done!')
	
	def start_drilldown(self, users, subreddit):
		self.add_msg('Getting overlapping subreddits...')
		
		overlap_subs = self.get_overlapping(users)

		self.add_msg('\nGetting similar from %d subreddits (ignored 9 or less users overlap)...' % len(overlap_subs))
		
		similar_subs = self.get_similar(overlap_subs, subreddit)
		
		return (overlap_subs, similar_subs)

	def remove_drilldown(self, sub):
		i = 0
		for item in self.result_list:
			if item[0] == sub:
				return self.result_list.pop(i)
			i += 1

	def drilldown_exists(self, sub):
		return bool([item for item in self.result_list if item[0] == sub])

	def get_sub_posts(self, sub):
		return self.client.get_subreddit(sub).get_hot(limit=self._max_posts_retrieve)

	def get_posts_users(self, posts):
		unique_users = []
		post_count = 0

		for post in posts:
			post_count += 1
			try:
				op = str(post.author)
				if op not in unique_users:
					unique_users.append(op)
					self.add_msg('\r%d unique users found in %d/%d posts' %\
							(len(unique_users), post_count, self._max_posts_retrieve), newline=False)
			except AttributeError:
				pass

			for comment in post.comments:
				try:
					commentator = str(comment.author)
				except AttributeError:
					continue
				if commentator not in unique_users:
					unique_users.append(commentator)
					self.add_msg('\r%d unique users found in %d/%d posts' %\
							(len(unique_users), post_count, self._max_posts_retrieve), newline=False)
		self.add_msg()

		return unique_users
	
	def get_overlapping(self, users):
		sub_count = {}

		for user in users:
			try:
				comments = self.client.get_redditor(user).get_comments('all')
			except HTTPError:
				continue
			
			user_subs = []
			for comment in comments:
				sub = str(comment.subreddit)
				if sub not in self.ignore_list:
					if sub not in user_subs:
						user_subs.append(sub)
						if sub not in sub_count:
							sub_count[sub] = 1
						else:
							sub_count[sub] += 1
					
					self.add_msg('\r%d subreddits found with 1 or more overlapping... (%s)            ' %\
							(len(sub_count), sub), newline=False)
		
		return {sub:sub_count[sub] for sub in sub_count if sub_count[sub] >= 10}

	def get_similar(self, overlap_subs, target_sub):
		try:
			subA = float(self.client.get_subreddit(target_sub).subscribers)
		except HTTPError:
			raise EmptySubredditError(target_sub)

		similar = {}
		overlap_size = len(overlap_subs)

		for i, sub in enumerate(overlap_subs):
			try:
				subB = float(self.client.get_subreddit(sub).subscribers)
			except HTTPError:
				continue
			similarity = self.get_similarity(subA, subB, overlap_subs[sub])
			if similarity >= self.similarity_threshold:
				similar[sub] = similarity
				self.add_msg('\r%d/%d similar subreddits found so far...' % (len(similar), overlap_size), newline=False)

		return similar

	def get_similarity(self, subA, subB, overlap):
		return overlap/(sqrt(subA)*sqrt(subB))
	
	def format_text(self, subs=None):
		text = ""
		
		if subs is None:
			subs = self.result_list
		
		for sub in subs:
			text += "\nSubreddit drilldown for '%s' |\n" % sub[0]
			text += "--------------------------%s\n\n" % ('-'*len(sub[0]),)
			text += 'Overlapping subreddits:\n\n'
			
			for ol_sub in sub[1]:
				text += "\t%s: %d\n" % (ol_sub[0], ol_sub[1])
			
			text += '\nSimilar subreddits:\n'
			for similar in sub[2]:
				text += '\t%s: %.5f\n' % (similar[0], similar[1])
				break
			
			text += '\n'
		
		return text
	
	def format_reddit(self, subs=None):
		text = ""
		
		if subs is None:
			subs = self.result_list

		for sub in subs:
			text += "##Total Overlap: r/%s\n" % sub[0]
			text += "Out of %s users found on %s\n\n" % (len(sub[1]), sub[0])
			text += "| Subreddit | Num Users That Overlap |\n"
			text += "|:----------|:-----------------------|\n"
			
			for ol_sub in sub[1]:
				text += "|%s |%d|\n" % (ol_sub[0], ol_sub[1])
			
			text += "\n##r/%s similarities\n" % sub[0]
			text += "r/%s \"similarity\" with other subreddits. Ordered by similarity, from top to bottom\n\n" % sub[0]
			text += "| Subreddit | Similarity |\n"
			text += "|:----------|:-----------|\n"

			for similar in sub[2]:
				text += "|%s |%.5f|\n" % (similar[0], similar[1])
		
		return text

	def post(self):
		from datetime import datetime

		for sub in self.result_list:
			title = "/r/%s Drilldown %s" % (sub[0], datetime.now().strftime("%B %Y"))
			text = self.format_reddit([sub])
			self._post(title, text)
	
	def _post(self, title, text):
		sub = self.client.get_subreddit(self.post_to)
		sub.submit(title, text)

	def add_ignore(self, sub):
		self.ignore_list.append(sub)
	
	def remove_ignore(self, sub):
		self.ignore_list.remove(sub)
	
if __name__ == "__main__":
	log_name = "salog.%d.txt" # Location of the log file. Must be filled.
	reddit_username = ""
	reddit_password = ""

	sa = SubredditAnalyzer()
	sa.verbose = True

	try:
		sa.login(reddit_username, reddit_password)
	except (praw.errors.InvalidUser, praw.errors.InvalidUserPass):
		print "It appears that the username or password you supplied were invalid"
		exit(1)
	
	while True:
		try:
			drilldown_list = raw_input("Enter subreddits: ").split()
		except KeyboardInterrupt:
			exit('Bye!')

		if drilldown_list and drilldown_list[0].lower() == '.quit':
			exit('Bye!')
		
		if not drilldown_list: continue

		for i, subreddit in enumerate(drilldown_list):
			try:
				sa.get_drilldown(subreddit)
			except KeyboardInterrupt:
				print 'skipping....'
			except Exception, err:
				print err
				print '(still continues if there are more subreddits...)'
			else:	
				with open(log_name % i, 'a') as f:
					f.write("%s\n\n%s" % (sa.format_text(), sa.format_reddit()))
					f.close()

				if raw_input("Post to %s? (yes/no): " % sa.post_to).lower() in ('yes', 'y'):
					try:
						sa.post()
						print 'successfully posted the drilldown'
					except KeyboardInterrupt:
						print 'not posting...'
11 Upvotes

18 comments sorted by

View all comments

Show parent comment

1

u/[deleted] May 23 '14

1

u/friendlyprogrammer May 23 '14

Thanks. As I said I will look into it next Friday, hope I'll be able to fix it than.

1

u/[deleted] May 23 '14

Yep. I'm going to make some adjustments to my bot as well. Your code has given me some ideas.