Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_query_code():
- invitation_manager_url = 'https://www.linkedin.com/mynetwork/invitation-manager/sent/'
- invitation_manager_response = requests.get(invitation_manager_url, cookies=cookies, headers=headers)
- soup = bs(invitation_manager_response.text, 'html.parser')
- code = soup.find_all('code')
- code_chunks_filtered = [
- c for c in code if
- "voyagerRelationshipsDashSentInvitationViews"
- in c.text
- ]
- if len(code_chunks_filtered) != 1:
- return {
- 'error': 'Found more than one code chunk containing the query',
- 'code_chunks_filtered': code_chunks_filtered,
- }
- code_chunk = code_chunks_filtered[0]
- try:
- code_json = json.loads(code_chunk.text)
- except ValueError:
- return {
- 'error': 'Code chunk is not in JSON format',
- 'code_chunk': code_chunk.text
- }
- try:
- request_url = code_json["request"]
- except KeyError:
- return {
- 'error': 'Code chunk does not contain a request URL',
- 'code_chunk': code_chunk.text
- }
- try:
- query_id = request_url.split('voyagerRelationshipsDashSentInvitationViews.', 1)[1]
- except IndexError:
- return {
- 'error': 'Code chunk does not contain a query ID',
- 'code_chunk': code_chunk.text
- }
- return {
- 'query_id': query_id,
- }
- def get_sent_connection_requests(start=0, count=100):
- query_id = get_query_code()
- if 'error' in query_id:
- return query_id['error']
- query_id = query_id['query_id']
- response = requests.get(
- (
- 'https://www.linkedin.com/voyager/api/graphql?'
- f'variables=(start:{start},count:{count},invitationType:CONNECTION)'
- '&&queryId=voyagerRelationshipsDashSentInvitationViews.'
- f'{query_id}'
- ),
- cookies=cookies,
- headers=headers,
- )
- try:
- elements = response.json()['data']['relationshipsDashSentInvitationViewsByInvitationType']['elements']
- except KeyError:
- return {
- 'error': 'Response does not contain the expected data',
- 'response': response.json(),
- }
- except json.JSONDecodeError:
- return {
- 'error': 'Response is not in JSON format',
- 'response': response.text,
- }
- try:
- user_ids = [e["invitation"]["entityUrn"] for e in elements]
- except KeyError:
- return {
- 'error': 'Response does not contain the expected data',
- 'response': response.json(),
- }
- return {
- 'elements': elements,
- 'user_ids': user_ids
- }
- result = get_sent_connection_requests(0, 100)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement