Advertisement
Guest User

IG.py

a guest
Feb 11th, 2019
6,687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.53 KB | None | 0 0
  1. """Instagram bot
  2. Created by Çlirim Furriku
  3. """
  4. import re
  5. import requests
  6. import logging
  7.  
  8.  
  9. class Insta:
  10.     def __init__(self, comment, likes_per_search, comments_per_search):
  11.         self.comment = comment
  12.         self.lps = likes_per_search
  13.         self.cps = comments_per_search
  14.     def do_header(self):
  15.         """Header"""
  16.         self.cookies = {
  17.             'csrftoken': self.csrftoken[0],
  18.             'sessionid': self.sessionid[0],
  19.         }
  20.         self.header = {
  21.             'Cookie': 'sessionid=' + self.sessionid[0] + '; csrftoken=' + self.csrftoken[0] + ';',
  22.             'Origin': 'https://www.instagram.com',
  23.             'Accept-Encoding': 'gzip, deflate',
  24.             'Accept-Language': 'en-US, en; q=0.8',
  25.             'User-Agent': 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36',
  26.             'X-Requested-With': 'XMLHttpRequest',
  27.             'X-CSRFToken': self.csrftoken[0],
  28.             'X-Instagram-Ajax': '90d932cc38bc',
  29.             'Content-Type': 'application/x-www-form-urlencoded',
  30.             'Accept': '*/*',
  31.             'Referer': 'https://www.instagram.com/',
  32.             'Authority': 'www.instagram.com',
  33.         }
  34.  
  35.     def login(self, email, psw):
  36.         """Login using Username and Password"""
  37.  
  38.         # import logging
  39.  
  40.         # These two lines enable debugging at httplib level (requests->urllib3->http.client)
  41.         # You will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA.
  42.         # The only thing missing will be the response.body which is not logged.
  43.  
  44.         # try:
  45.         #     import http.client as http_client
  46.         # except ImportError:
  47.         #     # Python 2
  48.         #     import httplib as http_client
  49.         # http_client.HTTPConnection.debuglevel = 1
  50.         #
  51.         # # You must initialize logging, otherwise you'll not see debug output.
  52.         # logging.basicConfig()
  53.         # logging.getLogger().setLevel(logging.DEBUG)
  54.         # requests_log = logging.getLogger("requests.packages.urllib3")
  55.         # requests_log.setLevel(logging.DEBUG)
  56.         # requests_log.propagate = True
  57.  
  58.         payload = {'action': 'login', 'username': email, 'password': psw}
  59.         header = {
  60.             'Origin': 'https://www.instagram.com',
  61.             'Accept-Encoding': 'gzip, deflate',
  62.             'Accept-Language': 'en-US, en; q=0.8',
  63.             'User-Agent': 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36',
  64.             'X-Requested-With': 'XMLHttpRequest',
  65.             'X-Instagram-Ajax': '90d932cc38bc',
  66.             'Content-Type': 'application/x-www-form-urlencoded',
  67.             'Accept': '*/*',
  68.             'Referer': 'https://www.instagram.com/accounts/login/',
  69.             'Authority': 'www.instagram.com',
  70.         }
  71.         response = requests.get('https://www.instagram.com/accounts/login/', headers=header)
  72.         self.csrftoken = re.findall('(?:csrftoken=)(................................)(?:;)', str(response.headers))
  73.         cookies = {
  74.             'csrftoken': self.csrftoken[0],
  75.         }
  76.  
  77.         header = {
  78.             'Cookie': 'csrftoken=' + (self.csrftoken[0]) + ';',
  79.             'Origin': 'https://www.instagram.com',
  80.             'Accept-Encoding': 'gzip, deflate',
  81.             'Accept-Language': 'en-US, en; q=0.8',
  82.             'User-Agent': 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36',
  83.             'X-Requested-With': 'XMLHttpRequest',
  84.             'X-Instagram-Ajax': '715dcf29ace5',
  85.             'DNT': '1',
  86.             'X-CSRFToken': str(self.csrftoken[0]),
  87.             'Content-Type': 'application/x-www-form-urlencoded',
  88.             'Accept': '*/*',
  89.             'Referer': 'https://www.instagram.com/accounts/login/',
  90.             'Authority': 'www.instagram.com',
  91.         }
  92.         r = requests.post('https://www.instagram.com/accounts/login/ajax/', headers=header, data=payload)
  93.         if 'checkpoint_required' in str(r.content):
  94.             link = re.findall('(?:"checkpoint_url": ")(/challenge/\d\d\d\d\d\d\d\d\d\d/\w\w\w\w\w\w\w\w\w\w/)', str(r.content))
  95.             link = 'https://www.instagram.com' + str(link[0])
  96.             header2 = {
  97.                 'Cookie': 'csrftoken=' + (self.csrftoken[0]),
  98.                 'Origin': 'https://www.instagram.com',
  99.                 'Accept-Encoding': 'gzip, deflate',
  100.                 'Accept-Language': 'en-US, en; q=0.8',
  101.                 'User-Agent': 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36',
  102.                 'X-Requested-With': 'XMLHttpRequest',
  103.                 'X-Instagram-Ajax': '715dcf29ace5',
  104.                 'DNT': '1',
  105.                 'X-CSRFToken': str(self.csrftoken[0]),
  106.                 'Content-Type': 'application/x-www-form-urlencoded',
  107.                 'Accept': '*/*',
  108.                 'Referer': link,
  109.                 'Authority': 'www.instagram.com',
  110.             }
  111.             print(link)
  112.             r = requests.get(link, headers=header2, cookies=cookies)
  113.             email = re.findall('(?:email":")(.*)(?:"}."navigation":)', str(r.text))
  114.             login = []
  115.             if len(email[0]) > 0:
  116.                 login.append(email[0])
  117.             print('2FA Required')
  118.             print('Please chose method to verify')
  119.             a = 1
  120.             for info in login:
  121.                 print(str(a) + ': ' + str(info))
  122.                 a += 1
  123.             choice = input('Enter Your choice: ')
  124.             r = requests.post(link, headers=header2, cookies=cookies, data={'choice': str(choice)})
  125.             if r.status_code == 200:
  126.                 code = input('Enter Verification conde: ')
  127.                 if len(code) is not 6:
  128.                     print('Verification code should be 6 digits')
  129.                     code = input('Enter Verification conde: ')
  130.                 r = requests.post(link, headers=header2, cookies=cookies, data={'security_code': code})
  131.                 if r.status_code == 200:
  132.                     print('Login Successful')
  133.                 else:
  134.                     print('Error ' + str(r.statatus_code))
  135.         self.sessionid = re.findall('(?:sessionid=)(................................)(?:;)', str(r.headers))
  136.         if len(str(self.sessionid)) == 0:
  137.             print('Login Error')
  138.             exit()
  139.  
  140.     def cookie_login(self, sessionid, csrftoken):
  141.         """Login using cookies"""
  142.         csrftoken = 'zfq2S8uGP9ukzKi4mcW545NtVols6h2w'
  143.         sessionid = '1308170680%3Al9jYfu3sOz7Tqq%3A14'
  144.         self.sessionid = [str(sessionid)]
  145.         self.csrftoken = [str(csrftoken)]
  146.  
  147.     def find_search(self, info, mthd):
  148.         """Find PostID by Tag or Location"""
  149.         if mthd == 1:
  150.             url = "https://www.instagram.com/explore/tags/" + info + "/?__a=1"
  151.         if mthd == 2:
  152.             url = "https://www.instagram.com/explore/locations/" + info + "/?__a=1"
  153.         r = requests.get(url)
  154.         self.id = re.findall('(?:"id":")(\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d)(?:",)', str(r.text))
  155.  
  156.     def find_on_followers(self, search_id):
  157.         """Find profile ID on someone's followers'"""
  158.         # try:
  159.         #     import http.client as http_client
  160.         # except ImportError:
  161.         #     # Python 2
  162.         #     import httplib as http_client
  163.         # http_client.HTTPConnection.debuglevel = 1
  164.         #
  165.         # # You must initialize logging, otherwise you'll not see debug output.
  166.         # logging.basicConfig()
  167.         # logging.getLogger().setLevel(logging.DEBUG)
  168.         # requests_log = logging.getLogger("requests.packages.urllib3")
  169.         # requests_log.setLevel(logging.DEBUG)
  170.         # requests_log.propagate = True
  171.  
  172.         url = "https://www.instagram.com/graphql/query/?query_hash=56066f031e6239f35a904ac20c9f37d9&variables=%7B%22id%22%3A%22" + search_id + "%22%2C%22include_reel%22%3Atrue%2C%22fetch_mutual%22%3Afalse%2C%22first%22%3A4%7D"
  173.  
  174.         r = requests.get(url, headers=self.header, cookies=self.cookies)
  175.         self.ids = re.findall('(?:{"id":")(\d\d\d\d\d\d\d\d\d\d)(?:")', str(r.text))
  176.         print(r.status_code)
  177.  
  178.     def find_on_comments(self, post_shortcode):
  179.         """Find people ID on comments"""
  180.         url = 'https://www.instagram.com/p/' + post_shortcode + '/?__a = 1'
  181.         r = requests.get(url)
  182.  
  183.     def do_like(self):
  184.         """Like posts by PostID"""
  185.         i = 1
  186.         for id in self.id:
  187.             url = "https://www.instagram.com/web/likes/" + str(id) + "/like/"
  188.             r = requests.post(url, headers=self.header)
  189.             print(r.status_code)
  190.             if i == self.lps:
  191.                 break
  192.  
  193.     def do_comment(self):
  194.         """Do comments on the PostID"""
  195.         i = 1
  196.         for postid in self.id:
  197.             url = "https://www.instagram.com/web/comments/" + str(postid) + "/add/"
  198.             header = {
  199.                 'Origin': 'https: // www.instagram.com',
  200.                 'X-Instagram-AJAX': '715dcf29ace5',
  201.                 'Content-Type': 'application/x-www-form-urlencoded',
  202.                 'Accept': '*/*',
  203.                 'X-Requested-With': 'XMLHttpRequest',
  204.                 'User-Agent': 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36' +
  205.                 ' (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36',
  206.                 'X-CSRFToken': self.csrftoken[0],
  207.                 'Referer': 'https://www.instagram.com/p/BsDxoeGlFhR/',
  208.                 'Accept-Encoding': 'gzip, deflate, br',
  209.                 'Accept-Language': 'en-US, en; q=0.8',
  210.                 'Cookie': 'sessionid=' + self.sessionid[0] +
  211.                 '; csrftoken=' + self.csrftoken[0] + ';',
  212.             }
  213.  
  214.             r = requests.post(url, headers=header, cookies=self.cookies, data={'comment_text': self.comment, 'replied_to_comment_id': ''})
  215.             print(r.status_code)
  216.             if i == self.cps:
  217.                 break
  218.  
  219.     def do_follow(self):
  220.         """ Follow peoples with by ID"""
  221.         for id in self.ids:
  222.             url = 'https://www.instagram.com/web/friendships/' + id + '/follow/'
  223.             r = requests.post(url, headers=self.header, cookies=self.cookies)
  224.             print(r.status_code)
  225.  
  226.     def do_unfollow(self):
  227.         """Unfollow people by ID"""
  228.         for id in self.ids:
  229.             url = 'https://www.instagram.com/web/friendships/' + id + '/unfollow/'
  230.             r = requests.post(url, headers=self.header, cookies=self.cookies)
  231.             print(r.status_code)
  232.  
  233.  
  234.  
  235. LPS = input('Number of likes for search item')
  236. CPS = input('Number of comments for search item')
  237. CMNT = input("Comment: ")
  238. IG = Insta(CMNT, LPS, CPS)
  239.  
  240. # How To Login
  241. # 1) Username/Passwoed
  242. # 2) Cookies
  243. HTL = 2
  244. if HTL == 1:
  245.     # email = input("Enter Your IG Username: ")
  246.     # psw = input("Enter Your IG Password: ")
  247.     EMAIL = 'popular.musical.yt'
  248.     PSW = ''
  249.     IG.login(EMAIL, PSW)
  250.  
  251. elif HTL == 2:
  252.     CSRFTOKEN = 'zfq2S8uGP9ukzKi4mcW545NtVols6h2w'
  253.     SESSIONID = ''
  254.     IG.cookie_login(SESSIONID, CSRFTOKEN)
  255. IG.do_header()
  256. S_ID = '1308170680'
  257. # s_id = '5821462185'
  258. IG.find_on_followers(S_ID)
  259. IG.do_follow()
  260. IG.do_unfollow()
  261. # comment = 'Nice Pic'
  262. #
  263. # Methods to get info
  264. # 1 - By Hashtags (f4f, like4like)
  265. # 2 - By Location (213385402/london-united-kingdom)
  266. # 3 -
  267. # mthd = 1
  268. # if mthd == 1:
  269. #   print('Hashtag example: sky l4l follow4follow')
  270. #   hashtags = input('Enter hashtags: ')
  271. #   info_search = hashtags.split(' ')
  272. #
  273. # while True:
  274. #     for element in info_search:
  275. #         ig.find_search(str(element), mthd)
  276. #         ig.do_like()
  277. #         ig.do_comment()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement