-
Notifications
You must be signed in to change notification settings - Fork 0
/
DirectImageLinkerBot.py
189 lines (166 loc) · 9.92 KB
/
DirectImageLinkerBot.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
import praw
import time
import re
import shelve
import bot_data as d
import schedule
app_id = d.app_id
refresh_token = d.refresh_token
app_secret = d.app_secret
app_uri = d.app_uri
user_agent = d.user_agent
del d
nonreplyusers = ["directimagelinkerbot", "imgurtranscriber", "automoderator", "apicontraption", "shitty_biden_bot", "shitty_trump_bot"] #make sure all in lower case
bannedRegex = re.compile("You've been banned from participating in /r/(.*)")
bannedRegex2 = re.compile("Your ban from /r/(.*) has changed")
data = shelve.open("data", "c") # is a dictionary
imgurRegex = re.compile("((((http|https)\:\/\/)|^|(?<= ))(www\.|i\.|m\.)?imgur\.com\/[a-zA-Z0-9]{6,7}?(?!(\.jpg|\.gif|\.gifv|\.png|\.jpeg|\.webm|,))(?=[^a-zA-Z0-9]|$| |\)))") #finds indirect imgur links only.
filetypes = [".png", ".gif", ".jpg", "gifv", "jpeg", "webm"]
url = ""
short_footer = "\n\n---\n[^Feedback](https://goo.gl/ChDHYn) ^| [^Already ^a ^direct ^link?](https://goo.gl/JVo094) ^| [^Why ^do ^I ^exist?](https://goo.gl/8WwAcJ) ^| [^Source](https://goo.gl/SBWyvz)"
footer = "\n\n---\n[^Feedback](https://np.reddit.com/message/compose/?to=DirectImageLinkBot&subject=Feedback&message=Don%27t%20forget%20to%20include%20a%20link%20to%20your/my%20comment) ^| [^Already ^a ^direct ^link?](https://np.reddit.com/r/DirectImageLinkerBot/wiki/res_links) ^| [^Why ^do ^I ^exist?](https://np.reddit.com/r/DirectImageLinkerBot/wiki/index) ^| [^Source](https://github.com/Theonefoster/DirectImageLinkerBot/blob/master/DirectImageLinkerBot.py)"
short_footer = footer #keeps tripping the spam filter so they're both the same for now
no_shortlink_subs = {}
if "banned" not in data.keys():
data["banned"] = {'pics', 'Romania', 'Weakendgunnit', 'wtf', 'Watches', 'battlestations', 'aww', 'atheism', 'talesfromtechsupport', 'gaming', 'weareportadelaide', 'politics', 'OnePiece', 'imagesofthe1990s', 'totalwar', 'history', 'OhCumOn', 'Futurology', 'japanlife', 'DaystromInstitute', 'Firefighting', 'redditgetsdrawn', 'NASCAR', 'creepy', 'books', 'movies', 'EarthPorn', 'labsafety', 'food', 'Minecraft', 'dogs', 'Cardinals', 'Wishlist', 'sunsetshimmer', 'gifs', 'photoshopbattles', 'SubredditDrama', 'PercyJacksonRP', 'cycling', 'RedditMinusMods', 'PoliticalDiscussion', 'anime', 'weekendgunnit', 'Denmark', 'EatCheapAndHealthy', 'mylittleandysonic1', 'funny', 'AskReddit', 'GetMotivated', 'TheSimpsons', 'DIY', 'mylittlepony'}
data.sync()
if "doneSubmissions" not in data.keys():
data["doneSubmissions"] = set()
data.sync()
print("Bot is banned from: " + str(data["banned"]))
def login():
print("Logging in..")
r = praw.Reddit(user_agent, disable_update_check=True)
r.set_oauth_app_info(app_id, app_secret, app_uri)
r.refresh_access_information(refresh_token)
print("Logged in as " + r.user.name)
return r
r = login()
data["loops"] = 31 #so that it checks mail on launch.
#data['doneSubmissions'] = set() #reset doneSubmissions
data.sync()
def mail():
msgs = list(r.get_unread(unset_has_mail=False, update_user=False))
for msg in msgs:
if msg.author is not None and msg.author.name == "AutoModerator":
msg.mark_as_read()
else:
match = re.findall(bannedRegex, msg.subject)
if not match:
match = re.findall(bannedRegex2, msg.subject)
if match:
if match[0] not in data['banned'] and msg.author is None: #subreddit ban messages have no author
sublist = data['banned']
sublist.add(str(match[0]))
data['banned'] = sublist
data.sync()
msg.reply("/r/" + str(match[0]) + " has been added to my ignore list. I won't bother you again.")
msg.mark_as_read()
print ("Added /r/" + str(match[0]) + " to ignore list.")
def submissions():
submissions = list(r.get_new(limit=100)) #4 per second
for submission in submissions:
if submission.id not in data["doneSubmissions"]:
donelist = data["doneSubmissions"]
donelist.add(submission.id)
data["doneSubmissions"] = donelist
data.sync()
if (submission.author.name.lower() not in nonreplyusers) and (submission.subreddit.display_name not in data['banned']):
if (submission.domain == "imgur.com") and not ("/a/" in submission.url) and not ("gallery" in submission.url) and (not submission.url[-4:] in filetypes):
if not submission.url.startswith("http"):
url = "https://" + submission.url
else:
if submission.url.startswith("http:/"):
url = "https" + submission.url[4:]
else:
url = submission.url
try:
if submission.subreddit.display_name.lower() in no_shortlink_subs:
submission.add_comment("[Here is a direct link to the image OP submitted for the benefit of mobile users](" + url + ".jpg)" + footer)
else:
submission.add_comment("[Here is a direct link to the image OP submitted for the benefit of mobile users](" + url + ".jpg)" + short_footer)
except Exception as e:
print ("Error trying to post to /r/" + submission.subreddit.display_name)
print (str(e))
else:
print ("Submission reply sent to " + submission.author.name + "!")
def comments():
subreddit = r.get_subreddit("all")
comments = list(subreddit.get_comments(limit=100, fetch=True)) #around 22 comments come in per second at peak time according to a single sample and my very reliable rough estimate
for comment in comments:
if comment.id not in data["doneSubmissions"]:
donelist = data["doneSubmissions"]
donelist.add(comment.id)
data["doneSubmissions"] = donelist
data.sync()
if (comment.author.name.lower() not in nonreplyusers) and (comment.subreddit.display_name not in data['banned']):
commentText = comment.body
match = list(re.findall(imgurRegex, commentText))
if (len(match) > 0):
if len(match) == 1: #one link in comment
if not match[0][0].startswith("http"):
url = "https://" + match[0][0]
else:
if match[0][0].startswith("http:/"):
url = "https" + match[0][0][4:]
else:
url = match[0][0]
if "gallery" not in url and "/a/" not in url:
try:
if comment.subreddit.display_name.lower() in no_shortlink_subs:
comment.reply("[Here is a direct link to your image for the benefit of mobile users](" + url + ".jpg)" + footer)
else:
comment.reply("[Here is a direct link to your image for the benefit of mobile users](" + url + ".jpg)" + short_footer)
except Exception as e:
print ("Error trying to post comment reply in /r/" + comment.subreddit.display_name)
print (str(e))
else:
print ("Comment reply sent to " + comment.author.name + "!")
elif len(match) < 10:
reply = ("Here are direct links to those images for the benefit of mobile users: \n\n")
for submatch in match:
if "gallery" not in submatch[0] and "/a/" not in submatch[0]: #what if gallery is in every match? It outputs an empty comment. TODO
if not submatch[0].startswith("http"):
url = "https://" + submatch[0]
else:
if submatch[0].startswith("http:/"):
url = "https" + submatch[0][4:]
else:
url = submatch[0]
reply = reply + (url + ".jpg\n\n")
if comment.subreddit.display_name.lower() in no_shortlink_subs:
reply += footer
else:
reply += short_footer
if len(reply) > (len(footer) + 100): #73 is len() of header ^ so this should cover there being at least 2 links
comment.reply(reply)
print ("Comment reply sent to " + comment.author.name + "!")
def clear_set():
data["doneSubmissions"] = set()
data.sync()
time.sleep(30)
#schedule.every(1).hours.do(clear_set)
while True:
try:
r.handler.clear_cache()
data["loops"] = data["loops"] + 1
if data["loops"] > 6: #no point checking mail every loop. 6 loops is about every 2 minutes depending on number of directimage comments sent
data["loops"] = 0
mail()
schedule.run_pending()
comments()
submissions()
data.sync()
except praw.errors.RateLimitExceeded as e:
print ("Rate limit exceeded! - " + str(e))
except praw.errors.HTTPException as e:
print ("HTTP Error - " + str(e._raw.status_code) + e._raw.reason)
pass
except Exception as e:
print (str(e))
try:
r = login()
except Exception as e:
print (str(e))
print ("Login Error. Sleeping..") # Mostly for when reddit is returning 503 on most requests.
time.sleep(30)