-
Notifications
You must be signed in to change notification settings - Fork 1
/
archillect.py
executable file
·87 lines (80 loc) · 3.25 KB
/
archillect.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
#!/usr/bin/env python2
import sys
import os
import errno
import requests
working_dir = os.path.dirname(os.path.realpath(__file__))
ARCHILLECT_URL = 'http://archillect.com/api/'
DEFAULT_FOLDER = os.path.join(working_dir, 'img')
DEFAULT_IMG_PREFIX = 'archillect'
ALLOWED_FORMATS = ['jpg']
LAST_NUM_FILE = os.path.join(working_dir, 'last')
DEFAULT_LINK = os.path.join(working_dir, 'archillect.jpg')
MAX_RETRIES = 30
def main():
if len(sys.argv) > 3 or '--help' in sys.argv or '-h' in sys.argv:
print 'Usage: {} [-h | --help] [-v | --verbose]'.format(sys.argv[0])
exit(1)
if not os.path.isfile(LAST_NUM_FILE):
with open(LAST_NUM_FILE, 'w') as last_file:
last_file.write('0')
with open(LAST_NUM_FILE, 'r+') as last_file:
num = int(last_file.read())
retries = MAX_RETRIES
while retries > 0:
try:
num = num + 1
retries = retries - 1
api_url = '{}{}'.format(ARCHILLECT_URL, num)
log('calling API: {}'.format(api_url))
api_req = requests.get(api_url)
if not api_req.ok:
log('API call failed: {} {}'.format(api_req.url, api_req.reason))
continue
api_req_json = api_req.json()
if 'error' in api_req_json:
log('API call failed: {} {}'.format(api_req.url, api_req_json['error']))
continue
if 'post' not in api_req_json \
or 'big' not in api_req_json["post"] \
or not isinstance(api_req_json['post']['big'], basestring):
log('API response invalid')
continue
img_url = api_req_json["post"]["big"]
extension = img_url.split('.')[-1]
if extension not in ALLOWED_FORMATS:
log('Format not allowed: {}'.format(extension))
continue
log("Loading imgage: {}".format(img_url))
img_req = requests.get(img_url)
if not img_req.ok:
log('Image request failed: {} {}'.format(img_req.url, img_req.reason))
continue
img_name = '{}.{}.{}'.format(DEFAULT_IMG_PREFIX, num, extension)
out_path = os.path.join(DEFAULT_FOLDER, img_name)
if not os.path.exists(DEFAULT_FOLDER):
os.makedirs(DEFAULT_FOLDER)
with open(out_path, 'wb') as out_file:
out_file.write(img_req.content)
symlink_force(out_path, DEFAULT_LINK)
last_file.seek(0)
last_file.write("{}".format(num))
last_file.truncate()
exit(0) # FINISHED
except requests.ConnectionError:
log('failed connection, retry next')
exit(1) #max retries exceeded
def symlink_force(target, link_name):
try:
os.symlink(target, link_name)
except OSError, e:
if e.errno == errno.EEXIST:
os.remove(link_name)
os.symlink(target, link_name)
else:
raise e
def log(msg):
if '-v' in sys.argv or '--verbose' in sys.argv:
print msg
if __name__ == '__main__':
main()