Advertisement
kopyl

Untitled

Aug 11th, 2023 (edited)
1,252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | None | 0 0
  1. def get_query_code():
  2.     invitation_manager_url = 'https://www.linkedin.com/mynetwork/invitation-manager/sent/'
  3.     invitation_manager_response = requests.get(invitation_manager_url, cookies=cookies, headers=headers)
  4.     soup = bs(invitation_manager_response.text, 'html.parser')
  5.    
  6.     code = soup.find_all('code')
  7.  
  8.     code_chunks_filtered = [
  9.         c for c in code if
  10.         "voyagerRelationshipsDashSentInvitationViews"
  11.         in c.text
  12.     ]
  13.  
  14.     if len(code_chunks_filtered) != 1:
  15.         return {
  16.             'error': 'Found more than one code chunk containing the query',
  17.             'code_chunks_filtered': code_chunks_filtered,
  18.         }
  19.  
  20.     code_chunk = code_chunks_filtered[0]
  21.    
  22.     try:
  23.         code_json = json.loads(code_chunk.text)
  24.     except ValueError:
  25.         return {
  26.             'error': 'Code chunk is not in JSON format',
  27.             'code_chunk': code_chunk.text
  28.         }
  29.     try:
  30.         request_url = code_json["request"]
  31.     except KeyError:
  32.         return {
  33.             'error': 'Code chunk does not contain a request URL',
  34.             'code_chunk': code_chunk.text
  35.         }
  36.     try:
  37.         query_id = request_url.split('voyagerRelationshipsDashSentInvitationViews.', 1)[1]
  38.     except IndexError:
  39.         return {
  40.             'error': 'Code chunk does not contain a query ID',
  41.             'code_chunk': code_chunk.text
  42.         }
  43.  
  44.     return {
  45.         'query_id': query_id,
  46.     }
  47.    
  48.  
  49. def get_sent_connection_requests(start=0, count=100):
  50.     query_id = get_query_code()
  51.     if 'error' in query_id:
  52.         return query_id['error']
  53.     query_id = query_id['query_id']
  54.    
  55.     response = requests.get(
  56.         (
  57.             'https://www.linkedin.com/voyager/api/graphql?'
  58.             f'variables=(start:{start},count:{count},invitationType:CONNECTION)'
  59.             '&&queryId=voyagerRelationshipsDashSentInvitationViews.'
  60.             f'{query_id}'
  61.         ),
  62.         cookies=cookies,
  63.         headers=headers,
  64.     )
  65.     try:
  66.         elements = response.json()['data']['relationshipsDashSentInvitationViewsByInvitationType']['elements']
  67.     except KeyError:
  68.         return {
  69.             'error': 'Response does not contain the expected data',
  70.             'response': response.json(),
  71.         }
  72.     except json.JSONDecodeError:
  73.         return {
  74.             'error': 'Response is not in JSON format',
  75.             'response': response.text,
  76.         }
  77.     try:
  78.         user_ids = [e["invitation"]["entityUrn"] for e in elements]
  79.     except KeyError:
  80.         return {
  81.             'error': 'Response does not contain the expected data',
  82.             'response': response.json(),
  83.         }
  84.    
  85.     return {
  86.         'elements': elements,
  87.         'user_ids': user_ids
  88.     }
  89.  
  90.  
  91. result = get_sent_connection_requests(0, 100)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement