-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·103 lines (87 loc) · 3.45 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
#! /usr/bin/env python
from datetime import datetime
import json
import time
import base64
import praw
import twitter
def post(event, context) -> None:
# event is populated by data from cloud pub/sub message
# in this case, the data is the config ()
try:
config = json.loads(base64.b64decode(event['data']).decode('utf-8'))
except Exception as ex:
print(
"An error occured while attempting to read the config. Exception: " +
repr(ex)
)
subreddit_name = config["Reddit"]["subreddit"]
sort_by = config["Reddit"]["sort_by"]
author_name = config["Reddit"]["author"]
twitter_name = config["Twitter"]["name"]
twitter_count = config["Twitter"]["tweet_count"]
message_prefix = config["message_prefix"]
message_suffix = config["message_suffix"]
message_replace = config["message_replace"]
testing = config["testing"]
reddit = praw.Reddit(
user_agent=config["Reddit"]["auth"]["username"],
client_id=config["Reddit"]["auth"]["client_id"],
client_secret=config["Reddit"]["auth"]["client_secret"],
username=config["Reddit"]["auth"]["username"],
password=config["Reddit"]["auth"]["password"],
)
twitterApi = twitter.Api(
consumer_key=config["Twitter"]["auth"]["consumer_key"],
consumer_secret=config["Twitter"]["auth"]["consumer_secret"],
access_token_key=config["Twitter"]["auth"]["access_key"],
access_token_secret=config["Twitter"]["auth"]["access_secret"],
tweet_mode="extended",
)
subreddit = reddit.subreddit(subreddit_name)
# Refactor number to something more descriptive
posts = subreddit.hot(limit=1)
if sort_by == 0:
posts = subreddit.hot(limit=5)
elif sort_by == 1:
posts = subreddit.new(limit=5)
elif sort_by == 2:
posts = subreddit.top(limit=5)
else:
print("Invalid config! Please change subreddit_sort_by to 0, 1, or 2")
# Of all the posts on the subreddit, we need to find the daily thread
submission = ""
for post in posts:
if post.author == author_name:
submission = post
print("Found post titled: " + post.title)
if submission.author != author_name:
print("Submission author not found. Edit the config and try again.")
exit()
updates = twitterApi.GetUserTimeline(
screen_name=twitter_name, count=twitter_count)
message = message_prefix + "\n\n"
print(len(updates) + " tweets found from " + twitter_name)
# We should probably filter map reduce lambda instead for extra coolness
# think of all the buzzwords!
for tweet in updates:
tweet_date = datetime.strptime(
tweet.created_at, "%a %b %d %H:%M:%S %z %Y")
if tweet_date.date() == datetime.today().date():
message = message + (str(tweet.full_text) + "\n\n").replace(
str(message_replace[0]), str(message_replace[1])
)
for i in range(len(message_suffix)):
message = message + str(message_suffix[i]) + "\n\n"
if testing:
print("Since this is just a test, we won't actually post it on Reddit.")
print(message)
else:
try:
submission.reply(message)
print("Message posted on Reddit.")
except Exception as exception:
print(
"There was a problem submitting the message to Reddit."
"Exception: " + repr(exception)
)