Now with less embarrassment!
This commit is contained in:
parent
64a22993bc
commit
ab492e70d0
11
flickrscripts.py
Normal file
11
flickrscripts.py
Normal file
@ -0,0 +1,11 @@
|
||||
import flickrapi
|
||||
|
||||
|
||||
def setup_flickr(self, api_key, api_secret):
|
||||
self.flickr = flickrapi.FlickrAPI(api_key, api_secret)
|
||||
|
||||
token, frob = self.flickr.get_token_part_one(perms='write')
|
||||
if not token:
|
||||
raw_input("Press ENTER after you authorized this program")
|
||||
|
||||
self.flickr.get_token_part_two((token, frob))
|
138
timewarp.py
138
timewarp.py
@ -1,78 +1,94 @@
|
||||
from __future__ import print_function
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import logging
|
||||
import sys
|
||||
from datetime import datetime, timedelta
|
||||
from os.path import expanduser
|
||||
|
||||
import flickrapi
|
||||
from flickrscripts import setup_flickr
|
||||
|
||||
|
||||
class TimeWarper(object):
|
||||
flickr = None
|
||||
_base_query_args = dict(user_id='me', page=1)
|
||||
|
||||
def __init__(self, sysargs=sys.argv[:]):
|
||||
self.sysargs = sysargs
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('min_upload_date')
|
||||
parser.add_argument('max_upload_date')
|
||||
parser.add_argument('set_to_date')
|
||||
def __init__(self, api_key, api_secret):
|
||||
setup_flickr(self, api_key, api_secret)
|
||||
self.log = logging.getLogger(self.__class__.__name__)
|
||||
self.log.level = logging.INFO
|
||||
|
||||
parser.add_argument(
|
||||
'-c', '--rcfile', default=expanduser('~/.frupplerc'),
|
||||
type=argparse.FileType('r')
|
||||
def run(self, set_to_date, **query_args):
|
||||
self.log.info(
|
||||
'Setting upload dates for photos matching query %r', query_args
|
||||
)
|
||||
query = self._base_query_args.copy()
|
||||
query.update(query_args)
|
||||
|
||||
self.args = parser.parse_args(self.sysargs[1:])
|
||||
self._rc_conf = json.load(self.args.rcfile)
|
||||
|
||||
self.flickr = flickrapi.FlickrAPI(
|
||||
self._rc_conf['APIKEY'], self._rc_conf['APISECRET']
|
||||
)
|
||||
|
||||
token, frob = self.flickr.get_token_part_one(perms='write')
|
||||
if not token:
|
||||
raw_input("Press ENTER after you authorized this program")
|
||||
|
||||
self.flickr.get_token_part_two((token, frob))
|
||||
|
||||
def set_upload_date_for_photos(self, pagenum, photo_nodes, to_date):
|
||||
for i, photo in enumerate(photo_nodes):
|
||||
setdate_response = self.flickr.photos_setDates(
|
||||
photo_id=photo.attrib['id'],
|
||||
date_posted=to_date
|
||||
)
|
||||
print('page {} photo {}: rsp={}, attribs={}'.format(
|
||||
pagenum, i, setdate_response, setdate_response.attrib))
|
||||
|
||||
def run(self):
|
||||
query_args = dict(
|
||||
user_id='me',
|
||||
min_upload_date=self.args.min_upload_date,
|
||||
max_upload_date=self.args.max_upload_date,
|
||||
page=1
|
||||
)
|
||||
|
||||
print('Getting first page')
|
||||
firstpage = self.flickr.photos_search(**query_args)
|
||||
self.set_upload_date_for_photos(
|
||||
1,
|
||||
firstpage.getchildren()[0].getchildren(),
|
||||
self.args.set_to_date
|
||||
)
|
||||
|
||||
npages = int(firstpage.getchildren()[0].attrib['pages'])
|
||||
|
||||
for pagenum in range(1, npages + 1):
|
||||
print('Getting another page')
|
||||
page = self.flickr.photos_search(**query_args)
|
||||
self.set_upload_date_for_photos(
|
||||
pagenum,
|
||||
page.getchildren()[0].getchildren(),
|
||||
self.args.set_to_date
|
||||
)
|
||||
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)
|
||||
|
||||
return 0
|
||||
|
||||
def _set_upload_date_for_photos(self, pagenum, page, set_to_date):
|
||||
for i, photo in enumerate(page.getchildren()[0].getchildren()):
|
||||
setdate_response = self.flickr.photos_setDates(
|
||||
photo_id=photo.attrib['id'],
|
||||
date_posted=set_to_date
|
||||
)
|
||||
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.getchildren()[0].getchildren()):
|
||||
yield i, page
|
||||
i += 1
|
||||
else:
|
||||
raise StopIteration
|
||||
|
||||
|
||||
def main(sysargs=sys.argv[:]):
|
||||
parser = argparse.ArgumentParser(
|
||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
||||
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(
|
||||
min_upload_date=(datetime.now() + timedelta(days=-1)),
|
||||
max_upload_date=(datetime.now() + timedelta(days=1))
|
||||
)
|
||||
log.warn('No query arguments provided! ' +
|
||||
'Defaulting to %r', query_args)
|
||||
|
||||
warper = TimeWarper(rc_conf['APIKEY'], rc_conf['APISECRET'])
|
||||
return warper.run(args.set_to_date, **query_args)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(TimeWarper().run())
|
||||
sys.exit(main())
|
||||
|
Loading…
Reference in New Issue
Block a user