-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
129 lines (105 loc) · 4.19 KB
/
main.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
import youtube
import playstv
import config
import time
import re
import praw
import os
import traceback
import sqlite3
import random
# regex used to find a playstv url
re_url = re.compile('(https?:\/\/(?:[a-z0-9-]+\.)*plays\.tv(?:\S*)?)')
################################################################################
print('Opening database...')
db = sqlite3.connect(config.DATABASE_FILE)
print('Logging into Reddit...')
reddit = praw.Reddit(user_agent=config.REDDIT_USER_AGENT)
reddit.login(config.REDDIT_USER, config.REDDIT_PASSWD, disable_warning=True)
print('Logging into YouTube...')
yt = youtube.auth()
def init_db():
c = db.cursor()
c.execute('CREATE TABLE IF NOT EXISTS reddit(postid TEXT PRIMARY KEY NOT NULL);')
c.execute('CREATE TABLE IF NOT EXISTS youtube(playsid TEXT PRIMARY KEY NOT NULL, youtubeid TEXT NOT NULL);')
db.commit()
def get_cache_video(video_id):
c = db.cursor()
c.execute('SELECT youtubeid FROM youtube WHERE playsid=?', [video_id])
r = c.fetchone()
if r:
return r[0]
return False
def add_cache_video(video_id, youtube_id):
c = db.cursor()
c.execute('INSERT INTO youtube VALUES (?, ?)', [video_id, youtube_id])
db.commit()
def add_cache_reddit(reddit_id):
c = db.cursor()
c.execute('INSERT INTO reddit VALUES (?)', [reddit_id])
db.commit()
def is_already_visited(reddit_id):
c = db.cursor()
c.execute('SELECT * FROM reddit WHERE postid=?', [reddit_id])
r = c.fetchone()
return r is not None
def check_subreddit(subreddit):
print('Checking /r/' + subreddit)
# retreiving new submissions
submissions = reddit.get_subreddit(subreddit).get_new(limit=20)
for submission in submissions:
match = re.search(re_url, submission.url)
# if the post isn't a playstv url, check the selftext
if not match:
match = re.search(re_url, submission.selftext)
if match and not is_already_visited(submission.id):
url = match.group(1)
video_id = playstv.get_video_id(url)
video_title = playstv.get_title(url)
video_author = playstv.get_author(url)
if video_author in config.BLACKLIST:
add_cache_reddit(submission.id)
return
if not video_id:
print('Unable to find video id for ' + url)
add_cache_reddit(submission.id)
return
elif not video_title:
print('Unable to find video title for ' + url)
add_cache_reddit(submission.id)
return
# if the playstv title is the default one, we use the reddit title
if video_title.startswith('Check out this'):
video_title = submission.title
print('PlayTv video found: ' + video_title)
youtube_id = get_cache_video(video_id)
if not youtube_id:
filename = config.DOWNLOAD_FOLDER + video_id + '.mp4'
print('Downloading...')
playstv.download(video_id)
print('Done downloading!')
print('Uploading to Youtube...')
youtube_id = youtube.upload(yt, config.DOWNLOAD_FOLDER + video_id + '.mp4', video_title,
config.YOUTUBE_DESCR.format(playstv_url=url, reddit_url=submission.short_link), '')
add_cache_video(video_id, youtube_id)
print('Done! Youtube ID: ' + youtube_id)
else:
print('Video already downloaded! Youtube ID: ' + youtube_id)
print('Replying to reddit post...')
submission.add_comment(config.REDDIT_MSG.format(url = youtube_id, quote = random.choice(config.QUOTES)))
submission.upvote()
print('All done!\n-------------------------')
add_cache_reddit(submission.id)
# create video folder if doesn't exists
if not os.path.exists(config.DOWNLOAD_FOLDER):
os.makedirs(config.DOWNLOAD_FOLDER)
init_db()
while True:
print('Checking subreddits...')
for sub in config.SUBREDDITS:
try:
check_subreddit(sub)
except Exception, err:
traceback.print_exc()
pass
time.sleep(2)