Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from InstagramAPI import InstagramAPI
- from threading import Timer
- import json
- import string
- import time
- import requests
- import random
- import os, sys
- import getpass
- from random import uniform
- '''
- Figure out if there's a next page
- Not needed? Might be in the future
- '''
- def nextPage(inbox):
- return "oldest_cursor" in inbox
- '''
- From the inbox, add each thread, per page (possibly? I only have two pages
- so it might not fully work for more, we shall see
- '''
- def getThreads(inb):
- threads = []
- unseen_count = inb["unseen_count"]
- #print(inb["threads"][0])
- for each in range(unseen_count):
- threads.append(inb["threads"][each])
- if "oldest_cursor" in inb:
- if len(threads) != unseen_count:
- getThreads(a.getv2Inbox(inb["oldest_cursor"]))
- return threads
- '''
- Links don't return media_id
- '''
- def getMediaId(link):
- ret = requests.get('https://api.instagram.com/oembed/?url=' + link)
- txt = ret.text
- if txt == 'No URL Match':
- return
- else:
- txt = json.loads(ret.text)
- return txt['media_id']
- '''
- Get random comment from file f
- '''
- def generateComment(f):
- line = next(f)
- for num, aline in enumerate(f):
- if random.randrange(num+2):
- continue
- line = aline
- return line
- '''
- From each thread in list, search messages from select groups, get code
- in order to like and comment, only if timestamp after last_seen_at
- '''
- def getCodes(threads):
- codes = set()
- for thread in threads:
- #json.loads(each)
- group_name = thread["thread_title"]
- thread_id = thread["thread_id"]
- viewer_id = thread["viewer_id"]
- #try to get a last_seen_at, if it exists. if not, it should only contain
- #unread messages in `items` in which case only go through those items (setting
- #last_seen_at to 0 so it'll always be less than the last activity time
- try:
- last_seen_at = thread["last_seen_at"][str(viewer_id)]["timestamp"]
- except:
- last_seen_at = 0
- messages = thread["items"]
- txt_name = ''.join(filter(lambda x: x in printable, group_name))
- #fix group names with emojis
- if str(txt_name).lower().strip() in map(str.lower, group_names):
- for message in messages:
- url =''
- if int(last_seen_at) < int(thread["last_activity_at"]):
- a.markItemSeen(thread_id, message["item_id"])
- typeM = message['item_type']
- if typeM == 'media_share':
- codes.add(str(message['media_share']['id']))
- url = "instagram.com/p/" + str(message['media_share']['code'])
- elif typeM == 'link':
- url = str(message['link']['text'])
- if 'instagram.com/p/' in url:
- codes.add(str(getMediaId(url)))
- if url:
- print('added code from group {0} with url {1}'.format(txt_name,url))
- return codes
- '''
- Read messages in each group, based on timestamp, per message
- '''
- def likeCommentMessages(codes):
- for each in codes:
- comments_file = open("comments.txt", "r")
- a.like(each)
- time.sleep(round(uniform(3.0,6.0),1))
- a.comment(each, generateComment(comments_file))
- time.sleep(round(uniform(1.0,3.0),1))
- print("Like/comment done")
- return 0
- u,p=input("Username: "),getpass.getpass()
- #IF YOU WANT TO HARDCODE, UNCOMMENT AND ENTER HERE
- #u,p='USER','PASSWORD'
- printable = set(string.printable)
- #Load group names from groups.txt, new line separated, stripping new lines
- group_names = []
- #threads = []
- with open("groups.txt") as f:
- for line in f:
- group_names.append(line.rstrip())
- print(group_names)
- def main():
- global a
- a = InstagramAPI(u,p)
- a.login()
- inbox = json.loads(a.getv2Inbox().text)["inbox"]
- threads = getThreads(inbox)
- codes = getCodes(threads)
- likeCommentMessages(codes)
- t = Timer(60*60, main)
- t.start()
- a.logout()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement