Skip to content

Commit

Permalink
reddit: updating reddit module praw dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
RustyBower committed Mar 16, 2019
1 parent dafa08a commit 8810781
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 76 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
xmltodict
pytz
praw<6.0.0
praw>=4.0.0,<6.0.0
pyenchant; python_version < '3.7'
geoip2
ipython<6.0; python_version < '3.3'
Expand Down
149 changes: 74 additions & 75 deletions sopel/modules/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from sopel.tools import SopelMemory, time
import datetime as dt
import praw
import prawcore
import re
import sys
if sys.version_info.major >= 3:
Expand Down Expand Up @@ -55,59 +56,58 @@ def rpost_info(bot, trigger, match=None):
client_secret=None,
)
s = r.submission(id=match.group(2))
except Exception:
r = praw.Reddit(user_agent=USER_AGENT)
s = r.get_submission(submission_id=match.group(2))

message = ('[REDDIT] {title} {link}{nsfw} | {points} points ({percent}) | '
'{comments} comments | Posted by {author} | '
'Created at {created}')
message = ('[REDDIT] {title} {link}{nsfw} | {points} points ({percent}) | '
'{comments} comments | Posted by {author} | '
'Created at {created}')

subreddit = s.subreddit.display_name
if s.is_self:
link = '(self.{})'.format(subreddit)
else:
link = '({}) to r/{}'.format(s.url, subreddit)

if s.over_18:
if subreddit.lower() in spoiler_subs:
nsfw = bold(color(' [SPOILERS]', colors.RED))
subreddit = s.subreddit.display_name
if s.is_self:
link = '(self.{})'.format(subreddit)
else:
nsfw = bold(color(' [NSFW]', colors.RED))

sfw = bot.db.get_channel_value(trigger.sender, 'sfw')
if sfw:
link = '(link hidden)'
bot.write(['KICK', trigger.sender, trigger.nick,
'Linking to NSFW content in a SFW channel.'])
else:
nsfw = ''

if s.author:
author = s.author.name
else:
author = '[deleted]'
link = '({}) to r/{}'.format(s.url, subreddit)

if s.over_18:
if subreddit.lower() in spoiler_subs:
nsfw = bold(color(' [SPOILERS]', colors.RED))
else:
nsfw = bold(color(' [NSFW]', colors.RED))

sfw = bot.db.get_channel_value(trigger.sender, 'sfw')
if sfw:
link = '(link hidden)'
bot.write(['KICK', trigger.sender, trigger.nick,
'Linking to NSFW content in a SFW channel.'])
else:
nsfw = ''

tz = time.get_timezone(bot.db, bot.config, None, trigger.nick,
trigger.sender)
time_created = dt.datetime.utcfromtimestamp(s.created_utc)
created = time.format_time(bot.db, bot.config, tz, trigger.nick,
trigger.sender, time_created)
if s.author:
author = s.author.name
else:
author = '[deleted]'

if s.score > 0:
point_color = colors.GREEN
else:
point_color = colors.RED
tz = time.get_timezone(bot.db, bot.config, None, trigger.nick,
trigger.sender)
time_created = dt.datetime.utcfromtimestamp(s.created_utc)
created = time.format_time(bot.db, bot.config, tz, trigger.nick,
trigger.sender, time_created)

percent = color(unicode(s.upvote_ratio * 100) + '%', point_color)
if s.score > 0:
point_color = colors.GREEN
else:
point_color = colors.RED

title = unescape(s.title)
message = message.format(
title=title, link=link, nsfw=nsfw, points=s.score, percent=percent,
comments=s.num_comments, author=author, created=created)
percent = color(unicode(s.upvote_ratio * 100) + '%', point_color)

bot.say(message)
title = unescape(s.title)
message = message.format(
title=title, link=link, nsfw=nsfw, points=s.score, percent=percent,
comments=s.num_comments, author=author, created=created)

bot.say(message)
except prawcore.exceptions.NotFound:
bot.say('No such post.')
return NOLIMIT

# If you change this, you'll have to change some other things...
@commands('redditor')
Expand All @@ -122,44 +122,43 @@ def redditor_info(bot, trigger, match=None):
)
match = match or trigger
try:
u = r.get_redditor(match.group(2))
except Exception: # TODO: Be specific
u = r.redditor(match.group(2))
message = '[REDDITOR] ' + u.name
now = dt.datetime.utcnow()
cakeday_start = dt.datetime.utcfromtimestamp(u.created_utc)
cakeday_start = cakeday_start.replace(year=now.year)
day = dt.timedelta(days=1)
year_div_by_400 = now.year % 400 == 0
year_div_by_100 = now.year % 100 == 0
year_div_by_4 = now.year % 4 == 0
is_leap = year_div_by_400 or ((not year_div_by_100) and year_div_by_4)
if (not is_leap) and ((cakeday_start.month, cakeday_start.day) == (2, 29)):
# If cake day is 2/29 and it's not a leap year, cake day is 1/3.
# Cake day begins at exact account creation time.
is_cakeday = cakeday_start + day <= now <= cakeday_start + (2 * day)
else:
is_cakeday = cakeday_start <= now <= cakeday_start + day

if is_cakeday:
message = message + ' | 13Cake day'
if commanded:
message = message + ' | https://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))

bot.say(message)
except prawcore.exceptions.NotFound:
if commanded:
bot.say('No such Redditor.')
return NOLIMIT
else:
return
# Fail silently if it wasn't an explicit command.

message = '[REDDITOR] ' + u.name
now = dt.datetime.utcnow()
cakeday_start = dt.datetime.utcfromtimestamp(u.created_utc)
cakeday_start = cakeday_start.replace(year=now.year)
day = dt.timedelta(days=1)
year_div_by_400 = now.year % 400 == 0
year_div_by_100 = now.year % 100 == 0
year_div_by_4 = now.year % 4 == 0
is_leap = year_div_by_400 or ((not year_div_by_100) and year_div_by_4)
if (not is_leap) and ((cakeday_start.month, cakeday_start.day) == (2, 29)):
# If cake day is 2/29 and it's not a leap year, cake day is 1/3.
# Cake day begins at exact account creation time.
is_cakeday = cakeday_start + day <= now <= cakeday_start + (2 * day)
else:
is_cakeday = cakeday_start <= now <= cakeday_start + day

if is_cakeday:
message = message + ' | 13Cake day'
if commanded:
message = message + ' | https://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))

bot.say(message)


# If you change the groups here, you'll have to change some things above.
@rule('.*%s.*' % user_url)
Expand Down

0 comments on commit 8810781

Please sign in to comment.