Advertisement
Guest User

Untitled

a guest
Feb 17th, 2018
784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from InstagramAPI import InstagramAPI
  3. from threading import Timer
  4. import json
  5. import string
  6. import time
  7. import requests
  8. import random
  9. import os, sys
  10. import getpass
  11. from random import uniform
  12.  
  13. '''
  14. Figure out if there's a next page
  15. Not needed? Might be in the future
  16. '''
  17. def nextPage(inbox):
  18. return "oldest_cursor" in inbox
  19.  
  20.  
  21. '''
  22. From the inbox, add each thread, per page (possibly? I only have two pages
  23. so it might not fully work for more, we shall see
  24. '''
  25. def getThreads(inb):
  26. threads = []
  27. unseen_count = inb["unseen_count"]
  28. #print(inb["threads"][0])
  29. for each in range(unseen_count):
  30. threads.append(inb["threads"][each])
  31. if "oldest_cursor" in inb:
  32. if len(threads) != unseen_count:
  33. getThreads(a.getv2Inbox(inb["oldest_cursor"]))
  34. return threads
  35.  
  36.  
  37.  
  38. '''
  39. Links don't return media_id
  40. '''
  41. def getMediaId(link):
  42. ret = requests.get('https://api.instagram.com/oembed/?url=' + link)
  43. txt = ret.text
  44. if txt == 'No URL Match':
  45. return
  46. else:
  47. txt = json.loads(ret.text)
  48. return txt['media_id']
  49.  
  50.  
  51. '''
  52. Get random comment from file f
  53. '''
  54. def generateComment(f):
  55. line = next(f)
  56. for num, aline in enumerate(f):
  57. if random.randrange(num+2):
  58. continue
  59. line = aline
  60. return line
  61.  
  62.  
  63. '''
  64. From each thread in list, search messages from select groups, get code
  65. in order to like and comment, only if timestamp after last_seen_at
  66. '''
  67. def getCodes(threads):
  68. codes = set()
  69. for thread in threads:
  70. #json.loads(each)
  71. group_name = thread["thread_title"]
  72. thread_id = thread["thread_id"]
  73. viewer_id = thread["viewer_id"]
  74.  
  75. #try to get a last_seen_at, if it exists. if not, it should only contain
  76. #unread messages in `items` in which case only go through those items (setting
  77. #last_seen_at to 0 so it'll always be less than the last activity time
  78. try:
  79. last_seen_at = thread["last_seen_at"][str(viewer_id)]["timestamp"]
  80. except:
  81. last_seen_at = 0
  82. messages = thread["items"]
  83. txt_name = ''.join(filter(lambda x: x in printable, group_name))
  84.  
  85. #fix group names with emojis
  86. if str(txt_name).lower().strip() in map(str.lower, group_names):
  87. for message in messages:
  88. url =''
  89. if int(last_seen_at) < int(thread["last_activity_at"]):
  90. a.markItemSeen(thread_id, message["item_id"])
  91. typeM = message['item_type']
  92. if typeM == 'media_share':
  93. codes.add(str(message['media_share']['id']))
  94. url = "instagram.com/p/" + str(message['media_share']['code'])
  95. elif typeM == 'link':
  96. url = str(message['link']['text'])
  97. if 'instagram.com/p/' in url:
  98. codes.add(str(getMediaId(url)))
  99. if url:
  100. print('added code from group {0} with url {1}'.format(txt_name,url))
  101. return codes
  102.  
  103.  
  104.  
  105.  
  106. '''
  107. Read messages in each group, based on timestamp, per message
  108. '''
  109. def likeCommentMessages(codes):
  110. for each in codes:
  111. comments_file = open("comments.txt", "r")
  112. a.like(each)
  113. time.sleep(round(uniform(3.0,6.0),1))
  114. a.comment(each, generateComment(comments_file))
  115. time.sleep(round(uniform(1.0,3.0),1))
  116. print("Like/comment done")
  117. return 0
  118.  
  119.  
  120.  
  121. u,p=input("Username: "),getpass.getpass()
  122. #IF YOU WANT TO HARDCODE, UNCOMMENT AND ENTER HERE
  123. #u,p='USER','PASSWORD'
  124.  
  125. printable = set(string.printable)
  126.  
  127. #Load group names from groups.txt, new line separated, stripping new lines
  128. group_names = []
  129. #threads = []
  130. with open("groups.txt") as f:
  131. for line in f:
  132. group_names.append(line.rstrip())
  133. print(group_names)
  134.  
  135.  
  136. def main():
  137. global a
  138. a = InstagramAPI(u,p)
  139. a.login()
  140. inbox = json.loads(a.getv2Inbox().text)["inbox"]
  141. threads = getThreads(inbox)
  142. codes = getCodes(threads)
  143. likeCommentMessages(codes)
  144. t = Timer(60*60, main)
  145. t.start()
  146. a.logout()
  147.  
  148.  
  149. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement