Guest User

Untitled

a guest
Dec 5th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. from InstagramAPI import InstagramAPI
  2. import time
  3.  
  4.  
  5. def ask_for_info():
  6. """
  7. Ask the user running this script for certain info we need.
  8. Returns a dictionary with the answers.
  9. """
  10. username = input('What\'s your Instagram Username? ')
  11. password = input('What\'s your Instagram Password? ')
  12. num_to_follow = input('How many people should we follow per day? ')
  13. tags_to_use = [input('What tag should I use? (No #) ')]
  14. while True:
  15. more = input('Any more? (Leave blank for no) ')
  16. if more == '':
  17. break
  18. tags_to_use.append(more)
  19.  
  20. # This does not validate the username or password, so make sure you enter in the right one!
  21.  
  22. return {
  23. 'username': username,
  24. 'password': password,
  25. 'num_to_follow': int(num_to_follow),
  26. 'tags_to_use': tags_to_use
  27. }
  28.  
  29.  
  30. def login(username, password):
  31. api = InstagramAPI(username, password) # Instantiate the class
  32. api.login() # Send a login request
  33. return api
  34.  
  35.  
  36. def find_targets(api, tag):
  37. """
  38. Search for users who have posted a picture with tag, and return a list of user IDs.
  39. """
  40.  
  41. ok = api.tagFeed(tag) # Automatically prints an error
  42. if not ok:
  43. raise Exception # Error the script
  44.  
  45. resp = api.LastJson
  46. images = resp['items']
  47.  
  48. users = []
  49. for image in images:
  50. users.append(
  51. # We use .get so we can filter out invalid data.
  52. # Invalid data is rare, but in this case we are prepared
  53. # pk is ID
  54. image.get('user', {}).get('pk', '')
  55. )
  56.  
  57. # a list of users from the list of users which id isn't an empty string
  58. return [u for u in users if u != '']
  59.  
  60.  
  61. def follow_all(api, targets, time_to_wait):
  62. """
  63. Follow all targets, while waiting time_to_wait in seconds
  64. """
  65. total_targets = len(targets)
  66. num = 0
  67. for t in targets:
  68. ok = api.follow(t)
  69. if not ok:
  70. print('Error while trying to follow ID: %s', t)
  71. num += 1
  72. print('Followed %d/%d' % (num, total_targets))
  73. time.sleep(time_to_wait)
  74.  
  75.  
  76. if __name__ == '__main__':
  77. answers = ask_for_info()
  78. print('Thanks!')
  79.  
  80. username = answers['username']
  81. password = answers['password']
  82. num_to_follow = answers['num_to_follow']
  83. tags_to_use = answers['tags_to_use']
  84.  
  85. api = login(username, password)
  86.  
  87. targets = []
  88. for tag in tags_to_use:
  89. targets += find_targets(api, tag)
  90. print('%d Targets Acquired!' % len(targets))
  91.  
  92. secs_in_a_day = 86400
  93. time_to_wait = secs_in_a_day / num_to_follow
  94.  
  95. follow_all(api, targets, time_to_wait)
  96. print('All Done!')
Add Comment
Please, Sign In to add comment