-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
206 lines (185 loc) · 9.52 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
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
import requests #dependencyrer
from lib.imageGeneration.ImageGenerator import makeimage
import tweepy
import facebook
import json
import rss
import time
from lib.instagram.heliohostInstagram import heliohostInstagram
from replaceHTMLCodes import replaceHTMLCodes
import traceback
from pathlib import Path
def defineVariables():
try:
with open('credentials_dev.json') as f:
credentials = json.load(f)
except:
try:
with open('credentials_hhprod.json') as f:
credentials = json.load(f)
except:
with open('credentials_prod.json') as f:
credentials = json.load(f)
global image_url, discord, twitter, insta, fb, discord_url, twitter_api, twitter_api_secret, twitter_access_token_secret, twitter_api_secret, twitter_bearer_token, twitter_access_token, fb_app_token, fb_page_id, ig_username, ig_password, outputdir, instagram_app_token, instagram_page_id
discord = credentials["discord"] #discord section
twitter = credentials["twitter"] #retreves twitter section for use below
insta = credentials["instagram"] #gets instagram section
fb = credentials["facebook"] #gets facebook section
discord_url = discord["webhook_url"] #webhook url from discord
twitter_api = twitter["api_key"] #twitter api key public
twitter_api_secret = twitter["api_key_secret"] #twitter api key secret
twitter_bearer_token = twitter["bearer_token"] #not needed now, but just in case
twitter_access_token = twitter["access_token"] #twitter access token
twitter_access_token_secret = twitter["access_token_secret"] #twitter access token secret
fb_app_token = fb["access_token"] #facebook app token
fb_page_id = fb["page_id"] #facebook page id
"""
ig_username = insta["username"]
ig_password = insta["password"]
"""
instagram_app_token = insta["access_token"]
instagram_page_id = insta["page_id"]
outputdir = credentials["general"]["img_dir"]
image_url = credentials["general"]["img_url"]
def run(textin, title, link, pubdate):
defineVariables()
"""
try:
#deletes all old images in the output directory before running
#shutil.rmtree(outputdir)
os.mkdir(outputdir)
except:
#if it is already not there, make it without erroring. Won't catch or care about other errors, so I hope there is no issue
os.mkdir(outputdir)
"""
Path(outputdir).mkdir(parents=True, exist_ok=True) # makes the output directory if it doesn't exist
#------------------------------- VARIABLES -------------------------------#
# Most of these are used for the discord webhook, but some are used for the other parts
#for all params, see https://discordapp.com/developers/docs/resources/webhook#execute-webhook
# Makes sure text fits within a character limit
text = textin
tweettext = title + " - " + textin
fbtext = title + "\n\n" + textin
# Truncates the link to remove the ?do=findComment&comment=###### part
try:
updated_link = link[:link.index("?")]
except ValueError:
updated_link = link
# Adds the title to the beginning of the text
text = "\n# " + title + "\n\n" + text
if (len(text) > 2000):
text = text[:1900] + "..."
# Webhook Data
data = {
#"username" : title, # Username of the webhook
"content" : "@everyone " + text + "\n\n" + updated_link # Message Contents and adds edited link to the end
}
# Replaces HTML Codes with their respective characters
rpHTML = replaceHTMLCodes(text)
text = rpHTML.replaceAmpersandCodes()
#------------------------------- END VARIABLES -------------------------------#
#------------------------------ DISCORD WEBHOOK ------------------------------#
try:
#Trying to post to discord
result = requests.post(discord_url, json = data)
#idk what this does, I got it from the internet, but it works so i'm happy.
try:
result.raise_for_status()
except requests.exceptions.HTTPError as err:
print("Discord news was not posted")
print(err)
else:
print("Discord news posted, code {}.".format(result.status_code))
except Exception as disc:
# if an error occured, catch it so the rest of the program still runs
print("\033[31m" + "An Exception Occured while attempting to post to Discord, but was caught. Here is the error: ")
#traceback.print_tb(disc.__traceback__)
print(traceback.format_exc())
print("\033[33m" + "The Program is still running, but the Discord post was not made." + "\033[0m")
#------------------------------- END DISCORD WEBHOOK -------------------------------#
#---------------------------------- FACEBOOK BOT ---------------------------------#
try:
fb = facebook.GraphAPI(access_token=fb_app_token, version="2.12")
resp = fb.get_object('me/accounts')
page_access_token = None
for page in resp['data']:
if page['id'] == fb_page_id:
page_access_token = page['access_token']
fb = facebook.GraphAPI(page_access_token)
fb.put_object(
parent_object="me",
connection_name="feed",
message=fbtext,
link=updated_link)
feed = fb.get_connections(fb_page_id, "feed")
last_post = feed["data"][0]["id"]
post_id = last_post.split("_")[1]
print("Facebook news was posted - https://www.facebook.com/HelioHost.org/posts/" + post_id)
except Exception as f:
print("\033[31m" + "An Exception Occured while attempting to post to Facebook, but was caught. Here is the error:")
#traceback.print_tb(f.__traceback__)
print(traceback.format_exc())
print("\033[33m" + "The Program is still running, but the Facebook post was not made." + "\033[0m")
#-------------------------------- END FACEBOOK BOT -------------------------------#
#---------------------------------- TWITTER BOT ----------------------------------#
# Authenticate to Twitter
try:
client = tweepy.Client(
consumer_key=twitter_api, consumer_secret=twitter_api_secret,
access_token=twitter_access_token, access_token_secret=twitter_access_token_secret
)
if (len(tweettext) > 256):
response = client.create_tweet(text=tweettext[:233] + "... " + updated_link)
print(f"Twitter news was posted - https://twitter.com/user/status/{response.data['id']}")
else:
response = client.create_tweet(text=tweettext + " " + updated_link)
print(f"Twitter news was posted - https://twitter.com/user/status/{response.data['id']}")
except Exception as t:
print("\033[31m" + "An Exception Occured while attempting to post to Twitter, but was caught. Here is the error:")
#traceback.print_tb(t.__traceback__)
print(traceback.format_exc())
print("\033[33m" + "The Program is still running, but the Twitter post was not made." + "\033[0m")
#------------------------------- END TWITTER BOT -------------------------------#
#---------------------------------- INSTAGRAM BOT ----------------------------------#
if (instagram_app_token == ""):
print("Skipping Instagram")
else:
try:
ig = heliohostInstagram(instagram_app_token)
photos = []
maxlength = 600 #max amount of characters on an image
if (len(text) > maxlength):
sections = int(len(text)/maxlength + 1) #number of images to create, aka the sections of text to use in each image
if (sections > 10):
sections = 10
for i in range(sections):
ig_text = text[maxlength * i:maxlength * (i + 1)] + "..."
outfile = outputdir + "/output-" + time.strftime("%Y%m%d-%H%M%S") + "-" + str(i) + ".png"
with open(outfile, 'w') as fp:
pass
photo = makeimage("dependencies/images/heliohost-bg.png",text=ig_text,font_size=85, font_down_offset=200, savetype="file", output_filename=outfile)
print(photo[1] + " " + str(i))
photos += [[photo][0]]
ig.upload_carousel(photos, text, image_url)
else:
ig_text = text
outfile = outputdir + "/output-" + time.strftime("%Y%m%d-%H%M%S") + "-0.png"
photo = makeimage("dependencies/images/heliohost-bg.png",text=ig_text,font_size=85, font_down_offset=200, savetype="both", output_filename=outfile)
ig.upload_photo(photo, text, image_url)
except Exception as i:
print("\033[31m" + "An Exception Occured while attempting to post to Instagram, but was caught. Here is the error:")
#traceback.print_tb(i.__traceback__)
print(traceback.format_exc())
print("\033[33m" + "The Program is still running, but the Instagram post was not made." + "\033[0m")
#------------------------------- END INSTAGRAM BOT -------------------------------#
# Main Loop
if __name__ == '__main__':
# HAHA SUCKER YOU THOUGHT THAT WAS THE MAIN LOOP IT WAS JUST AN IF STATEMENT
# THIS IS THE MAIN LOOP
while(True):
# datapt is short for data point, it is the data point that is returned from the rss feed
datapt = rss.heliohostrss()
if (datapt[0] == True):
# text, title, link, publication date
run(datapt[1], datapt[2], datapt[3], datapt[4])
time.sleep(10)