-
Notifications
You must be signed in to change notification settings - Fork 53
/
bot_mentions.py
96 lines (74 loc) · 2.82 KB
/
bot_mentions.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
import os
import sys
import praw
import re
from tinydb import TinyDB, Query
import anim
from collections import Counter
import spaw
from twisted.internet import task, reactor
streamable_username = os.environ.get("streamable_username")
streamable_password = os.environ.get("streamable_password")
reddit_client_id = os.environ.get("reddit_client_id")
reddit_client_secret = os.environ.get("reddit_client_secret")
reddit_refresh_token = os.environ.get("reddit_refresh_token")
_spaw = spaw.SPAW()
_spaw.auth(streamable_username, streamable_password)
db = TinyDB("db.json")
reddit = praw.Reddit(
client_id=reddit_client_id,
client_secret=reddit_client_secret,
refresh_token=reddit_refresh_token,
user_agent="/u/objection-bot v0.1",
)
User = Query()
print("starting...")
def get_comment_chain(comment):
if not isinstance(comment, praw.models.Comment):
return
parent_comment = get_comment_chain(comment.parent())
if parent_comment is not None:
return [comment, *parent_comment]
else:
return [comment]
def get_submission(comment):
if not isinstance(comment, praw.models.Comment):
return comment
else:
return get_submission(comment.parent())
def init_stream(subreddit_name: str):
subreddit = reddit.subreddit(subreddit_name)
return subreddit.stream.comments(pause_after=-1)
def check_mentions():
for message in reddit.inbox.mentions():
if len(db.search(User.id == message.id)) == 0:
try:
comment = reddit.comment(message.id)
print(f"doing {comment.id} (https://www.reddit.com{comment.permalink})")
# handle metadata
print(f"handling metadata...")
db.insert({"id": comment.id})
comments = list(reversed(get_comment_chain(comment)))[:-1]
authors = [comment.author.name for comment in comments]
most_common = [t[0] for t in Counter(authors).most_common()]
submission = get_submission(comment)
# generate video
output_filename = f"{comment.id}.mp4"
print(f"generating video {output_filename}...")
characters = anim.get_characters(most_common)
anim.comments_to_scene(
comments, characters, output_filename=output_filename
)
# upload video
print(f"uploading video...")
response = _spaw.videoUpload(output_filename)
print(response)
comment.reply(
f"[Here's the video!](https://streamable.com/{response['shortcode']})"
)
print(f"done {comment.id}")
except Exception as e:
print(e)
l = task.LoopingCall(check_mentions)
l.start(60.0 * 5)
reactor.run()