Skip to content

Commit

Permalink
Handle missing key/value pairs better (#71)
Browse files Browse the repository at this point in the history
* Better handle missing keys

* Add back in length check

* Better checks on first and last name

* Fix indentation

* Fix indentation again lol

* Add back in missing statement

* adding another check
  • Loading branch information
initstring authored Sep 25, 2023
1 parent 1e184cc commit 200282c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
28 changes: 17 additions & 11 deletions linkedin2username.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,21 +499,27 @@ def find_employees(result):
# The "elements" list is the mini-profile you see when scrolling through a
# company's employees. It does not have all info on the person, like their
# entire job history. It only has some basics.
for body in result_json['elements']:
profile = (body['hitInfo']
['com.linkedin.voyager.search.SearchProfile']
['miniProfile'])
full_name = f"{profile['firstName']} {profile['lastName']}"
employee = {'full_name': full_name,
'occupation': profile['occupation']}

# Some employee names are not disclosed and return empty. We don't want those.
if len(employee['full_name']) > 1:
found_employees.append(employee)
found_employees = []
for body in result_json.get('elements', []):
profile = (
body.get('hitInfo', {})
.get('com.linkedin.voyager.search.SearchProfile', {})
.get('miniProfile', {})
)
first_name = profile.get('firstName', '').strip()
last_name = profile.get('lastName', '').strip()

# Dont include profiles that have only a single name
if first_name and last_name:
full_name = f"{first_name} {last_name}"
occupation = profile.get('occupation', "")
found_employees.append({'full_name': full_name, 'occupation': occupation})

return found_employees




def do_loops(session, company_id, outer_loops, args):
"""
Performs looping where the actual HTTP requests to scrape names occurs
Expand Down
25 changes: 25 additions & 0 deletions tests/test_linkedin2username.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
2: "John Davidson-Smith",
3: "John-Paul Smith-Robinson",
4: "José Gonzáles",
5: "🙂 Emoji Folks 🙂"
}


Expand All @@ -28,6 +29,10 @@ def test_f_last():
mutator = NameMutator(name)
assert mutator.f_last() == set(["jgonzales", ])

name = TEST_NAMES[5]
mutator = NameMutator(name)
assert mutator.f_last() == set(["efolks", ])


def test_f_dot_last():
name = TEST_NAMES[1]
Expand All @@ -46,6 +51,10 @@ def test_f_dot_last():
mutator = NameMutator(name)
assert mutator.f_dot_last() == set(["j.gonzales", ])

name = TEST_NAMES[5]
mutator = NameMutator(name)
assert mutator.f_dot_last() == set(["e.folks", ])


def test_last_f():
name = TEST_NAMES[1]
Expand All @@ -64,6 +73,10 @@ def test_last_f():
mutator = NameMutator(name)
assert mutator.last_f() == set(["gonzalesj", ])

name = TEST_NAMES[5]
mutator = NameMutator(name)
assert mutator.last_f() == set(["folkse", ])


def test_first_dot_last():
name = TEST_NAMES[1]
Expand All @@ -82,6 +95,10 @@ def test_first_dot_last():
mutator = NameMutator(name)
assert mutator.first_dot_last() == set(["jose.gonzales", ])

name = TEST_NAMES[5]
mutator = NameMutator(name)
assert mutator.first_dot_last() == set(["emoji.folks", ])


def test_first_l():
name = TEST_NAMES[1]
Expand All @@ -100,6 +117,10 @@ def test_first_l():
mutator = NameMutator(name)
assert mutator.first_l() == set(["joseg", ])

name = TEST_NAMES[5]
mutator = NameMutator(name)
assert mutator.first_l() == set(["emojif", ])


def test_first():
name = TEST_NAMES[1]
Expand All @@ -118,6 +139,10 @@ def test_first():
mutator = NameMutator(name)
assert mutator.first() == set(["jose", ])

name = TEST_NAMES[5]
mutator = NameMutator(name)
assert mutator.first() == set(["emoji", ])


def test_clean_name():
mutator = NameMutator("xxx")
Expand Down

0 comments on commit 200282c

Please sign in to comment.