Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2020
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.17 KB | None | 0 0
  1. def get_requests_session(i_auth=None):
  2.     link = 'https://www.instagram.com/accounts/login/'
  3.     login_url = 'https://www.instagram.com/accounts/login/ajax/'
  4.     s = requests.Session()
  5.     incorrect_auth_pk = set()
  6.     if i_auth is None:
  7.         i_auth = InstagramAuth()
  8.  
  9.     iter_counter = len(i_auth.all_auth_list) or 1
  10.     while iter_counter > -1:
  11.         if i_auth.proxy:
  12.             proxy = i_auth.proxy.proxies
  13.         else:
  14.             proxy = {}
  15.         time = int(datetime.datetime.now().timestamp())
  16.  
  17.         payload = {
  18.             'username': i_auth.username,
  19.             'enc_password': f'#PWD_INSTAGRAM_BROWSER:0:{time}:{i_auth.password}',
  20.             # <-- note the '0' - that means we want to use plain passwords
  21.             'queryParams': {},
  22.             'optIntoOneTap': 'false'
  23.         }
  24.  
  25.         r = s.get(link)
  26.         csrf = re.findall(r"csrf_token\":\"(.*?)\"", r.text)[0]
  27.         logger.info(f"Try login with username: {i_auth.username} password: {i_auth.password}")
  28.         r = s.post(login_url, data=payload, proxies=proxy, headers={
  29.             "user-agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36",
  30.             "x-requested-with": "XMLHttpRequest",
  31.             "referer": "https://www.instagram.com/accounts/login/",
  32.             "x-csrftoken": csrf
  33.         })
  34.         try:
  35.             answer = r.json()
  36.             if answer['authenticated']:
  37.                 logger.info("Success auth!")
  38.                 return s
  39.         except:
  40.             answer = {'authenticated': False}
  41.  
  42.         logger.info(answer)
  43.         incorrect_auth_pk.add(i_auth.pk)
  44.         logger.info(f"Auth data {i_auth.username} is broken. Try next. Has {len(i_auth.all_auth_list)} values")
  45.         i_auth.from_redis()
  46.         if incorrect_auth_pk == set(i_auth.all_auth_list):
  47.             raise AllInstagramDatasBroken('No more datas')
  48.         logger.info(f"Try auth with {i_auth.username}")
  49.         iter_counter -= 1
  50.  
  51.  
  52. def requests_session_get(link: str, i_auth: InstagramAuth, session: Session):
  53.     i_auth.get_headers_from_string()
  54.  
  55.     logger.info(f'Parse requests with {i_auth.username} login. link - {link}')
  56.     if i_auth.proxy:
  57.         proxy = i_auth.proxy.proxies
  58.     else:
  59.         proxy = {}
  60.     logger.info(proxy)
  61.     # session = requests_login(session=session, auth=auth)
  62.     logger.info(f"Session after: {session}")
  63.     r = session.get(url=link, proxies=proxy, allow_redirects=True)
  64.     logger.info(f"Request ends with {r.status_code}")
  65.     logger.info(f"Answer is: \n {r.content[:100]}")
  66.     answer = {}
  67.     try:
  68.         answer = r.json()
  69.     except simplejson.errors.JSONDecodeError as err:
  70.         logger.error(f"Error in parsing {r.url}")
  71.         logger.error(err)
  72.         i_auth.state.downgrade()
  73.         i_auth.to_redis()
  74.         try:
  75.  
  76.             session.close()
  77.             sleep(3)
  78.             i_auth.from_redis()
  79.             session = get_requests_session(i_auth)
  80.             r = session.get(url=link, proxies=proxy, allow_redirects=True)
  81.             logger.info(f"Request ends with {r.status_code}")
  82.             logger.info(f"Answer is: \n {r.content[:100]}")
  83.             answer = r.json()
  84.         except AllInstagramDatasBroken as err:
  85.             logger.error(f"All instagram datas is broken")
  86.             logger.error(err)
  87.             raise InstagramAuthDataError(f"Broken auth data")
  88.         except Exception as err:
  89.  
  90.             logger.error(f"answer url is {r.url} reason {r.reason}")
  91.             logger.error(err)
  92.     sleep(1)
  93.  
  94.     return answer, session
  95.  
  96. def get_comment(victim: str, auth: InstagramAuth, session: Session):
  97.     victim_hash = victim.split('/')[-1]
  98.     comment_hash = auth.comments_hash
  99.     logger.info(f"Comment hash {comment_hash}")
  100.     url_base = 'https://www.instagram.com/graphql/query/?'
  101.     after = None
  102.     error_counter = 0
  103.     comments = []
  104.     while True:
  105.         logger.info(f'After is {after}')
  106.         after_value = f',"after":"{after}"' if after else ''
  107.         after_value = after_value.replace(': ', ':').replace(", ", ",")
  108.         variables = f'{{"shortcode":"{victim_hash}","first":20{after_value}}}'
  109.         get_params = {
  110.             'query_hash': comment_hash,
  111.             'variables': variables
  112.         }
  113.         current_link = url_base + urlencode(get_params)
  114.         try:
  115.             data, session = requests_session_get(current_link, i_auth=auth, session=session)
  116.             # data = curl_get(current_link, auth=auth)
  117.             logger.info(f"Comment data is {data}")
  118.             test_data = data['data']
  119.         except KeyError as err:
  120.             error_counter += 1
  121.             logger.error(err)
  122.             logger.error(f"incorrect parsing link {unquote(current_link)}\n {error_counter} try to sleep ")
  123.  
  124.             sleep(1)
  125.             if error_counter < 5:
  126.                 continue
  127.  
  128.             return comments, session
  129.         except InstagramAuthDataError as err:
  130.  
  131.             raise InstagramAuthDataError(err)
  132.  
  133.         else:
  134.             for node in data["data"]["shortcode_media"]["edge_media_to_parent_comment"]['edges']:
  135.                 logger.info(f"{node['node']['text']} from {node['node']['owner']['username']}")
  136.                 template = f"{node['node']['owner']['username']} написал: {node['node']['text']}"
  137.                 comments.append(template)
  138.                    
  139.             else:
  140.                 if data["data"]["shortcode_media"]["edge_media_to_parent_comment"]['page_info']['has_next_page']:
  141.                     after = data["data"]["shortcode_media"]["edge_media_to_parent_comment"]['page_info']['end_cursor']
  142.                     sleep(2)
  143.                     continue
  144.                 else:
  145.                     return comments, session
  146.  
  147.         return comments, session
  148.  
  149.  
  150. def parse(link: str):
  151.     if not link.startswith('https://www.instagram.com/p/') and not link.startswith('https://www.instagram.com/tv/'):
  152.         raise ValueError(f'Incorrect link {link}')
  153.     if link.count("?"):
  154.         link = link.split("?")[0]
  155.     post_hash = link.split('/')[-1]
  156.     i_auth = InstagramAuth()
  157.     session = get_requests_session(i_auth)
  158.     comments = get_comment(victim=post_hash, auth=i_auth, session=session)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement