-
Notifications
You must be signed in to change notification settings - Fork 0
/
flickr.py
273 lines (183 loc) · 10.3 KB
/
flickr.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
from shutil import copyfile
import os
import flickrapi
import flickr_keys
import sqlite3
import webbrowser
class flickr:
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
def __init__(self):
import flickr_keys
#flickr
self.data = []
self.api_key=flickr_keys.api_key
self.api_secret=flickr_keys.api_secret
self.conf_user='24415554@N04'
self.DEBUG = False
def search_flickr(self,timestamp):
self.flickrLogin()
photo_data = self.flickr.photos.search(
api_key=self.api_key,
user_id=self.conf_user,
per_page=4,
min_taken_date=timestamp,
max_taken_date=timestamp
)
return int(photo_data['photos']['total'])
def getDescription(self,photoInfo):
from alphabet_detector import AlphabetDetector
ad = AlphabetDetector()
text=u''
descriptionString=photoInfo['photo']['description']['_content']
for line in descriptionString.splitlines():
if not 'CYRILLIC' in ad.detect_alphabet(line):
text+="\n"+line
return text
def flickrLogin(self):
#heroku+flickrapi hack
#flickrapi does not allow change of path to local file with access token. It store in /tmp/.flickr/oauth-tokens.sqlite. My script deployed in heroku, and tmp folder is removed after script end.
#so after login oauth-tokens.sqlite copying to script folder, and before login - copyng to temp folder.
tokens_tmp_folder = os.path.expanduser(os.path.join("~", ".flickr"))
tokens_filename = os.path.join(tokens_tmp_folder, 'oauth-tokens.sqlite')
tokens_tmp_path = tokens_filename
tokens_longstore_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),'oauth-tokens.sqlite')
if not os.path.exists(tokens_tmp_folder):
os.makedirs(tokens_tmp_folder)
if os.path.isfile(tokens_longstore_path):
copyfile(tokens_longstore_path,tokens_tmp_path)
#end of heroku+flickrapi hack, use flickrapi normally
self.flickr = flickrapi.FlickrAPI(self.api_key, self.api_secret, format='parsed-json')
# Only do this if we don't have a valid token already
if not self.flickr.token_valid(perms='read'):
# Get a request token
self.flickr.get_request_token(oauth_callback='oob')
# Open a browser at the authentication URL. Do this however
# you want, as long as the user visits that URL.
authorize_url = self.flickr.auth_url(perms=u'write')
webbrowser.open_new_tab(authorize_url)
print 'Authorize this app in browser: '+authorize_url
# Get the verifier code from the user. Do this however you
# want, as long as the user gives the application the code.
verifier = unicode(raw_input('Verifier code: '))
# Trade the request token for an access token
self.flickr.get_access_token(verifier)
#print('Step 2: use Flickr')
copyfile(tokens_tmp_path, tokens_longstore_path) #heroku+flickrapi hack
def loadSets(self,sqliteConn):
try:
collections = self.flickr.collections.getTree(collection_id = self.flickrCollection,)
except Exception as e:
print ('ERROR. Empty feeder '+self.flickrCollection)
return 0
else:
pass
sql='CREATE TABLE sets (id INTEGER PRIMARY KEY, title text, name text, setId text);'
sqliteConn.execute(sql)
for collection in collections['collections']['collection']:
if 'set' in collection:
currentCollection=collection['title']
print str(len(collection['set'])) + ' sets in collection '+ collection['title']
for set in collection['set']:
sql='INSERT INTO sets (title,name,setId) VALUES(?, ?, ?);'
sqliteConn.execute(sql,[collection['title'],set['title'],set['id']])
self.sqlite.commit()
for row in sqliteConn.execute('SELECT * FROM sets ORDER BY id'):
pass
def crawlPhotos(self,sqliteConn):
#my home windows install sort of broken, so i did not use spatial extensions
sql='CREATE TABLE setsphotos (id INTEGER PRIMARY KEY, set_id INTEGER, photo_id INTEGER, title TEXT, urlOriginal text, urlLarge text, lat text, lon text, description TEXT, url TEXT, equirectangular Boolean);'
sqliteConn.execute(sql)
sql='CREATE TABLE tags (id INTEGER PRIMARY KEY, photo_id INTEGER, tag TEXT, machine_tag BOOLEAN);'
sqliteConn.execute(sql)
for row in sqliteConn.execute('SELECT * FROM sets ORDER BY id'):
print 'crawl photos in set ' + str(row[2].encode('utf-8'))
feeder_photoset_id=row[3]
try:
photos = self.flickr.photosets.getPhotos(photoset_id = feeder_photoset_id,user_id=self.conf_user)
except Exception as e:
print ('ERROR. Cannot get photo in set '+feeder_photoset_id)
return 0
for photo in photos['photoset']['photo']:
dbPhoto=dict()
photo_id=photo['id']
dbPhoto['id']=photo['id']
dbPhoto['title']=photo['title']
photo_data = self.flickr.photos.getSizes(photo_id=photo_id)
for d in photo_data['sizes']['size']:
if d['label']=='Original':
dbPhoto['urlSource']=d['source']
if d['label']=='Large':
dbPhoto['urlLarge']=d['source']
if ('urlLarge' not in dbPhoto):
dbPhoto['urlLarge'] = dbPhoto['urlSource']
photoInfo=self.flickr.photos.getInfo(photo_id=photo_id)
location = photoInfo['photo'].get('location',dict())
dbPhoto['lat']=str(location.get('latitude',''))
dbPhoto['lon']=str(location.get('longitude',''))
dbPhoto['description']=photoInfo['photo']['description']['_content']
dbPhoto['url']=photoInfo['photo']['urls']['url'][0]['_content']
#pp.pprint(photoInfo)
#quit()
equirectangular=0
for tag in photoInfo['photo']['tags']['tag']:
sql='INSERT INTO tags (photo_id, tag, machine_tag) VALUES(?, ?, ?);'
sqliteConn.execute(sql,[dbPhoto['id'], tag['_content'], tag['machine_tag'] ])
self.sqlite.commit()
if (tag['_content']=='equirectangular'):
equirectangular=1
try:
sql='INSERT INTO setsphotos (set_id , photo_id , title , urlOriginal , urlLarge , lat, lon , description, url, equirectangular) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?);'
sqliteConn.execute(sql,[feeder_photoset_id, dbPhoto['id'], dbPhoto['title'], dbPhoto['urlSource'], dbPhoto['urlLarge'], dbPhoto['lat'], dbPhoto['lon'], dbPhoto['description'], dbPhoto['url'],equirectangular ])
self.sqlite.commit()
print dbPhoto['title']
except Exception as e:
print ('ERROR. photo='+str(dbPhoto['id']) , sys.exc_info())
return 0
def render(self,sqliteConn):
websitefolder='website'
for row in sqliteConn.execute('SELECT * FROM sets ORDER BY id'):
#print row[3]
#feeder_photoset_id=row[3]
#print cur.fetchone()["name"]
import pprint
pp = pprint.PrettyPrinter()
#pp.pprint(row)
pagename=row[2]
if not os.path.exists(os.path.join(websitefolder,'content','post')):
os.makedirs(os.path.join(websitefolder,'content','post'))
filename = os.path.join(websitefolder,'content','post',pagename.replace(' ','_')+'.md')
print 'write page to '+filename
f = open(filename,'w')
f.write('+++\n')
f.write('title = "'+row[2]+'"\n')
f.write('date = "1970-01-01"\n')
f.write('tags = [ ]"\n')
f.write('categories = [ ]"\n')
f.write('+++\n')
print 'SELECT * FROM setsphotos WHERE set_id ="'+row[3]+'" ORDER BY id '
for rowPhoto in sqliteConn.execute('SELECT * FROM setsphotos WHERE set_id ="'+row[3]+'" ORDER BY id '):
f.write(u'<a data-flickr-embed="true" data-footer="true" href="{url}" title="HARDCODED"><img src="{src}" alt="{alt}"></a><script async src="http://embedr.flickr.com/assets/client-code.js" charset="utf-8"></script>'.format(url=rowPhoto[9],src=rowPhoto[5],alt=rowPhoto[3]) )
f.write("</br> \n")
f.close()
def dropdb(self):
if os.path.exists(self.dbPath):
os.remove(self.dbPath)
def procedure(self):
#self.dropdb()
self.sqlite=sqlite3.connect(self.dbPath)
self.row_factory = self.dict_factory
sqliteConn = self.sqlite.cursor()
print "Database created and opened succesfully"
self.flickrLogin(sqliteConn)
self.loadSets(sqliteConn)
self.crawlPhotos(sqliteConn)
self.render(sqliteConn)
sqliteConn.close()
quit()