Guest User

Untitled

a guest
Nov 20th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. #!/usr/bin/python
  2. from argparse import ArgumentParser
  3. from ConfigParser import ConfigParser
  4. from fivehundredpx.client import FiveHundredPXAPI
  5. from fivehundredpx.auth import *
  6. from urllib import urlretrieve
  7. from urlparse import urlparse
  8. from os.path import basename, join as join_path
  9. from random import shuffle
  10. from glob import glob
  11. from os import remove
  12.  
  13.  
  14. def clear_directory(directory):
  15. files = glob(join_path(directory, '*'))
  16. for f in files:
  17. remove(f)
  18.  
  19.  
  20. if __name__ == "__main__":
  21. parser = ArgumentParser()
  22. parser.add_argument("dir", type=str)
  23. parser.add_argument("key", type=str)
  24. parser.add_argument("secret", type=str)
  25. parser.add_argument("username", type=str)
  26. parser.add_argument("password", type=str)
  27. parser.add_argument("-n", type=int, default=100)
  28. parser.add_argument("-x", action='store_true', help="clear existing")
  29. parser.add_argument("-s", action='store_true', help="shuffle photos")
  30. args = parser.parse_args()
  31.  
  32. SHUFFLE = args.s
  33. DIRECTORY = args.dir
  34. USERNAME = args.username
  35. PASSWORD = args.password
  36. CONSUMER_KEY = args.key
  37. CONSUMER_SECRET = args.secret
  38. N_PHOTOS = args.n
  39. CLEAR_EXISTING = args.x
  40.  
  41. handler = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
  42. token = handler.get_request_token()
  43. handler.set_request_token(token.key, token.secret)
  44. token = handler.get_xauth_access_token(USERNAME, PASSWORD)
  45. handler.set_access_token(token.key, token.secret)
  46. api = FiveHundredPXAPI(handler)
  47.  
  48. result = api.photos(require_auth=True, feature='editors', personalized_categories=True, image_size=2048, sort='highest_rating', rpp=100)
  49.  
  50. photos = result['photos']
  51. if SHUFFLE:
  52. shuffle(photos)
  53.  
  54. if CLEAR_EXISTING:
  55. clear_directory(DIRECTORY)
  56.  
  57. selection = photos[:N_PHOTOS]
  58. for data in selection:
  59. image_url = data['image_url']
  60. url = data['url']
  61.  
  62. if data['image_format'] != 'jpeg':
  63. print("Unsure of how to deal with this image, skipping %s" % url)
  64. continue
  65.  
  66. # Get filename
  67. o = urlparse(url)
  68. file_name = basename(o.path) + ".jpg"
  69. file_path = join_path(DIRECTORY, file_name)
  70. urlretrieve(image_url, file_path)
Add Comment
Please, Sign In to add comment