Advertisement
Guest User

Untitled

a guest
Mar 21st, 2017
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.00 KB | None | 0 0
  1. import requests, argparse, sys
  2.  
  3. class checker:
  4.     def __init__(self):
  5.  
  6.         #Declare some variables
  7.         self.headers = {'User-agent': 'Mozilla/5.0'}
  8.         self.loginurl = 'https://www.instagram.com/accounts/login/ajax/'
  9.         self.url = 'https://www.instagram.com/'
  10.  
  11.         #Start a session and update headers
  12.         self.s = requests.session()
  13.         self.s.headers.update(self.headers)
  14.  
  15.  
  16.         #Gets username, password, textfile to check usernames, and output file for available usernames.
  17.         parser = argparse.ArgumentParser()
  18.         parser.add_argument("-u", dest='username', help="Instagram username",
  19.                             action="store")
  20.         parser.add_argument("-p", dest='password', help="Instagram password",
  21.                             action="store")
  22.         parser.add_argument("-i", dest='inputf', help="Textfile with usernames",
  23.                             action="store")
  24.         parser.add_argument("-o", dest='outputf', help="Output textfile",
  25.                             action="store")
  26.         args = parser.parse_args()
  27.  
  28.         #Save variables from argparse
  29.         self.username = args.username
  30.         self.password = args.password
  31.         self.inputf = args.inputf
  32.         self.outputf = args.outputf
  33.  
  34.     def login(self, username, password):
  35.         #Logs into instagram
  36.         loginRequest = self.s.post(
  37.                 self.loginurl,
  38.                     headers={
  39.                         'x-csrftoken': self.s.get(self.url).text.split('csrf_token": "')[1].split('"')[0],
  40.                         'x-instagram-ajax':'1',
  41.                         'x-requested-with': 'XMLHttpRequest',
  42.                         'Origin': self.url,
  43.                         'Referer': self.url,
  44.                     },
  45.                     data={
  46.                         'username':username,
  47.                         'password':password,
  48.                     }
  49.                 )
  50.            
  51.         if loginRequest.json()['authenticated']:
  52.             print('Logged In.')
  53.         else:
  54.             sys.exit("Login Failed, closing program.")
  55.  
  56.     def get_usernames(self, filename):
  57.         #Gets username from file
  58.         with open(filename, "r") as f:
  59.             usernames = f.read().split("\n")
  60.             return usernames
  61.  
  62.     def check_usernames(self, username, output):
  63.         #checks username and saves available usernames to new file
  64.         for user in usernames:
  65.             r = self.s.get(self.url+user)
  66.             al = r.text
  67.             text = al[al.find('<title>') + 7 :al.find('</title>')]
  68.             if "Page Not Found" in text:
  69.                 with open(output, "a") as a:
  70.                     a.write(user+'\n')
  71.  
  72. if __name__ == "__main__":
  73.     check = checker()
  74.     check.login(check.username, check.password)
  75.  
  76.     #Clears output file for new usernames
  77.     with open(check.outputf, "w") as a:
  78.         print('Output file cleared.')
  79.        
  80.     usernames = check.get_usernames(check.inputf)
  81.     check.check_usernames(usernames, check.outputf)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement