2018-10-15 14:16:10 +01:00
|
|
|
import os
|
|
|
|
import glob
|
|
|
|
import flickr_api
|
|
|
|
from bleach import clean
|
|
|
|
import arrow
|
|
|
|
import keys
|
|
|
|
import common
|
2018-11-03 09:49:45 +00:00
|
|
|
from common import cached_property
|
2018-10-15 14:16:10 +01:00
|
|
|
import settings
|
|
|
|
from pprint import pprint
|
|
|
|
import logging
|
|
|
|
|
2018-10-15 21:57:45 +01:00
|
|
|
#class FlickrFollows(common.Follows):
|
|
|
|
|
|
|
|
|
2018-10-15 14:16:10 +01:00
|
|
|
class FlickrFavs(common.Favs):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__('flickr')
|
|
|
|
flickr_api.set_keys(
|
|
|
|
api_key = keys.flickr.get('key'),
|
|
|
|
api_secret = keys.flickr.get('secret')
|
|
|
|
)
|
|
|
|
self.user = flickr_api.Person.findByUserName(
|
|
|
|
keys.flickr.get('username')
|
|
|
|
)
|
|
|
|
|
2018-10-15 21:57:45 +01:00
|
|
|
@property
|
|
|
|
def feeds(self):
|
|
|
|
logging.info('Generating OPML feeds for Flickr')
|
|
|
|
feeds = []
|
|
|
|
pages = 1
|
|
|
|
page = 1
|
|
|
|
while page <= pages:
|
|
|
|
fetched = self.user.getPublicContacts(
|
|
|
|
page=page
|
|
|
|
)
|
|
|
|
for u in fetched:
|
|
|
|
feeds.append({
|
|
|
|
'text': u.username,
|
|
|
|
'xmlUrl': "https://api.flickr.com/services/feeds/photos_public.gne?lang=en-us&format=rss_200&id=%s" % u.id,
|
|
|
|
'htmlUrl': "https://www.flickr.com/photos/%s" % u.id
|
|
|
|
})
|
|
|
|
pages = fetched.info.pages
|
|
|
|
page = page + 1
|
|
|
|
return feeds
|
|
|
|
|
2018-10-15 14:16:10 +01:00
|
|
|
def run(self):
|
|
|
|
pages = 1
|
|
|
|
page = 1
|
|
|
|
while page <= pages:
|
2018-10-15 21:57:45 +01:00
|
|
|
logging.info('fetching for Flickr: page %d' % page)
|
2018-10-15 14:16:10 +01:00
|
|
|
fetched = self.user.getFavorites(
|
|
|
|
user_id=self.user.id,
|
2018-10-16 08:59:52 +01:00
|
|
|
page=page,
|
|
|
|
min_fave_date=self.since
|
2018-10-15 14:16:10 +01:00
|
|
|
)
|
|
|
|
for p in fetched:
|
|
|
|
photo = FlickrFav(p)
|
|
|
|
photo.run()
|
|
|
|
pages = fetched.info.pages
|
|
|
|
page = page + 1
|
|
|
|
|
|
|
|
|
|
|
|
class FlickrFav(common.ImgFav):
|
|
|
|
def __init__(self, flickrphoto):
|
|
|
|
self.flickrphoto = flickrphoto
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "fav-of %s" % (self.url)
|
|
|
|
|
2018-11-03 09:49:45 +00:00
|
|
|
@cached_property
|
2018-10-16 08:59:52 +01:00
|
|
|
def owner(self):
|
|
|
|
return self.info.get('owner')
|
|
|
|
|
2018-11-03 09:49:45 +00:00
|
|
|
@cached_property
|
2018-10-16 08:59:52 +01:00
|
|
|
def info(self):
|
2018-11-03 09:49:45 +00:00
|
|
|
return self.flickrphoto.getInfo()
|
2018-10-16 08:59:52 +01:00
|
|
|
|
2018-10-15 14:16:10 +01:00
|
|
|
@property
|
|
|
|
def author(self):
|
|
|
|
return {
|
|
|
|
'name': "%s" % self.owner.username,
|
|
|
|
'url': "%s" % self.owner.getProfileUrl(),
|
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def id(self):
|
|
|
|
return "%s" % self.info.get('id')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def url(self):
|
|
|
|
return "https://www.flickr.com/photos/%s/%s/" % (
|
|
|
|
self.owner.id,
|
|
|
|
self.id
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def content(self):
|
|
|
|
return "%s" % self.info.get('description')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def geo(self):
|
|
|
|
if 'location' not in self.info:
|
|
|
|
return None
|
|
|
|
|
|
|
|
lat = self.info.get('location').get('latitude', None)
|
|
|
|
lon = self.info.get('location').get('longitude', None)
|
|
|
|
return (lat, lon)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def title(self):
|
|
|
|
return clean(''.strip("%s" % self.info.get('title')))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def targetprefix(self):
|
|
|
|
return os.path.join(
|
|
|
|
settings.paths.get('archive'),
|
|
|
|
'favorite',
|
|
|
|
"flickr_%s_%s" % (
|
|
|
|
common.slugfname('%s' % self.owner.id),
|
|
|
|
self.id,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def exists(self):
|
|
|
|
maybe = glob.glob("%s*" % self.targetprefix)
|
|
|
|
if len(maybe):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def published(self):
|
|
|
|
return arrow.get(self.info.get('dateuploaded'))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def tags(self):
|
|
|
|
tags = []
|
|
|
|
for t in self.info.get('tags'):
|
|
|
|
tags.append("%s" % t.text)
|
|
|
|
return tags
|
|
|
|
|
|
|
|
@property
|
|
|
|
def images(self):
|
|
|
|
sizes = self.flickrphoto.getSizes()
|
|
|
|
for maybe in ['Original', 'Large 2048', 'Large 1600', 'Large']:
|
|
|
|
if maybe in sizes:
|
|
|
|
f = "%s%s" % (self.targetprefix, common.TMPFEXT)
|
|
|
|
return {
|
|
|
|
f: sizes.get(maybe).get('source')
|
|
|
|
}
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
if not self.exists:
|
|
|
|
self.fetch_images()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
t = FlickrFavs()
|
|
|
|
t.run()
|