-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedditMirrorBot.py
366 lines (297 loc) · 11.3 KB
/
RedditMirrorBot.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# -*- encoding: utf-8 -*-
from configparser import ConfigParser
from json import load, dump
from os import getpid, listdir, remove, path, makedirs, rename
from shutil import rmtree
from prawcore.exceptions import RequestException, ServerError
from time import sleep, ctime, time
from requests import get
from copy import copy
import logging
import praw
import re
import subprocess
import youtube_dl
import os
import simplejson as json
import requests
import threading
import urllib.parse
# Initialize logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# Create log file handler
handler = logging.FileHandler('mirrorbot.log')
handler.setLevel(logging.DEBUG)
# Create logging format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.debug('Logging initialized.')
logger.debug("Opening config")
config = ConfigParser()
config.read("config.ini")
logger.debug("Config opened")
do_access_id = config["DigitalOcean"]["access_id"]
do_secret_key = config["DigitalOcean"]["secret_key"]
do_bucket_name = config["DigitalOcean"]["bucket_name"]
specified_sub = config["Reddit"]["sub_to_mirror"]
specified_host = config["General"]["backend_host"]
# Logger class for youtube_dl
class MyLogger(object):
def debug(self, msg):
logger.debug(msg)
def warning(self, msg):
logger.warning(msg)
def error(self, msg):
logger.error(msgs)
ydl_opts = {
'format': 'best[ext=webm]/bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
'logger': MyLogger(),
}
def cleanup_init():
if not path.exists("Media/"):
makedirs("Media/")
logger.debug("Media path doesn't exist, created it.")
return
else:
rmtree("Media/")
makedirs("Media/")
logger.debug("Cleaned Media path")
return
def cleanup(path):
if path[-1] != "/":
path += "/"
logger.debug("Cleanup " + path)
if not path.exists(path):
logging.debug("Cleanup: " + path + " does not exist. Creating it.")
makedirs(path)
return
else:
rmtree(path)
logging.debug("Cleanup: " + path + " successfully removed.")
return
# Returns true if a video for the submission already exists, false otherwise
def is_submission_mirrored(submission):
video_list = get_sub_video_list(do_bucket_name)
return (submission.id in video_list)
# Takes an s3 bucket object as input
# Returns a list of reddit submission IDs for which videos exist
def get_sub_video_list(bucket_name):
bucket = get_bucket(bucket_name)
logger.debug("get_video_list()")
regex = re.compile('/\w*\.', re.IGNORECASE)
raw_text = str([x for x in bucket.objects.all()])
video_list = regex.findall(raw_text)
return [x[1:-1] for x in video_list]
# Returns an object representing the specified bucket
def get_bucket(bucket_name):
logger.debug("Creating s3 resource object")
resource = boto3.resource('s3',
region_name='nyc3',
endpoint_url="https://nyc3.digitaloceanspaces.com",
aws_access_key_id=do_access_id,
aws_secret_access_key=do_secret_key)
return resource.Bucket(bucket_name)
# Takes in a url and attempts to download the video
# Returns true if successful
def download_video(url, sub_id=None, working_dir):
logger.info("Downloading " + url)
output_filename = working_dir + "video.mp4"
opts = copy(ydl_opts)
opts['outtmpl'] = output_filename
yt = youtube_dl.YoutubeDL(opts)
# Twitter post
if "twitter" in url:
logger.info('Twitter video detected.')
response = yt.extract_info(url, process=False)
try:
while response.get("url"):
response = yt.extract_info(response["url"], process=False)
except youtube_dl.utils.DownloadError:
logger.error("Twitter Youtube-dl download error. Unable to download video from URL.")
return False
url = response["webpage_url"]
# Reddit video, only entered if sub_id exists (won't be used for comment mirroring)
elif "v.redd.it" in url and sub_id:
if download_reddit_video(url, sub_id, working_dir, output_filename):
logger.info(url + " successfully downloaded.")
else:
logger.error(url + " was unable to be downloaded.")
# Otherwise, download normally
else:
try:
yt.download([url])
except (youtube_dl.utils.DownloadError) as e:
logger.error("Download error: " + str(e))
return False
except (youtube_dl.utils.SameFileError) as e:
logger.error("Download error: " + str(e))
return False
except (UnicodeDecodeError) as e:
logger.error("Download error: " + str(e))
return False
return True
# Attempts to download a reddit-hosted video.
# Returns true if successful
def download_reddit_video(url, sub_id, working_dir, output_filename):
sub = reddit.submission(id=sub_id)
if sub.media is None:
if hasattr(sub, "crosspost_parent"):
sub.media = reddit.submission(sub.crosspost_parent[3:]).media
else:
url = get(sub.url).url
_id = praw.models.reddit.submission.Submission.id_from_url(url)
sub.media = reddit.submission(_id).media
# Time added to filename to avoid processes writing over each other
video_url = sub.media["reddit_video"]["fallback_url"]
video_name = working_dir + (time()) + "_video"
download(video_name, video_url)
audio_url = video_url.rsplit("/", 1)[0] + "/audio"
audio_name = working_dir + str(time()) + "_audio"
download(audio_name, audio_url)
if sub.media["reddit_video"]["is_gif"]:
logger.debug("Reddit video is a gif.")
rename(video_name, output_filename)
#if not gif but still no audio
elif not 'octet-stream' in magic.Magic(mime=True,uncompress=True).from_file('Media/audio'):
logger.debug("Reddit video has no audio.")
rename(video_name, output_filename)
#audio exists
else:
logger.debug("Running combine_media() on reddit video.")
combine_media(video_name, audio_name, output_filename)
logger.debug("Media combining complete.")
return True
def combine_media(video, audio, output_filename):
output = str(time()) + "_output.mp4"
command = [
"ffmpeg",
"-v", "quiet",
"-i", "Media/" + video,
"-i", "Media/" + audio,
"-c", "copy",
"-f", "mp4",
"Media/" + output,
"-y"
]
subprocess.run(command)
def download(filename, url):
with open("Media/" + filename, "wb") as file:
file.write(get(url).content)
#converts given file to mp4, and returns new filename
def conv_to_mp4(file_name):
vid_name = file_name[:-4] + ".mp4"
##check if file is mkv and convert to mp4
if ".mkv" in file_name:
ffmpeg_subproc = [
"ffmpeg",
"-i", file_name,
"-strict", "-2", #fixes opus experimental error
"-vcodec", "copy",
"-y",
vid_name
]
conv_process = subprocess.run(ffmpeg_subproc)
return vid_name
else:
return file_name
def upload_video(file_name, _id):
file_name = conv_to_mp4(file_name)
logger.debug("Uploading to DO...")
save_file_size(file_name)
logger.debug("Size:", str(os.path.getsize(file_name)/1024/1024) + "MB")
client = session.client('s3',
region_name='nyc3',
endpoint_url="https://nyc3.digitaloceanspaces.com",
aws_access_key_id=do_access_id,
aws_secret_access_key=do_secret_key)
key = "videos/" + str(_id) + ".mp4"
try:
client.upload_file(file_name, 'pf-mirror-1', key)
except (boto3.S3UploadFailedError) as e:
logger.error(_id + " failed to upload: " + str(e))
return None
logger.debug(_id + " successfully uploaded.")
resource = boto3.resource('s3',
region_name='nyc3',
endpoint_url="https://nyc3.digitaloceanspaces.com",
aws_access_key_id=do_access_id,
aws_secret_access_key=do_secret_key)
logger.debug("DO key: " + key)
client.put_object_acl(ACL='public-read', Bucket='pf-mirror-1', Key=key)
key = "videos/" + key
mirror_url = "https://pf-mirror-1.nyc3.digitaloceanspaces.com/" + key
logger.info("Upload complete!")
return str(mirror_url)
# Takes in a submission object and attempts to mirror it
# Returns true if mirror was successful
def mirror_submission(submission):
working_dir = "Media/" + str(time()) + "_dl/"
cleanup(working_dir)
download_video(submission.url, submission.id, working_dir)
mirror_url = upload_video(working_dir + "video.mp4", submission.id)
# Listens for POST requests to create new mirrors
def network_listener():
# Watches for new subreddit submissions, and attempts to mirror them
def sub_watcher():
reddit = praw.Reddit(**config["Reddit"])
stream = reddit.subreddit(sub_to_mirror).stream.submissions(pause_after=1)
while True:
cleanup_submissions()
try:
# Get next post
submission = next(stream)
logger.debug("Got next post: ", submission.title, " ", "https://reddit.com" + submission.permalink + "\n" + submission.url)
except RequestException:
# Client side error
logger.error("RequestException in sub_watcher")
sleep(60)
except ServerError:
# Reddit side error
logger.error("ServerError in sub_watcher")
sleep(60)
except StopIteration:
logger.error("StopIteration in sub_watcher")
break
else:
if submission is None:
logger.info("No new posts.")
continue
if submission.is_self:
logger.info("Skipping self-post.")
continue
# Don't bother creating mirror for posts over a month old
if submission.created_utc < time() - 3600 * 24 * 30:
logger.info("Submission is too old.")
continue
if is_submission_mirrored(submission):
logger.info("Submission already mirrored.")
continue
if mirror_submission(submission):
logger.info(submission.id + " successfully mirrored!")
continue
else:
logger.error("Something went wrong with " + submission.id + ". Check the logs for info.")
continue
cleanup_submissions()
# Watches for new comments containing !mirror, and mirrors the parent comment's video if possible
def comment_watcher():
# Manages POST listener, comment watcher, and sub watcher
def main():
jobs = []
# sub_watcher
jobs.append(multiprocessing.Process(name="Submissions:", target=sub_watcher))
# comment_watcher
jobs.append(multiprocessing.Process(name="Comments:", target=comment_watcher))
# network listener
jobs.append(multiprocessing.Process(name="Network:", target=network_listener))
# start threads
for job in jobs:
job.start()
if __name__ == "__main__":
cleanup_init()
if path.exists("/usr/bin/ffmpeg"):
print(main())
else:
print("Needs ffmpeg")