-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsource-to-csv.sh
More file actions
48 lines (39 loc) · 1.99 KB
/
Copy pathsource-to-csv.sh
File metadata and controls
48 lines (39 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env bash
# Search LinkedIn, deduplicate, drop existing connections, write a CSV.
#
# ./source-to-csv.sh "CTO fintech London" out.csv [count]
#
# No writes. Spends search quota and nothing else.
. "$(dirname "$0")/lib.sh"
KEYWORDS="${1:?usage: ./source-to-csv.sh \"<keywords>\" <out.csv> [count]}"
OUT="${2:?usage: ./source-to-csv.sh \"<keywords>\" <out.csv> [count]}"
COUNT="${3:-50}"
require_ready
echo "searching: $KEYWORDS" >&2
profiles="$(act search.people "$(jq -nc --arg k "$KEYWORDS" --argjson c "$COUNT" \
'{keywords:$k, source:"search", start:0, count:$c}')" | jq '.profiles')"
# Page until we have COUNT or the results run out. nextStart is null when there is no more.
start="$(jq 'length' <<<"$profiles")"
while [ "$(jq 'length' <<<"$profiles")" -lt "$COUNT" ] && [ "$start" -gt 0 ]; do
page="$(act search.people "$(jq -nc --arg k "$KEYWORDS" --argjson s "$start" --argjson c "$COUNT" \
'{keywords:$k, source:"search", start:$s, count:$c}')")"
new="$(jq '.profiles' <<<"$page")"
[ "$(jq 'length' <<<"$new")" -gt 0 ] || break
profiles="$(jq -s 'add' <(echo "$profiles") <(echo "$new"))"
start="$(jq -r '.nextStart // 0' <<<"$page")"
echo " $(jq 'length' <<<"$profiles") so far" >&2
done
# Deduplicate on publicId.
profiles="$(jq 'unique_by(.publicId)' <<<"$profiles")"
echo "deduplicated to $(jq 'length' <<<"$profiles")" >&2
# Drop anyone already connected or with an invite pending.
ids="$(jq -c '[.[].publicId]' <<<"$profiles")"
statuses="$(act network.status "$(jq -nc --argjson i "$ids" '{publicIds:$i}')" | jq '.statuses')"
profiles="$(jq --argjson s "$statuses" '[.[] | select($s[.publicId] == "none" or $s[.publicId] == null)]' <<<"$profiles")"
echo "$(jq 'length' <<<"$profiles") with no existing relationship" >&2
# CSV, quoted properly.
{
echo 'publicId,fullName,headline,title,company,location,url'
jq -r '.[] | [.publicId, .fullName, (.headline // ""), (.title // ""), (.company // ""), (.location // ""), .url] | @csv' <<<"$profiles"
} > "$OUT"
echo "wrote $OUT" >&2