Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- from argparse import ArgumentParser
- from ConfigParser import ConfigParser
- from fivehundredpx.client import FiveHundredPXAPI
- from fivehundredpx.auth import *
- from urllib import urlretrieve
- from urlparse import urlparse
- from os.path import basename, join as join_path
- from random import shuffle
- from glob import glob
- from os import remove
- def clear_directory(directory):
- files = glob(join_path(directory, '*'))
- for f in files:
- remove(f)
- if __name__ == "__main__":
- parser = ArgumentParser()
- parser.add_argument("dir", type=str)
- parser.add_argument("key", type=str)
- parser.add_argument("secret", type=str)
- parser.add_argument("username", type=str)
- parser.add_argument("password", type=str)
- parser.add_argument("-n", type=int, default=100)
- parser.add_argument("-x", action='store_true', help="clear existing")
- parser.add_argument("-s", action='store_true', help="shuffle photos")
- args = parser.parse_args()
- SHUFFLE = args.s
- DIRECTORY = args.dir
- USERNAME = args.username
- PASSWORD = args.password
- CONSUMER_KEY = args.key
- CONSUMER_SECRET = args.secret
- N_PHOTOS = args.n
- CLEAR_EXISTING = args.x
- handler = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
- token = handler.get_request_token()
- handler.set_request_token(token.key, token.secret)
- token = handler.get_xauth_access_token(USERNAME, PASSWORD)
- handler.set_access_token(token.key, token.secret)
- api = FiveHundredPXAPI(handler)
- result = api.photos(require_auth=True, feature='editors', personalized_categories=True, image_size=2048, sort='highest_rating', rpp=100)
- photos = result['photos']
- if SHUFFLE:
- shuffle(photos)
- if CLEAR_EXISTING:
- clear_directory(DIRECTORY)
- selection = photos[:N_PHOTOS]
- for data in selection:
- image_url = data['image_url']
- url = data['url']
- if data['image_format'] != 'jpeg':
- print("Unsure of how to deal with this image, skipping %s" % url)
- continue
- # Get filename
- o = urlparse(url)
- file_name = basename(o.path) + ".jpg"
- file_path = join_path(DIRECTORY, file_name)
- urlretrieve(image_url, file_path)
Add Comment
Please, Sign In to add comment