-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunconnect_bulk.py
executable file
·163 lines (119 loc) · 4.62 KB
/
unconnect_bulk.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from pathlib import Path
from time import sleep
import requests
import concurrent.futures
from utils import write_to_excel
import pandas as pd
def extract_connections(first_page=0, last_page=20):
def process(i):
raw = get_page_raw_data(i)
return extract_page_data(raw)
all_data = []
with concurrent.futures.ThreadPoolExecutor() as executor:
for result in executor.map(process, range(first_page, last_page + 1)):
all_data.extend(result)
write_to_excel(all_data)
def extract_page_data(raw_json):
page_data = {}
print("extracting data from each item ...")
print("items_done", end=": ")
raw_data = raw_json.get("included", None)
for j, item_raw in enumerate(raw_data):
urn = item_raw.get("entityUrn", None).replace(
"urn:li:fsd_profile:", "").replace("urn:li:fsd_connection:", "")
try:
item = page_data[urn]
except Exception:
item = dict()
fname = item_raw.get("firstName", None)
lname = item_raw.get("lastName", None)
headline = item_raw.get("headline", None)
created_at = item_raw.get("createdAt", None)
public_id = item_raw.get("publicIdentifier", None)
if urn:
item["urn"] = urn
if fname:
item["firstName"] = fname
if lname:
item["lastName"] = lname
if headline:
item["headline"] = headline
if created_at:
item["createdAt"] = created_at
if public_id:
item["publicIdentifier"] = public_id
page_data[urn] = item
print("all done!\n\n")
return page_data.values()
def get_page_raw_data(page, cookies, headers):
print(f"{page} ...", end=" ")
start = page * 40
params = {
'decorationId': 'com.linkedin.voyager.dash.deco.web.mynetwork.ConnectionListWithProfile-15',
'count': '40',
'q': 'search',
'sortType': 'RECENTLY_ADDED',
'start': start,
}
response = requests.get('https://www.linkedin.com/voyager/api/relationships/dash/connections',
params=params, cookies=cookies, headers=headers, timeout=10)
print("done", "--"*10, "\n")
return response.json()
def remove_connections(count, path, cookies, headers):
def process(row):
print("removing ", row.firstName)
params = {
'action': 'removeFromMyConnections',
}
json_data = {
'connectedMember': f'urn:li:fsd_profile:{row.urn}',
}
_ = requests.post('https://www.linkedin.com/voyager/api/relationships/dash/connections',
params=params, cookies=cookies, headers=headers, json=json_data, timeout=10)
print("removing ", row.firstName, " done!")
df = pd.read_excel(path)[:count]
print("\n\nwill remove following people:\n")
for row in df.itertuples(index=True, name='connections'):
print(row.firstName, row.lastName)
inp = input("proceed???:(y/N)")
if inp != "y":
return
with concurrent.futures.ThreadPoolExecutor() as executor:
for result in executor.map(process, df.itertuples(index=True, name='connections')):
# all_data.extend(result)
pass
txt = """enter 1 for extracting connections and making excel file
enter 2 for unconnecting some of your connections
enter 3 for deleting connections excel file (!! Danger !!)
enter 0 to quite..
"""
def main():
while True:
try:
print(txt)
inp = int(input("what to do?:"))
if inp == 1:
print(f"starting up... total pages: {total_pages}")
make_excel_file()
extract_connections(0, total_pages)
# sort_connections_file_by_oldest_to_new()
print("\n\nfinish..")
elif inp == 2:
excel_file = Path(EXCEL_FILE_PATH)
if not excel_file.is_file():
make_excel_file()
extract_connections(0, total_pages)
count = int(input("How much?:"))
remove_connections(count)
sleep(10)
make_excel_file()
extract_connections(0, total_pages)
elif inp == 3:
make_excel_file()
elif inp == 0:
break
except TypeError as e:
print("\n\n\n\nbad input... try again..\n\n", e)
print("Bye Bye!")
if __name__ == '__main__':
main()