-
Notifications
You must be signed in to change notification settings - Fork 0
/
point_refresher.py
77 lines (66 loc) · 2.22 KB
/
point_refresher.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
from json import load, dump
import scrapetube
from chat_downloader import ChatDownloader, errors
import sqlite3
conn = sqlite3.connect("points.db")
cur = conn.cursor()
try:
data = load(open("preferences.json", "r"))
except FileNotFoundError:
data = {}
with open("preferences.json", "w") as f:
dump(data, f)
exit(0)
try:
with open("known_streams.txt", "r") as f:
known_streams = f.read().splitlines()
known_streams = [x.replace("\n", "") for x in known_streams]
print(known_streams)
except FileNotFoundError:
known_streams = []
with open("known_streams.txt", "w") as f:
pass
def ignore_exc(iterable):
iterator = iter(iterable)
while True:
try:
item = next(iterator)
except StopIteration:
break
except:
continue
yield item
# Create table if not exists
cur.execute(
"CREATE TABLE IF NOT EXISTS points (user_id TEXT, points INTEGER, channel_id TEXT)"
)
conn.commit()
for channel_id in data.keys():
vids = scrapetube.get_channel(channel_id, content_type="streams")
print(f"Processing for channel {channel_id}")
vids = [vid for vid in vids if vid["videoId"] not in known_streams]
for vid in vids:
stream_id = vid["videoId"]
print(f"processing {stream_id}")
try:
chat = ChatDownloader().get_chat(stream_id)
except Exception as e:
continue
for message in ignore_exc(chat):
user_id = message["author"]["id"]
# add 10 points to user
cur.execute(
f"SELECT * FROM points WHERE user_id = '{user_id}' and channel_id = '{channel_id}'"
)
if cur.fetchone() is None:
cur.execute(
f"INSERT INTO points (user_id, points, channel_id) VALUES ('{user_id}', 10, '{channel_id}')"
)
else:
cur.execute(
f"UPDATE points SET points = points + 10 WHERE user_id = '{user_id}' and channel_id = '{channel_id}'"
)
conn.commit()
# print(f"added 10 points to {user_id}")
with open("known_streams.txt", "a") as f:
f.write(stream_id + "\n")