Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import glob
  3. import os
  4. import random
  5. import shutil
  6. from google_images_download import google_images_download
  7. from InstagramAPI import InstagramAPI
  8.  
  9. keyw = ["turtles", "cats", "dogs", "birds", "landscape", "skyline", "satellite images", "exoplanets", "lakes", "hippos", "exotic animals"]
  10. path2img = ""
  11. username = ""
  12. password = ""
  13. tofetch = 50
  14.  
  15. # login
  16. api = InstagramAPI(username, password)
  17. api.login()
  18.  
  19. def fetch(keyw):
  20. # google images download
  21. response = google_images_download.googleimagesdownload()
  22. arguments = {"keywords": keyw, "limit": tofetch, "print_urls": True, "type": "photo", "size": ">800*600", "output_directory": "./images", "safe_search": True}
  23. response.download(arguments)
  24.  
  25. # move ./images/*keyword* to ./images/
  26. try:
  27. for i in glob.glob("./images/*/*"):
  28. shutil.move(i, "./images")
  29. except:
  30. print("error while moving downloaded images a dir up")
  31. # some other file operation?
  32. try:
  33. for i in glob.glob("./images/*"):
  34. shutil.rmtree("./images/"+keyw, ignore_errors=True)
  35. except:
  36. pass
  37.  
  38. firstFilePath = path2img+os.listdir(path2img)[0]
  39. print("image to be uploaded: "+firstFilePath)
  40.  
  41. try:
  42. API.uploadPhoto(firstFilePath, caption="1 bild fΓΌr dich lol :)")
  43. except:
  44. print("upload failed")
  45. exit()
  46.  
  47. # remove image to avoid dups uploads
  48. os.remove(firstFilePath)
  49.  
  50. # if <2 images in imagedir, fetch new ones
  51. if len(os.listdir(path2img)) < 2:
  52. chosen_keyw = keyw[random.randint(0, len(keyw))]
  53. # keyw.pop(chosen_keyw)
  54. fetch(chosen_keyw)
  55.  
  56. print("uploaded: "+firstFilePath)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement