forked from maxpowa/inumuta-modules
/
reddit.py
232 lines (190 loc) · 7.29 KB
/
reddit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# coding=utf8
"""
reddit-info.py - Willie Reddit module
Author: Edward Powell, embolalia.net
About: http://willie.dftba.net
This module provides special tools for reddit, namely showing detailed
info about reddit posts
"""
from __future__ import unicode_literals
from willie.module import commands, rule, example, NOLIMIT, OP
from willie.formatting import bold, color, colors
from willie.web import USER_AGENT
from willie.tools import WillieMemory, time
import HTMLParser
import datetime as dt
import praw
import re
import sys
if sys.version_info.major >= 3:
unicode = str
domain = r'https?://(?:www\.|np\.)?reddit\.com'
post_url = '(%s/r/.*?/comments/[\w-]+)' % domain
comment_url = '(%s/r/.*?/comments/[\w-]+/\S+?/[\w-]+)' % domain
user_url = '%s/u(ser)?/([\w-]+)' % domain
post_regex = re.compile(post_url)
comment_regex = re.compile(comment_url)
user_regex = re.compile(user_url)
def setup(bot):
if not bot.memory.contains('url_callbacks'):
bot.memory['url_callbacks'] = WillieMemory()
bot.memory['url_callbacks'][post_regex] = rpost_info
bot.memory['url_callbacks'][user_regex] = redditor_info
def shutdown(bot):
del bot.memory['url_callbacks'][post_regex]
del bot.memory['url_callbacks'][user_regex]
@rule('.*%s.*' % post_url)
def rpost_info(bot, trigger, match=None):
r = praw.Reddit(user_agent=USER_AGENT)
match = match or trigger
s = r.get_submission(url=match.group(1))
if (comment_regex.match(match.group(0))):
return rcomment_info(bot, trigger, s)
message = ('[REDDIT] {title} {link}{nsfw} | {points} points ({percent}) | '
'{comments} comments | Posted by {author} | '
'Created at {created}')
if s.is_self:
link = '(self.{})'.format(s.subreddit.display_name)
else:
link = '({}) to /r/{}'.format(s.url, s.subreddit.display_name)
if s.over_18:
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]'
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.score > 0:
point_color = colors.GREEN
else:
point_color = colors.RED
percent = color(unicode(s.upvote_ratio * 100) + '%', point_color)
h = HTMLParser.HTMLParser()
message = message.format(
title=h.unescape(s.title), link=link, nsfw=nsfw, points=s.score, percent=percent,
comments=s.num_comments, author=author, created=created)
bot.say(message)
def rcomment_info(bot, trigger, s):
message = ('[REDDIT] Comment on {link}{nsfw} | {created} | {points} points | '
'{author}: {message}')
if (s.comments[0] == None):
return
c = s.comments[0]
if s.is_self:
link = '(self.{})'.format(s.subreddit.display_name)
else:
link = '({}) to /r/{}'.format(s.url, s.subreddit.display_name)
if s.over_18:
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 c.author:
author = c.author.name
else:
author = '[deleted]'
tz = time.get_timezone(bot.db, bot.config, None, trigger.nick,
trigger.sender)
time_created = dt.datetime.utcfromtimestamp(c.created_utc)
created = time.format_time(bot.db, bot.config, tz, trigger.nick,
trigger.sender, time_created)
h = HTMLParser.HTMLParser()
message = message.format(
title=h.unescape(s.title), link=s.short_link, nsfw=nsfw, points=c.score,
author=author, created=created, message=c)
bot.say(message)
# If you change this, you'll have to change some other things...
@commands('redditor')
@example('.redditor Batman')
def redditor_info(bot, trigger, match=None):
"""Show information about the given Redditor"""
commanded = re.match(bot.config.prefix + 'redditor', trigger)
r = praw.Reddit(user_agent=USER_AGENT)
match = match or trigger
try:
u = r.get_redditor(match.group(2))
except:
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 + ' | 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))
bot.say(message)
# If you change the groups here, you'll have to change some things above.
@rule('.*%s.*' % user_url)
def auto_redditor_info(bot, trigger):
redditor_info(bot, trigger)
@commands('setsafeforwork', 'setsfw')
@example('.setsfw true')
@example('.setsfw false')
def update_channel(bot, trigger):
"""
Sets the Safe for Work status (true or false) for the current
channel. Defaults to false.
"""
if bot.privileges[trigger.sender][trigger.nick] < OP:
return
else:
sfw = trigger.group(3).strip().lower() == 'true'
bot.db.set_channel_value(trigger.sender, 'sfw', sfw)
if sfw:
bot.reply('Got it. %s is now flagged as SFW.' % trigger.sender)
else:
bot.reply('Got it. %s is now flagged as NSFW.' % trigger.sender)
@commands('getsafeforwork', 'getsfw')
@example('.getsfw [channel]')
def get_channel_sfw(bot, trigger):
"""
Gets the preferred channel's Safe for Work status, or the current
channel's status if no channel given.
"""
channel = trigger.group(2)
if not channel:
channel = trigger.sender
channel = channel.strip()
sfw = bot.db.get_channel_value(channel, 'sfw')
if sfw:
bot.say('%s is flagged as SFW' % channel)
else:
bot.say('%s is flagged as NSFW' % channel)