Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def parse_users_search_response(api_result):
- def parse():
- total = api_result["data"]["searchDashClustersByAll"]["paging"]["total"]
- if total == 0:
- return {"users": []}
- for element in api_result["data"]["searchDashClustersByAll"]["elements"]:
- if element["items"][0]["item"]["entityResult"] is not None:
- return {"users": element["items"]}
- return {
- "error": "Response does not have proper structure (none of the entityResult is not None)",
- "response": api_result
- }
- try:
- return parse()
- except (KeyError, IndexError) as e:
- return {
- "error": "Response does not have proper structure",
- "response": api_result,
- "exception": f"{e.__class__.__name__}: {e}"
- }
- def make_url(keywords, params, query_id):
- params.append(
- {"resultType": ["PEOPLE"]},
- )
- s = "List("
- for item in params:
- key = list(item.keys())[0]
- value = item[key]
- s += f"(key:{key},value:List({','.join(value)})),"
- s = s[:-1] + ")"
- keywords = f"keywords:{keywords}," if keywords else ""
- url = (
- f'https://www.linkedin.com/voyager/api/graphql?variables=(query:('
- f'{keywords}flagshipSearchIntent:SEARCH_SRP,queryParameters:{s}))'
- f'&&queryId=voyagerSearchDashClusters.{query_id}'
- )
- return url
- def find_users(params_list=None, keywords=""):
- if params_list is None:
- params_list = []
- query_id_result = get_graphql_query_id(
- 'https://www.linkedin.com/search/results/people/',
- 'voyagerSearchDashClusters'
- )
- if "error" in query_id_result:
- return query_id_result
- query_id = query_id_result["query_id"]
- url = make_url(keywords, params_list, query_id)
- response = requests.get(url, headers=headers, cookies=cookies)
- if response.status_code != 200:
- return {
- "error": "Response status code is not 200 for /voyager/api/graphql",
- "response": response
- }
- return parse_users_search_response(response.json())
- params = [
- {"currentCompany": ["72111389"]},
- {"geoUrn": ["102264497"]},
- {"network": ["S", "O"]},
- {"talksAbout": ["marketing"]}
- ]
- keywords = "max"
- users = find_users(params, keywords)
- print(json.dumps(users, indent=4, sort_keys=True))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement