Skip to content

Commit

Permalink
[reddit] PEP8ify and enable callbacks for info
Browse files Browse the repository at this point in the history
  • Loading branch information
embolalia committed May 18, 2013
1 parent f2e3625 commit 4ab4653
Showing 1 changed file with 44 additions and 29 deletions.
73 changes: 44 additions & 29 deletions reddit-info.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,57 +8,72 @@

import praw
import re
domain = r'https?://(?:www\.|np\.)?reddit\.com'
post_url = '(%s/r/.*?/comments/[\w-]+)' % domain
user_url = '%s/u(ser)?/([\w-]+)' % domain


def setup(willie):
regex = re.compile('http(?:s)?://(www\.)?reddit\.com/(r/.*?/comments/[\w-]+|u(ser)?/[\w-]+)')
if not willie.memory.contains('url_exclude'):
willie.memory['url_exclude'] = [regex]
else:
exclude = willie.memory['url_exclude']
exclude.append(regex)
willie.memory['url_exclude'] = exclude
post_regex = re.compile(post_url)
user_regex = re.compile(user_url)
if not willie.memory.contains('url_callbacks'):
willie.memory['url_callbacks'] = {}
willie.memory['url_callbacks'][post_regex] = rpost_info
willie.memory['url_callbacks'][user_regex] = redditor_info


def rpost_info(willie, trigger):
def rpost_info(willie, trigger, match=None):
r = praw.Reddit(user_agent='phenny / willie IRC bot - see dft.ba/-williesource for more')
s = r.get_submission(url=trigger.group(1))

message = '[REDDIT] '+s.title
if s.is_self: message = message + ' (self.' + s.subreddit.display_name + ')'
else: message = message + ' (' + s.url + ')' +' to r/'+s.subreddit.display_name
match = match or trigger
s = r.get_submission(url=match.group(1))

message = '[REDDIT] ' + s.title
if s.is_self:
message = message + ' (self.' + s.subreddit.display_name + ')'
else:
message = message + ' (' + s.url + ')' + ' to r/' + s.subreddit.display_name
if s.over_18:
message = message + ' 05[NSFW]'
#TODO implement per-channel settings db, and make this able to kick
message = message +' | ' + str(s.ups-s.downs)+' points (03'\
+str(s.ups)+'|05'+str(s.downs)+') | '+str(s.num_comments)\
+' comments | Posted by '+s.author.name
message = (message + ' | ' + str(s.ups - s.downs) + ' points (03'
+ str(s.ups) + '|05' + str(s.downs) + ') | ' +
str(s.num_comments) + ' comments | Posted by ' + s.author.name)
#TODO add creation time with s.created
willie.say(message)
rpost_info.rule = '.*(http(?:s)?://(www\.)?reddit\.com/r/.*?/comments/[\w-]+).*'
rpost_info.rule = '.*%s.*' % post_url

def redditor_info(willie, trigger):

def redditor_info(willie, trigger, match=None):
"""Show information about the given Redditor"""
commanded = re.match(willie.config.prefix+'.*', trigger)
commanded = re.match(willie.config.prefix + 'redditor', trigger)
r = praw.Reddit(user_agent='phenny / willie IRC bot - see dft.ba/-williesource for more')
match = match or trigger
try:
u = r.get_redditor(trigger.group(2))
u = r.get_redditor(match.group(2))
except:
if commanded:
willie.say('No such Redditor.')
return
return willie.NOLIMIT
else:
return
#Fail silently if it wasn't an explicit command.

message = '[REDDITOR] '+u.name
if commanded: message = message + ' | http://reddit.com/u/'+u.name
if u.is_gold: message = message + ' | 08Gold'
if u.is_mod: message = message + ' | 05Mod'
message = message + ' | Link: '+str(u.link_karma)+ ' | Comment: '+str(u.comment_karma)


message = '[REDDITOR] ' + u.name
if commanded:
message = message + ' | http://reddit.com/u/' + u.name
if u.is_gold:
message = message + ' | 08Gold'
if u.is_mod:
message = message + ' | 05Mod'
message = message + ' | Link: ' + str(u.link_karma) + ' | Comment: ' + str(u.comment_karma)

#TODO detect cake day with u.created
willie.say(message)
#If you change this, you'll have to change some things above.
redditor_info.commands = ['redditor']


def auto_redditor_info(willie, trigger):
redditor_info(willie, trigger)
#If you change the groups here, you'll have to change some things above.
auto_redditor_info.rule = '.*http(?:s)?://(?:www\.)?reddit\.com/u(ser)?/([\w-]+).*'
auto_redditor_info.rule = '.*%s.*' % user_url

0 comments on commit 4ab4653

Please sign in to comment.