-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
238 lines (182 loc) · 6.62 KB
/
utils.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
from quotes import (
bhendiCount,
happyCakeday,
randomQuote,
shutupSaiman,
)
from functools import lru_cache
import re
import os
import json
import sys
from datetime import datetime, date
from prawcore.exceptions import NotFound
from praw.exceptions import RedditAPIException
from Reddit import reddit
cakedayRedditors = []
def utcTime():
return datetime.utcnow().timestamp()
class SignalHandler:
def __init__(self):
import signal
signal.signal(signal.SIGINT, self._signalHandler)
signal.signal(signal.SIGTERM, self._signalHandler)
self.exitWhenLoopEnds = False
self.inLoop = False
def _signalHandler(self, signal, frame):
print(f"RECIEVED SIGNAL: {signal}, Bye")
if not self.inLoop:
sys.exit(0)
else:
self.exitWhenLoopEnds = True
def loopEnd(self):
self.inLoop = False
if self.exitWhenLoopEnds:
sys.exit(0)
def loopStart(self):
self.inLoop = True
def cakedayCheck(comment):
if comment.author in cakedayRedditors:
return False
try:
created = comment.author.created_utc
timeDiff = datetime.utcnow() - datetime.fromtimestamp(created)
res = timeDiff.days % 365 == 0
except NotFound:
try:
res = bool(comment.__dict__.get("author_cakeday"))
except Exception as e:
print(e)
return False
if res:
cakedayRedditors.append(comment.author)
return True
return False
def commentCheck():
print("Checking old comments")
me = reddit.user.me()
if not me:
return
for comment in me.comments.new():
# Get Already wished redditors btw runs
if re.search(r"^Happy cakeday", comment.body):
cakedayRedditors.append(comment.parent().author)
# Delete bad comnents
if comment.score < -4:
# comment.delete()
print("Deleted comment {parentId}")
# Pull a sneaky one
elif (
comment.score < 0
and "Quote Sauce" in comment.body
and "\u200e" not in comment.body
and utcTime() - comment.created_utc < 5000
):
comment.refresh()
if len(comment.replies) == 0:
from quotes import randomQuote
comment.edit(randomQuote() + "\u200e")
print(f"Pulled a sneaky one on {comment.permalink}")
def blockRedditor(redditor):
print("User Blocked: " + redditor.name)
reddit.redditor("I_eat_I_repeat").message("User Blocked", "u/" + redditor.name)
redditor.block()
def inboxCheck():
print("Checking Inbox")
for msg in reddit.inbox.messages():
if msg.subject == "Block me":
msg.reply("Okay done")
blockRedditor(msg.author)
def replyToComment(comment, replyTxt):
try:
replyComment = comment.reply(replyTxt)
comment.save()
print("\tSuccess: " + replyComment.id)
except RedditAPIException as e:
print(e)
def getAge(timestamp):
# timestamp in format YYYYMMDD
d = int(timestamp)
return (date.today() - date(d // 10000, d // 100 % 100, d % 100)).days
def _processSubtitle(vId):
# Checks if ydl has downloaded the subtitle and writes them to subs folder
for file_ in os.listdir():
if vId in file_:
with open(file_, "r") as f:
subData = f.read()
cleanSub = "\n\n".join(a for a in subData.split("\n\n")[1:] if a)
with open("subs/" + file_.split(".")[0], "w") as f:
f.write(cleanSub)
print("Downloaded Subtitle for " + vId)
os.remove(file_)
def downloadNewSubtitles():
print("Checking for new subtitles")
from youtube_dl import YoutubeDL
playlist = "https://www.youtube.com/playlist?list=UUy9cb7U-Asbhbum0ZXArvfQ"
ydlOpts = {
"ignoreerrors": True,
"no_warnings": True,
"quiet": True,
"outtmpl": "%(upload_date)s%(id)s",
"skip_download": True,
}
with YoutubeDL(ydlOpts) as ydl:
playlistRes = ydl.extract_info(playlist, download=False, process=False)
if not playlistRes:
print("Failed to download playlist, skipping new subtitle check")
return
# Youtubedl doesnt return the info dict if this option is passed
ydlOpts["writesubtitles"] = True
with YoutubeDL(ydlOpts) as ydl:
for vid in playlistRes["entries"]:
for subFile in os.listdir("subs/"):
if vid["id"] in subFile:
if getAge(subFile[:8]) > 30:
return
break
else:
ydl.process_ie_result(vid, download=True)
_processSubtitle(vid["id"])
@lru_cache
def getActiveSubs():
wikiPg = reddit.subreddit("SaimanSaid").wiki["activesubs"].content_md
return "+".join([a.strip() for a in wikiPg.splitlines() if a])
@lru_cache
def getPermanentRespones():
# Get a dict containing premanent responses
# {"hi saibot", "hello, user"}
wikiPg = reddit.subreddit("SaimanSaid").wiki["permanent_responses"].content_md
return json.loads(wikiPg)
def processComment(comment):
me = reddit.user.me()
if (
re.search(r"\b(chup|shut ?(the)? ?(fuck)? ?up|stop)\b", comment.body, re.I)
and comment.parent().author == me
):
print(f"Replying to '{comment.permalink}' with shutupSaiman")
replyToComment(comment, shutupSaiman())
elif cakedayCheck(comment):
print(f"Replying to '{comment.permalink}' with Cakeday")
replyToComment(comment, happyCakeday())
elif re.search(
r"\b(Bh[ei]+ndi|Sai(man)?-?(Said| ?bot)|Timothy|saiman)\b", comment.body, re.I
):
print(f"Replying to '{comment.permalink}' with random quote")
replyToComment(comment, randomQuote())
elif re.search(r"bhendicount", comment.body, re.I):
print(f"Replying to '{comment.permalink}' with bhendi count")
replyToComment(comment, bhendiCount(comment))
elif re.search(r"!saibot (ignore|block)", comment.body, re.I):
if comment.parent.author == me:
blockRedditor(comment.author)
elif comment.body.lower() == "serpentine":
print(f"Replying to '{comment.permalink}' with repost check")
replyToComment(
comment.submission, "This is an automated action \n\nu/repostsleuthbot"
)
else:
for reg, response in getPermanentRespones().items():
if re.compile(reg, re.I).search(comment.body):
print("replying with permanent response")
replyToComment(comment, response)
break