-
Notifications
You must be signed in to change notification settings - Fork 1
/
fkr.py
84 lines (71 loc) · 2.75 KB
/
fkr.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
#!/usr/bin/env python
#-*- encoding:utf-8 -*-
##ref https://secure.flickr.com/services/api/misc.urls.html
##ref https://secure.flickr.com/services/api/flickr.photos.search.html
##ref https://secure.flickr.com/services/api/explore/flickr.photos.search
import json
import os
import time
import requests
import urllib
from PIL import Image
from StringIO import StringIO
from requests.exceptions import ConnectionError
def get_img(query, path, img_num):
"""Download full size images from Google image search.
Don't print or republish images without permission.
I used this to train a learning algorithm.
"""
api_key = 'ba12e9d62ea20cf4a6c6aab4baa0c2f1'
#if meet api_key Error please visit https://secure.flickr.com/services/api/explore/flickr.photos.search and push the "Call method" button ,and then search api_key in the reply page
BASE_URL = 'https://secure.flickr.com/services/rest/?method=flickr.photos.search&format=json&api_key=' + api_key + '&text='+ query.replace(' ','+') +'&per_page=' + str(img_num)
BASE_PATH = os.path.join(path, query)
if not os.path.exists(BASE_PATH):
os.makedirs(BASE_PATH)
r = requests.get(BASE_URL)
title=1
for image_info in json.loads(r.text[14:-1])['photos']['photo']:
file = os.path.join(BASE_PATH, '%s.jpg') % str(title)
url = 'http://farm' + str(image_info['farm']) + '.staticflickr.com/' + str(image_info['server']) + '/' + image_info['id'] + '_' + image_info['secret'] + '_b.jpg'
try:
print 'Fetching %s ' % url
urllib.urlretrieve(url, file)
# os.system('aria2c "' + url + '" -o ' + file)
# os.system('wget "' + url + '" -O ' + file)
print 'save as %s' % file
except ConnectionError, e:
print 'could not download %s' % url
continue
# else :
# print 'download %s is uncomplete' % url
# continue
title += 1
#get_img('query', 'Directory',30)
#The URL takes the following format:
#
#http://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}.jpg
# or
#http://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}_[mstzb].jpg
# or
#http://farm{farm-id}.staticflickr.com/{server-id}/{id}_{o-secret}_o.(jpg|gif|png)
#
#The letter suffixes are as follows:
#s small square 75x75
#q large square 150x150
#t thumbnail, 100 on longest side
#m small, 240 on longest side
#n small, 320 on longest side
#- medium, 500 on longest side
#z medium 640, 640 on longest side
#c medium 800, 800 on longest side†
#b large, 1024 on longest side*
#o original image, either a jpg, gif or png, depending on source format
#Example
#
#http://farm1.staticflickr.com/2/1418878_1e92283336_m.jpg
#
#farm-id: 1
#server-id: 2
#photo-id: 1418878
#secret: 1e92283336
#size: m