box-o-sand/flickrscripts/timewarp.py

96 lines
3.1 KiB
Python
Raw Normal View History

import argparse
import json
2012-02-06 03:49:57 +00:00
import logging
import sys
2012-02-06 03:49:57 +00:00
from datetime import datetime, timedelta
from os.path import expanduser
from flickrscripts.common import setup_flickr
class TimeWarper(object):
2012-02-06 03:49:57 +00:00
flickr = None
_base_query_args = dict(user_id='me', page=1)
2012-02-06 03:49:57 +00:00
def __init__(self, api_key, api_secret):
self.flickr = setup_flickr(api_key, api_secret)
2012-02-06 03:49:57 +00:00
self.log = logging.getLogger(self.__class__.__name__)
self.log.level = logging.INFO
2012-02-06 03:49:57 +00:00
def run(self, set_to_date, **query_args):
self.log.info(
'Setting upload dates for photos matching query %r', query_args
)
2012-02-06 03:49:57 +00:00
query = self._base_query_args.copy()
query.update(query_args)
2012-02-06 03:49:57 +00:00
for i, page in self._get_pages(query):
self.log.info('Processing page %s, attributes=%r',
i, page.attrib)
self._set_upload_date_for_photos(i, page, set_to_date)
2012-02-06 03:49:57 +00:00
return 0
2012-02-06 03:49:57 +00:00
def _set_upload_date_for_photos(self, pagenum, page, set_to_date):
for i, photo in enumerate(page.findall('photos/photo')):
setdate_response = self.flickr.photos_setDates(
photo_id=photo.attrib['id'],
2012-02-06 03:49:57 +00:00
date_posted=set_to_date
)
2012-02-06 03:49:57 +00:00
self.log.info('page %s photo %s: response attribs=%s',
pagenum, i, setdate_response.attrib)
def _get_pages(self, query):
self.log.info('Getting pages for query %r', query)
i = 1
while True:
page = self.flickr.photos_search(**query)
if len(page.findall('photos/photo')):
2012-02-06 03:49:57 +00:00
yield i, page
i += 1
else:
raise StopIteration
def main(sysargs=sys.argv[:]):
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
prog='flickrscripts timewarp')
2012-02-06 03:49:57 +00:00
parser.add_argument('set_to_date')
parser.add_argument('-q', '--search-query-param', default=[],
action='append', help='Add a key=value param to the search query list')
parser.add_argument(
'-c', '--rcfile', default=expanduser('~/.flickrscripts.json'),
help='JSON file containing `APIKEY` and `APISECRET` keys',
type=argparse.FileType('r')
)
args = parser.parse_args(sysargs[1:])
rc_conf = json.load(args.rcfile)
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
log = logging.getLogger('main')
log.level = logging.INFO
log.info('Preparing query args')
query_args = {}
for key_val in args.search_query_param:
key, value = key_val.split('=', 1)
query_args[key] = value
if len(query_args.keys()) == 0:
query_args = dict(
2012-02-06 03:49:57 +00:00
min_upload_date=(datetime.now() + timedelta(days=-1)),
max_upload_date=(datetime.now() + timedelta(days=1))
)
2012-02-06 03:49:57 +00:00
log.warn('No query arguments provided! ' +
'Defaulting to %r', query_args)
2012-02-06 03:49:57 +00:00
warper = TimeWarper(rc_conf['APIKEY'], rc_conf['APISECRET'])
return warper.run(args.set_to_date, **query_args)
if __name__ == '__main__':
2012-02-06 03:49:57 +00:00
sys.exit(main())