forked from LindseyB/starwars-dot-gif
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter_bot.py
69 lines (55 loc) · 1.61 KB
/
twitter_bot.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
import base64
import json
import requests
import ConfigParser
import random
import os
import time
from twython import Twython
from base64 import b64encode
from makeGifs import makeGif
config = ConfigParser.ConfigParser()
config.read("config.cfg")
config.sections()
CLIENT_ID = config.get("imgur", "client_id")
API_KEY = config.get("imgur", "api_key")
APP_KEY = config.get("twitter", "app_key")
APP_SECRET = config.get("twitter", "app_secret")
OAUTH_TOKEN = config.get("twitter", "oauth_token")
OAUTH_TOKEN_SECRET = config.get("twitter", "oauth_token_secret")
headers = {"Authorization": "Client-ID " + CLIENT_ID}
url = "https://api.imgur.com/3/upload.json"
while True:
quote = makeGif(random.randint(4,6), 0, rand=True)
quote = ' '.join(quote)
# resize the gif in 90% increments, automatically trying to get the precent was not working. :(
while(os.path.getsize('star_wars.gif') > 2097152):
os.popen('convert star_wars.gif -resize 90% star_wars.gif')
try:
response = requests.post(
url,
headers = headers,
data = {
'key': API_KEY,
'image': b64encode(open('star_wars.gif', 'rb').read()),
'type': 'base64',
'name': 'star_wars.gif',
'title': 'Star Wars Dot Gif'
}
)
except requests.exceptions.ConnectionError:
# try again.
continue
try:
res_json = response.json()
link = res_json['data']['link']
except ValueError:
# try again.
continue
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
status = '"' + quote + '" ' + link + ' #starwarsgif'
print "tweeting..."
twitter.update_status(status=status)
print "sleeping..."
# sleep 1 hour
time.sleep(3600)