-
Notifications
You must be signed in to change notification settings - Fork 1
/
post-twitter.py
103 lines (74 loc) · 3.25 KB
/
post-twitter.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
import os
import json
import github
import shutil
import random
import pprint
import requests
from pprint import pprint
from datetime import datetime
from TwitterAPI import TwitterAPI
api = TwitterAPI(os.environ.get('TWITTER_CONSUMER_KEY'),
os.environ.get('TWITTER_CONSUMER_SECRET'),
os.environ.get('TWITTER_ACCESS_TOKEN_KEY'),
os.environ.get('TWITTER_ACCESS_TOKEN_SECRET'))
# Get the GitHub Gist that contains our state database
gh = github.Github(os.environ.get('GIST_TOKEN'))
gist = gh.get_gist(os.environ.get('STATE_DB_GIST'))
stateDb = json.loads(gist.files['state.json'].content)
print(f" : Loaded state DB with {len(stateDb)} entries")
# Get the memories DB
req = requests.get(os.environ.get('MEMORY_DB_URL'))
memories = req.json()
print(f" : Loaded memories DB with {len(memories)} memories")
# Randomly select memories from the database, comparing them to the state database
# to make sure they haven't already been posted. If they have then pick a new one.
print(" : Choosing a random memory")
while True:
# Find our random memory to post from the DB
chosenMemory = random.choice(memories)
print(f" : Checking memory '{chosenMemory['title']}'")
if chosenMemory['title'] in stateDb:
print(f" : WARNING: Memory {chosenMemory['title']} already posted; choosing new one...")
continue
else:
break
print(" : Memory Chosen")
print("==================================================================")
pprint(chosenMemory)
print("==================================================================")
# Download the memory image
response = requests.get(chosenMemory['source'], stream=True)
with open('img.jpg', 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
# Assemble the tweet text
tweet = f"{chosenMemory['caption_twitter']} {chosenMemory['flickr_url']}\n\nPhoto By {chosenMemory['photographer_twitter']}"
print(" : Preview of tweet to be posted")
print("==================================================================")
print(tweet)
print("==================================================================")
# Post to Twitter!
if "DRY_RUN" in os.environ:
print(" : Dry Run, exiting without posting to twitter")
else:
# STEP 1 - upload image
file = open('img.jpg', 'rb')
data = file.read()
r = api.request('media/upload', None, {'media': data})
if r.status_code == 200:
print(' : SUCCESS: Photo upload to twitter')
else:
raise SystemExit(f" : FAILURE: Photo upload to twitter: {r.text}")
# STEP 2 - post tweet with a reference to uploaded image
if r.status_code == 200:
media_id = r.json()['media_id']
r = api.request('statuses/update', {'status': tweet, 'media_ids': media_id})
if r.status_code == 200:
twitterPostData = json.loads(r.text)
print(' : SUCCESS: Tweet posted')
# Append to the state database
stateDb[chosenMemory['title']] = {"tweet_id":twitterPostData['id'], "posted_on":datetime.now().isoformat()}
gist.edit(files={"state.json": github.InputFileContent(content=json.dumps(stateDb, indent=2))})
print(" : State DB updated")
else:
raise SystemExit(f" : FAILURE: Tweet not posted: {r.text}")