forked from info-beamer/package-flickr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service
executable file
·115 lines (99 loc) · 3.15 KB
/
service
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
#!/usr/bin/python
import sys, os
import random
import time
import json
import requests
from hosted import CONFIG
CONFIG.restart_on_update()
FLICKR = CONFIG['flickr']
MAX_PHOTOS = 40
def cleanup():
for name in os.listdir("."):
if name.startswith("photo-"):
os.unlink(name)
def log(msg):
print >>sys.stderr, msg
def get_photos(group_id, page, per_page=20):
photos = requests.get("https://api.flickr.com/services/rest/", params = dict(
api_key = FLICKR['api_key'],
method = "flickr.groups.pools.getPhotos",
format = "json",
nojsoncallback = 1,
group_id = group_id,
per_page = per_page,
page = page,
)).json()['photos']
def transform(photo):
photo['urls'] = [
"https://farm%s.staticflickr.com/%s/%s_%s%s.jpg" % (
photo['farm'], photo['server'], photo['id'], photo['secret'], suffix
) for suffix in ('_h', '_b', '_z')
]
return photo
pages = photos['pages']
photos = [
transform(photo)
for photo in
photos['photo']
]
return pages, photos
def add_photo(info, photo_data):
photos = sorted(name for name in os.listdir(".") if name.startswith("photo-") and name.endswith(".jpg"))
while len(photos) >= MAX_PHOTOS:
try:
os.unlink(photos[0])
os.unlink(photos[0].replace(".jpg", ".json"))
photos.pop(0)
except:
pass
basename = "photo-%012d-%s" % (time.time(), info['id'])
info['stored'] = time.time()
info['jpg'] = "%s.jpg" % basename
with file("%s.json" % basename, "wb") as out:
json.dump(info, out)
with file("%s.jpg" % basename, "wb") as out:
out.write(photo_data)
def get_user_info(user_id):
person = requests.get("https://api.flickr.com/services/rest/", params = dict(
api_key = FLICKR['api_key'],
method = "flickr.people.getInfo",
format = "json",
nojsoncallback = 1,
user_id = user_id,
)).json()['person']
if 'realname' in person:
name = person['realname']['_content']
elif 'username' in person:
name = person['username']['_content']
else:
name = "?"
return dict(
profile = person['profileurl']['_content'],
name = name,
)
def main():
cleanup()
pages = 1
while 1:
page = random.randint(1, max(1, pages-1))
pages, photos = get_photos(FLICKR['group'], page, per_page=10)
log("%d pages available" % pages)
for photo in photos:
for url in photo['urls']:
log("trying to fetch %s" % url)
r = requests.get(url, allow_redirects=False)
log("status code is %d" % r.status_code)
if r.status_code == 200:
break
time.sleep(1)
else:
log("couldn't find a usable image")
continue
photo['user_info'] = get_user_info(photo['owner'])
add_photo(photo, r.content)
time.sleep(3)
log("fetch cycle complete. waiting")
time.sleep(120)
if __name__ == "__main__":
main()