-
Notifications
You must be signed in to change notification settings - Fork 6
/
reddit.py
261 lines (243 loc) · 10.5 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import discord
from discord.ext import commands
import praw, prawcore.exceptions
from urllib.request import urlopen, Request
import urllib.parse
from datetime import datetime, timedelta
import random
import re
import utils
class Reddit(commands.Cog):
disabled_subs = ['coconutwater','trypophobia']
disabled_str = "Sorry, retrieving posts from that sub is not allowed"
def __init__(self,bot):
self.bot = bot
# get tokens from file
f = open('tokens.txt','r')
reddit_clientid = f.readline().strip()
reddit_token = f.readline().strip()
discord_token = f.readline().strip()
f.close()
self.reddit = praw.Reddit(client_id=reddit_clientid,
client_secret=reddit_token,
user_agent='windows:natsgifbot (by /u/efitz11)')
def get_comment(self, url):
# comment = self.reddit.comment(url=url)
comment = self.reddit.submission(url=url).comments[0]
return comment.body
def sub(self, subreddit, selfpost=False,limit=25):
if subreddit in self.disabled_subs:
return [self.disabled_str]
list = []
try:
for submission in self.reddit.subreddit(subreddit).hot(limit=limit):
if submission.is_self == selfpost and not submission.stickied:
list.append(submission)
num = random.randint(0,len(list)-1)
return self.get_submission_string(list[num])
except prawcore.exceptions.Redirect:
return ["Error: subreddit not found"]
@commands.command()
async def u(self, ctx, *text:str):
"""<user> get a random comment from a user"""
text = ''.join(text)
user = self.reddit.redditor(text)
comments = user.comments.new(limit=10)
idx = random.randint(0, 9)
i = 0
for comment in comments:
if i == idx:
text = self.printcomment(comment)
await ctx.send(text)
return
else:
i += 1
@commands.command()
async def un(self, ctx, *text:str):
"""<user> get newest comment from a user"""
text = ''.join(text)
user = self.reddit.redditor(text)
comments = user.comments.new(limit=1)
for c in comments:
await ctx.send(self.printcomment(c))
return
@commands.command()
async def us(self, ctx, user:str, *search:str):
"""<user> search user comments for text"""
search = ' '.join(search).lower()
u = self.reddit.redditor(user)
comments = u.comments.new(limit=25)
for c in comments:
if search in c.body.lower():
await ctx.send(self.printcomment(c))
return
def printcomment(self, comment):
subreddit = comment.subreddit
author = comment.author
submission = comment.submission
title = submission.title
score = comment.score
link = "https://reddit.com" + comment.permalink + "?context=10"
t = utils.prettydate(int(comment.created_utc))
return "[%s] comment by **/u/%s** on \"**%s**\" in /r/%s, %s:\n```%s```<%s>" % (str(score), author, title, subreddit, t, comment.body, link)
@commands.command()
async def r(self, ctx,*text:str):
"""<subreddit> get a random link post from a subreddit"""
text = ''.join(text)
for string in self.sub(text):
if len(string)>0:
await ctx.send(string)
# await ctx.send(self.sub(text))
def getsubmissiontext(self, submission):
url = submission.url
s = ""
if submission.over_18:
s = "**post is NSFW; embed hidden**\n"
url = "<" + url + ">"
score = self.getsubmissionscore(submission)
if submission.is_self:
output = submission.title + "\n" + submission.shortlink
else:
output = s+"["+ score + "]\t " + submission.title + "\n" + url + " \t<" + submission.shortlink+">"
return output
def getsubmissionscore(self, submission):
score = submission.score
if score > 999:
score = str(int(score/100)/10.0) + "k"
else:
score = str(score)
return score
def get_submission_string(self, submission, hide=False):
ret = []
if submission.link_flair_text is not None:
flair = "*[%s]*" % submission.link_flair_text
else:
flair = ""
userflair = ""
if hasattr(submission, 'author_flair_type'):
if submission.author_flair_type == 'text' and submission.author_flair_text is not None:
userflair = " [*%s*] " % submission.author_flair_text
if submission.author_flair_type == 'richtext':
for i in submission.author_flair_richtext:
if 'e' in i and i['e'] == 'text':
userflair = " [*%s*] " % i['t'].strip()
# import time
# now = time.time()
# diff = datetime.fromtimestamp(now) - datetime.utcfromtimestamp(now)
# time = utils.prettydate(int(submission.created_utc) + int(diff.total_seconds()))
time = utils.prettydate(int(submission.created_utc))
if submission.is_self:
ret.append("[%s] **%s** %s - posted by /u/%s%s to /r/%s, %s" % (self.getsubmissionscore(submission), submission.title, flair, submission.author, userflair, submission.subreddit, time))
ret.append("%s" % submission.selftext)
ret.append("<%s>" % submission.shortlink[:1995])
else:
ret.append("[%s] **%s** - posted by /u/%s%s to /r/%s, %s" % (self.getsubmissionscore(submission), submission.title, submission.author, userflair, submission.subreddit, time))
ret.append("")
if submission.over_18:
ret.append("**post is NSFW; embed hidden**\n<%s>\t\t<%s>" % (submission.url, submission.shortlink))
else:
if hide:
ret.append("**post embed hidden by request**\n<%s>\t\t<%s>" % (submission.url, submission.shortlink))
else:
ret.append("%s\t\t<%s>"% (submission.url, submission.shortlink))
return ret
@commands.command()
async def rh(self, ctx, text:str, num:int=-1):
"""<subreddit> <num> get the #num post from subreddit/hot"""
#subreddit = ''.join(text).lower()
subreddit = text
if subreddit in self.disabled_subs:
await ctx.send(self.disabled_str)
return
count = 0
if num > 0:
for submission in self.reddit.subreddit(subreddit).hot(limit=(num+2)):
if submission.stickied:
continue
count += 1
if count == num:
for string in self.get_submission_string(submission):
if len(string) > 0:
await ctx.send(string)
else:
output = "10 hottest posts from r/%s\n" % text
for submission in self.reddit.subreddit(subreddit).hot(limit=10):
score = self.getsubmissionscore(submission)
output = output + "["+ score + "]\t " + submission.title + "\n\t\t<" + submission.shortlink + ">\n"
await ctx.send(output)
@commands.command()
async def rn(self, ctx, text:str, num:int=-1):
"""<subreddit> <num> get the #num post from subreddit/new"""
subreddit = text
if subreddit in self.disabled_subs:
await ctx.send(self.disabled_str)
return
count = 0
if num > 0:
for submission in self.reddit.subreddit(subreddit).new(limit=(num+2)):
count += 1
if count == num:
for string in self.get_submission_string(submission):
if len(string) > 0:
await ctx.send(string)
else:
output = "10 newest posts from r/%s\n" % text
for submission in self.reddit.subreddit(subreddit).new(limit=10):
score = self.getsubmissionscore(submission)
output = output + "["+ score + "]\t " + submission.title + "\n\t\t<" + submission.shortlink + ">\n"
await ctx.send(output)
@commands.command()
async def rs(self, ctx, subreddit:str, *query:str):
"""<subreddit> get the first post (by hot) matching the query"""
patterns = []
hidden = False
if "hide" in query:
query = ["" if x.lower() == "hide" else x for x in query]
hidden = True
for s in query:
patterns.append(re.compile(s,re.IGNORECASE))
if subreddit.endswith("/new"):
subreddit = subreddit[:subreddit.find("/")]
if subreddit in self.disabled_subs:
await ctx.send(self.disabled_str)
return
list = self.reddit.subreddit(subreddit).new(limit=100)
else:
i = subreddit.find("/")
if i != -1:
subreddit = subreddit[:i]
if subreddit in self.disabled_subs:
await ctx.send(self.disabled_str)
return
list = self.reddit.subreddit(subreddit).hot(limit=100)
for submission in list:
matched = True
for pat in patterns:
if not re.search(pat,submission.title):
matched = False
break
if matched:
# output = self.getsubmissiontext(submission)
# await ctx.send(output)
count = 0
for string in self.get_submission_string(submission, hide=hidden):
if len(string) == 0:
count += 1
continue
elif len(string) < 2000:
if count == 1:
string = "```%s```" % string
await ctx.send(string)
else:
strs = utils.split_long_message(string)
for s in strs:
await ctx.send("```%s```" % s)
count = count + 1
return
@commands.command()
async def fp(self, ctx):
"""get a random FP quote"""
for string in self.sub('justFPthings',selfpost=True, limit=250):
await ctx.send(string)
def setup(bot):
bot.add_cog(Reddit(bot))