-
Notifications
You must be signed in to change notification settings - Fork 0
/
flickrlist.py
294 lines (236 loc) · 10.5 KB
/
flickrlist.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/python
# -- coding: utf-8 --
import sys
import time
import os
import urllib
import flickrapi
import auth
import webapp2
import logging
from time import mktime
from datetime import datetime, date, timedelta
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp import util
from google.appengine.ext import webapp
from urllib2 import urlopen, URLError
from flickrapi import FlickrAPI, FlickrError
from flickrapi.tokencache import TokenCache
from google.appengine.ext import db
from config import *
from weibo import APIClient
#from gaesessions import get_current_session
from userinfo import UserInfo
from flickrentity import FlickrRecord
from imageentity import ImageEntity
from google.appengine.api import urlfetch
from weibopy.auth import OAuthHandler
from weibopy.api import API
from weibopy.error import WeibopError
try:
import json
except ImportError:
from django.utils import simplejson as json
if sys.getdefaultencoding() != 'utf8':
reload(sys)
sys.setdefaultencoding('utf8')
_DEBUG=True
class FlickrListHandler(webapp2.RequestHandler):
def Upload(self, weibo, status, source=None, lat=None, long=None, url=None, image=None):
ext = ""
filename = ""
n = url.rfind('.')
if n != (-1):
ext = url[n:].lower()
n = url.rfind('/')
if n != (-1):
filename = url[n+1:].lower()
if not V2:
weibo.upload(str(filename), status, source=source, lat=lat, long=long, image=image)
#weibo.update_status(status, lat=lat, long=lat)
else:
if ext == ".jpg":
weibo.upload.statuses__upload(status=status, lat=lat, long=long, url=url, pic_jpg=image)
elif ext == ".gif":
weibo.upload.statuses__upload(status=status, lat=lat, long=long, url=url, pic_gif=image)
elif ext == ".png":
weibo.upload.statuses__upload(status=status, lat=lat, long=long, url=url, pic_png=image)
def PostWeibo(self, weibo, entity, imageentity, handleExcept=False):
logging.info( "posting %s "% entity.photo_id)
self.response.out.write( "posting %s <br/>"% entity.photo_id)
n = imageentity.url.rfind('.')
if n != (-1):
ext = imageentity.url[n:].lower()
title = (u'我上传了一些照片:%s %s' % (entity.title, entity.link))
try:
self.Upload(weibo, title, source = WEIBO_APP_KEY,lat=entity.latitude, long=entity.longitude, url=imageentity.url, image=imageentity.image)
#weibo.post.statuses__upload_url_text(status=title, lat=entity.latitude, long=entity.longitude, url=imageentity.url)
#elif
#weibo.post.statuses__update(status=title, lat=entity.latitude, long=entity.longitude)
return True
except Exception as e:
logging.info( "post exception: %s "% (e))
self.response.out.write("post exception: %s "% (e))
#weibo = auth.WeiboReauth(weibo)
#if not handleExcept:
# self.PostWeibo(weibo, entity, imageentity, handleExcept=False)
return False
def ClearImage(self):
q = db.GqlQuery("SELECT * from ImageEntity")
db.delete(q.fetch(200))
def GetFlickrImages(self, flickr, userinfo):
if userinfo.flickr_browse_type == "set":
return self.GetFlickrImagesFromPhotoSet(flickr, userinfo)
elif userinfo.flickr_browse_type == "tag":
return self.GetFlickrImagesFromPhotoTag(flickr, userinfo, userinfo.flickr_browse_typename)
else:
return self.GetFlickrImagesFromPhotoStream(flickr, userinfo)
def GetFlickrImagesFromPhotoStream(self, flickr, userinfo):
list = self.GetFlickrImagesFromPhotoTag(flickr, userinfo, tags = None)
return list
def GetFlickrImagesFromPhotoSet(self, flickr, userinfo):
list = []
sets = flickr.photosets_getList(user_id=userinfo.flickr_id)
#这里每轮最多10张
for photo in flickr.walk_set(photoset_id=userinfo.flickr_browse_setid, per_page='10'):
photo_id = photo.attrib['id']
title = photo.attrib['title']
farm = photo.attrib['farm']
server = photo.attrib['server']
secret = photo.attrib['secret']
logging.info("get %s"% photo_id)
self.response.out.write("get %s<br/>"% photo_id)
photo = FlickrRecord.gql('WHERE photo_id=:1', photo_id).get()
#photo = None
if photo:
logging.info("photo exist ")
self.response.out.write("photo exist <br/>")
continue
url = "http://farm%s.staticflickr.com/%s/%s_%s.jpg"%(farm, server, photo_id, secret)
link = "http://www.flickr.com/photos/%s/%s" % (userinfo.flickr_id, photo_id)
e = FlickrRecord()
e.photo_id = photo_id
e.title = title
e.url = url
e.link = link
list.append(e)
return list
def GetFlickrImagesFromPhotoTag(self, flickr, userinfo, tags):
list = []
#url = "http://api.flickr.com/services/feeds/photos_public.gne?id="+flickr_id+"&lang=en-us&format=rss_200"
url = "http://api.flickr.com/services/feeds/photos_public.gne?id="+userinfo.flickr_id+"&lang=en-us&format=json"
if tags :
url = url +"&tags="+tags
#proxies = {'http': 'http://127.0.0.1:8580'}
proxies={}
feedinput = urllib.urlopen(url, proxies=proxies).read()
p1 = feedinput.find("[")
p2 = feedinput.rfind("]")
feedinput1 = feedinput[p1: p2+1]
entries = json.loads(feedinput1)
for entry in entries:
title = entry["title"]
url = entry["media"]['m']
link = entry["link"]
pos1 = link.rfind("/")
pos2 = link.rfind("/",0,pos1-1)
photo_id = link[pos2+1:pos1]
photo_id = photo_id.strip()
sd = entry["date_taken"]
if sd[-6] == '-':
sd = sd[0:-6]
format = "%Y-%m-%dT%H:%M:%S"
d = time.strptime(sd, format)
date_taken = datetime.fromtimestamp(mktime(d))
current_time = datetime.now()
delta = current_time - date_taken
if delta> timedelta(days=userinfo.flickr_max_days):
logging.info("%s: time exceed "%photo_id)
self.response.out.write("time exceed exist %s <br/>"%photo_id)
continue
logging.info("%s"% photo_id)
self.response.out.write("%s<br/>"% photo_id)
photo = FlickrRecord.gql('WHERE photo_id=:1', photo_id).get()
#photo = None
if photo:
logging.info("photo exist")
self.response.out.write("photo exist <br/>")
continue
e = FlickrRecord()
e.photo_id = photo_id
e.title = title
e.url = url
e.link = link
e.date = date_taken
list.append(e)
return list
def get(self):
#session = get_current_session()
#nickname = session.get("nickname", 0)
#if not nickname:
# logging.info("not logged in")
# return
#这里必须有nickname,否则不知道是谁
nickname = self.request.get('nickname')
userinfo = UserInfo.gql("WHERE nickname=:1", nickname).get()
if not userinfo:
logging.info("no userinfo found: %s",nickname)
self.response.out.write("no userinfo found<br/>")
return
#此处不用认证,否则会加入很多未公开的图片的"
flickr = auth.FlickrAuth(userinfo, no_token=True)
if not flickr:
logging.info("flickr authentication failed")
self.response.out.write("flickr authentication failed")
return
weibo = auth.WeiboAuth(userinfo)
if not weibo:
logging.info("weibo authentication failed")
self.response.out.write("weibo authentication failed")
return
#获得相片
self.ClearImage()
list = self.GetFlickrImages(flickr, userinfo)
#获得位置需要认证
flickr = auth.FlickrAuth(userinfo, no_token=False)
if not flickr:
logging.info("flickr authentication failed")
self.response.out.write("flickr authentication failed")
return
for e in list:
location = None
try:
data = flickr.flickr_call(method='flickr.photos.geo.getLocation', photo_id=e.photo_id, format='rest')
l = flickr.parse_etree(data)
location = l.find('photo/location')
except FlickrError:
logging.info( "%s has no location info."% (e.photo_id))
self.response.out.write( "%s has no location info.<br/>"% (e.photo_id))
if e.url[-6:] == "_m.jpg":
e.url = e.url[:-5] + "b.jpg"
url1 = "http://images0-focus-opensocial.googleusercontent.com/gadgets/proxy?container=focus&gadget=a&resize_h=500&url="+e.url
else:
url1 = e.url
content = (urlfetch.fetch(url1)).content
ie = ImageEntity()
ie.url = e.url
ie.url1 = url1
ie.image = db.Blob(content)
if location is not None:
e.latitude = location.attrib['latitude']
e.longitude = location.attrib['longitude']
e.accuracy = location.attrib['accuracy']
if self.PostWeibo(weibo, e, ie) :
e.put()
ie.put()
logging.info("sync ok")
self.response.out.write("sync ok<br/>")
application = webapp2.WSGIApplication([
('/sync', FlickrListHandler)
],
debug=True)
def main():
#util.run_wsgi_app(application)
run_wsgi_app(application)
if __name__ == '__main__':
main()