-
Notifications
You must be signed in to change notification settings - Fork 3
/
tiktok-user-list-data.py
70 lines (55 loc) · 1.84 KB
/
tiktok-user-list-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
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
import requests
import json
import sys
"""
GET PARAMETERS
---
(1) input file - should be a .txt list of usernames separated by new lines
(2) output file - should be .json
"""
USER_LIST_F = str(sys.argv[1])
OUTPUT_F = str(sys.argv[2])
"""
READ IN LIST OF USERS
"""
with open(USER_LIST_F) as f:
USER_LIST = [user.strip() for user in f]
"""
GET ACCESS TOKEN, DETERMINE FIELDS
"""
def get_access_token():
CLIENT_KEY = "PUT YOUR CLIENT KEY HERE" ## substitute with your client key
CLIENT_SECRET = "PUR YOU SECRET KEY HERE" ## substitute with your secret key
r = requests.post('https://open.tiktokapis.com/v2/oauth/token/',
headers={'Content-Type': 'application/x-www-form-urlencoded',
'Cache-Control': 'no-cache'
},
data={'client_key':CLIENT_KEY,
'client_secret':CLIENT_SECRET,
'grant_type':'client_credentials'})
ACCESS_TOKEN = r.json()['access_token']
return ACCESS_TOKEN
ACCESS_TOKEN = get_access_token()
ALL_FIELDS = 'display_name,bio_description,avatar_url,is_verified,follower_count,\
following_count,likes_count,video_count'
"""
COLLECT DATA
"""
DATA_LIST = []
for i, USER in enumerate(USER_LIST):
if i == 1000: # rate limit
print("rate limit is up! left off searching for:", USER)
break
try:
D = requests.post('https://open.tiktokapis.com/v2/research/user/info/?fields=%s'%ALL_FIELDS,
headers = {'authorization':'bearer '+ACCESS_TOKEN},
data = {'username':USER})
DATA_LIST.append(D.json())
except:
print("search for", USER, "didn't work! ...moving on!")
continue
"""
WRITE DATA TO FILE
"""
with open(OUTPUT_F, 'w') as f:
json.dump(DATA_LIST, f, indent=2)