Advertisement
kopyl

Untitled

Aug 15th, 2023 (edited)
1,077
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.40 KB | None | 0 0
  1. def parse_users_search_response(api_result):
  2.     def parse():
  3.         total = api_result["data"]["searchDashClustersByAll"]["paging"]["total"]
  4.         if total == 0:
  5.             return {"users": []}
  6.  
  7.         for element in api_result["data"]["searchDashClustersByAll"]["elements"]:
  8.             if element["items"][0]["item"]["entityResult"] is not None:
  9.                 return {"users": element["items"]}
  10.         return {
  11.             "error": "Response does not have proper structure (none of the entityResult is not None)",
  12.             "response": api_result
  13.         }
  14.    
  15.     try:
  16.         return parse()
  17.     except (KeyError, IndexError) as e:
  18.         return {
  19.             "error": "Response does not have proper structure",
  20.             "response": api_result,
  21.             "exception": f"{e.__class__.__name__}: {e}"
  22.         }
  23.    
  24.  
  25. def make_url(keywords, params, query_id):
  26.     params.append(
  27.         {"resultType": ["PEOPLE"]},
  28.     )
  29.     s = "List("
  30.     for item in params:
  31.         key = list(item.keys())[0]
  32.         value = item[key]
  33.         s += f"(key:{key},value:List({','.join(value)})),"
  34.     s = s[:-1] + ")"
  35.     keywords = f"keywords:{keywords}," if keywords else ""
  36.     url = (
  37.         f'https://www.linkedin.com/voyager/api/graphql?variables=(query:('
  38.         f'{keywords}flagshipSearchIntent:SEARCH_SRP,queryParameters:{s}))'
  39.         f'&&queryId=voyagerSearchDashClusters.{query_id}'
  40.     )
  41.     return url
  42.  
  43.  
  44. def find_users(params_list=None, keywords=""):
  45.     if params_list is None:
  46.         params_list = []
  47.     query_id_result = get_graphql_query_id(
  48.         'https://www.linkedin.com/search/results/people/',
  49.         'voyagerSearchDashClusters'
  50.     )
  51.     if "error" in query_id_result:
  52.         return query_id_result
  53.    
  54.     query_id = query_id_result["query_id"]
  55.     url = make_url(keywords, params_list, query_id)
  56.     response = requests.get(url, headers=headers, cookies=cookies)
  57.     if response.status_code != 200:
  58.         return {
  59.             "error": "Response status code is not 200 for /voyager/api/graphql",
  60.             "response": response
  61.         }
  62.     return parse_users_search_response(response.json())
  63.  
  64.  
  65. params = [
  66.     {"currentCompany": ["72111389"]},
  67.     {"geoUrn": ["102264497"]},
  68.     {"network": ["S", "O"]},
  69.     {"talksAbout": ["marketing"]}
  70. ]
  71. keywords = "max"
  72.  
  73. users = find_users(params, keywords)
  74. print(json.dumps(users, indent=4, sort_keys=True))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement