-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_user_index_data.py
executable file
·40 lines (32 loc) · 1.11 KB
/
generate_user_index_data.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
import requests
import json
from multiprocessing import Pool
def get_user_index(username:str):
"""
input: username
output: id(index) of the user, which is required later to send request to user page
"""
url = "https://dreamhack.io/api/v1/services/suggestion/"
params = {"keyword": username, "section": ""}
result = requests.get(url, params=params)
data = json.loads(result.text)
print(username)
for item in data["users"]["results"]:
if item["nickname"] == username:
return item["id"]
def run():
file = open("username_list.txt", "r")
result_data = {}
lines = file.readlines()
user_count = len(lines)
usernames = list(map(lambda line: line.split("\n")[0], lines))
with Pool(user_count) as p:
user_indexes = p.map(get_user_index, usernames)
for i in range(user_count):
result_data[usernames[i]] = user_indexes[i]
file.close()
user_index_file = open("user_index.json", "w")
user_index_file.write(json.dumps(result_data, indent=2, ensure_ascii=False))
user_index_file.close()
if __name__=="__main__":
run()