Advertisement
Guest User

wololow

a guest
Oct 30th, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.15 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. """Follows Instagram users with similar taste and likes their photos.
  4.  
  5. Scrapes users who have liked a seed user's recent photos (avoiding
  6. users whose profiles seem spammy), then likes some of their most
  7. popular recent photos and follows them. After 3 days, unfollows them.
  8.  
  9. Required modules:
  10. httplib2
  11. simplejson
  12.  
  13. Version: 2.1.8
  14.  
  15. Licensed under a BSD New license.
  16.  
  17. Uses the https://github.com/Instagram/python-instagram client.
  18. by Spike Padley, 2014.
  19. """
  20.  
  21. import json
  22. import logging
  23. import os
  24. import random
  25. import re
  26. import time
  27. from instagram import client
  28.  
  29. # CUSTOMISABLE
  30. CONFIG = {
  31. 'client_id': 'e77b81e7bf0141348e73a53885de2982',
  32. 'client_secret': '669b27dcb790400bb421f9c66ac77077',
  33. 'redirect_uri': 'http://trapshop.biz',
  34. 'access_token': '1544274105.e77b81e.2a7e44a5e4f544a8a1f2a2bed24036d6',
  35. 'client_ips': ''
  36. }
  37. SEED_USER = 'kevin'
  38. NUM_TO_FOLLOW = 25
  39. NUM_TO_UNFOLLOW = 25
  40. # END CUSTOMISABLE
  41.  
  42. # Logging stuff
  43. logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  44. logger = logging.getLogger(__name__)
  45.  
  46. # Global declarations
  47. TILES_PATH = os.getcwd()+'/Tiles.json'
  48.  
  49.  
  50. def username_to_id(username):
  51. """Accepts a username and returns its ID."""
  52. user = api.user_search(q=username, count=1)
  53. if username != user[0].username:
  54. logger.error('Username to ID failed')
  55. return user[0].id
  56.  
  57.  
  58. def check_user(user, ids_to_avoid=[]): # TODO: Check user not super popular
  59. """Checks if user meets criteria to be followed, returns boolean.
  60.  
  61. Args:
  62. user (object): An instagram.models.User object
  63. ids_to_avoid (list): IDs to avoid, defaults to empty list
  64. """
  65. if (
  66. user.profile_picture != 'http://images.ak.instagram.com/profiles/anonymousUser.jpg'
  67. and user.full_name
  68. and user.bio
  69. and re.search(r'follow|f4f|1D|one ?direction|bieber|shout', user.bio, re.I) is None
  70. and user.id not in ids_to_avoid
  71. ):
  72. rel = api.user_relationship(user_id=user.id)
  73. if (
  74. rel.outgoing_status == 'none'
  75. and rel.incoming_status != 'followed_by'
  76. and rel.target_user_is_private is False
  77. ):
  78. return True
  79. else:
  80. return False
  81.  
  82. try:
  83. while True:
  84.  
  85. api = client.InstagramAPI(**CONFIG)
  86.  
  87. # Load Tiles.json
  88. tiles = {}
  89. with open(TILES_PATH) as f:
  90. tiles = json.load(f)
  91.  
  92. # Make a list of users who are currently being followed, or have been followed before
  93. already_followed = []
  94. for tile in tiles['present']:
  95. already_followed.append(tile['user_id'])
  96. for tile in tiles['past']:
  97. already_followed.append(tile['user_id'])
  98.  
  99. # Scrape users
  100. scraped_users = []
  101.  
  102. def scrape_users():
  103. next_url = ''
  104. while len(scraped_users) < NUM_TO_FOLLOW:
  105.  
  106. recent_media, next_url = api.user_recent_media(user_id=username_to_id(SEED_USER), count=2, with_next_url=next_url)
  107. for media in recent_media:
  108.  
  109. likes = api.media_likes(media_id=media.id)
  110. for user in likes:
  111.  
  112. if check_user(user=user, ids_to_avoid=(already_followed + scraped_users)):
  113.  
  114. scraped_users.append(user.id)
  115. logger.info('Scraped user ' + user.id)
  116.  
  117. if len(scraped_users) >= NUM_TO_FOLLOW:
  118. return
  119. else:
  120. logger.info('Avoided user ' + user.id)
  121. scrape_users()
  122.  
  123. logger.info('Following and liking the photos of %s users', len(scraped_users))
  124.  
  125. # Loop through scraped_users and like their photos and follow them
  126. for user_id in scraped_users:
  127. try:
  128. recent_media, next_url = api.user_recent_media(user_id=user_id, count=12)
  129.  
  130. media_dict = {}
  131. for media in recent_media:
  132. media_dict[media.like_count] = media.id
  133.  
  134. i = 1
  135. for likes in sorted(media_dict.keys(), reverse=True):
  136.  
  137. if not 0 < likes < 300:
  138. continue
  139.  
  140. if (random.random() + (i / (1 / 0.07))) < 0.5 or i <= 2:
  141.  
  142. api.like_media(media_id=media_dict[likes]) # like_media doesn't return anything?
  143. logger.info('Liked media ' + media_dict[likes])
  144. time.sleep(random.randint(20, 50))
  145.  
  146. i += 1
  147.  
  148. follow = api.follow_user(user_id=user_id)
  149. if follow[0].outgoing_status != 'none':
  150.  
  151. tiles['present'].append({'user_id': user_id, 'time_followed': time.time()})
  152. logger.info('Followed user ' + user_id)
  153.  
  154. except Exception, e:
  155. logger.error(e)
  156.  
  157. # Work out who (if anyone) is due for unfollowing
  158. to_unfollow = []
  159. for tile in tiles['present']:
  160. if (time.time() - tile['time_followed']) > (60 * 60 * 24 * 3):
  161. to_unfollow.append(tile)
  162. if len(to_unfollow) >= NUM_TO_UNFOLLOW:
  163. break
  164.  
  165. logger.info('Unfollowing %s users', len(to_unfollow))
  166.  
  167. # Unfollow users due for unfollowing
  168. for tile in to_unfollow:
  169. try:
  170. unfollow = api.unfollow_user(user_id=tile['user_id'])
  171. if unfollow[0].outgoing_status == 'none':
  172.  
  173. tiles['present'].remove(tile)
  174. tiles['past'].append(tile)
  175. logger.info('Unfollowed user ' + tile['user_id'])
  176.  
  177. except Exception, e:
  178. logger.error(e)
  179.  
  180. with open(TILES_PATH, 'w') as f:
  181. json.dump(tiles, f)
  182.  
  183. logger.info('Waiting 1 hour until repeat')
  184. time.sleep(60 * 60)
  185.  
  186. except KeyboardInterrupt:
  187. # ^C exits the script: Save Tiles.json first
  188. with open(TILES_PATH, 'w') as f:
  189. json.dump(tiles, f)
  190. logger.info('Saved and exited')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement