Repository navigation
Expand file tree
/
Copy pathTikAPI.py
More file actions
111 lines (87 loc) · 3.11 KB
/
Copy pathTikAPI.py
File metadata and controls
111 lines (87 loc) · 3.11 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
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
from newspaper import Article
from bs4 import BeautifulSoup
import requests
import json
import os
import time
class API():
def __init__(self):
pass
def get_jsoned(self, url):
"""
:param url:
:return: json file containing the matadata
"""
art = Article(url)
art.download()
art.parse()
soup = BeautifulSoup(art.html, features="lxml")
for div in soup.findAll('script'):
if "window.__INIT_PROPS__" in div.text:
text = div.text[24:]
text = text.replace('\n', '')
return json.loads(text)
def get_user_info(self, name):
"""
:param name of user:
:return: user Data
"""
url = 'https://www.tiktok.com/@' + name
jsoned = ''
try:
jsoned = self.get_jsoned(url)
return jsoned['/@:uniqueId']['userData']
except Exception as E:
return {}
def get_user_videos(self, name):
url = 'https://www.tiktok.com/@' + name
jsoned = self.get_jsoned(url)
return jsoned['/@:uniqueId']['itemList']
def get_trending_hashtags(self):
url = 'https://www.tiktok.com/trending'
jsoned = self.get_jsoned(url)
return jsoned['/trending']['challengeList']
def get_trending_posts(self):
url = 'https://www.tiktok.com/trending'
jsoned = self.get_jsoned(url)
return jsoned['/trending']['itemList']
def get_video_info(self, url):
jsoned = self.get_jsoned(url)
try:
temp = jsoned['/@:uniqueId/video/:id']['videoData']
temp['url'] = url
return temp
except Exception as identifier:
print(identifier)
return {}
def get_hash_recommend_list(self, hashtag):
url = 'https://www.tiktok.com/tag/' + hashtag
jsoned = self.get_jsoned(url)
return jsoned['/tag/:name']['recommendList']
def get_hash_videos(self, hashtag):
url = 'https://www.tiktok.com/tag/' + hashtag
jsoned = get_jsoned(url)
return jsoned['/tag/:name']['itemList']
def get_videos_from_music(self, music_id, name):
url = 'https://www.tiktok.com/music/'+name.replace(' ', '-') + music_id
jsoned = self.get_jsoned(url)
return jsoned['/music/*-:id']['itemList']
def get_music_recommend_list(self, music_id, name):
url = 'https://www.tiktok.com/music' + name.replace(' ', '-') + music_id
jsoned = self.get_jsoned(url)
return jsoned['/music/*-:id']['recommendList']
def download_video(self, url, post_name=None):
r = requests.get(url, stream=True)
if post_name:
with open(post_name, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024 * 1024):
if chunk:
f.write(chunk)
else:
return None
else:
for chunk in r.iter_content(chunk_size=1024 * 1024):
if chunk:
return chunk
else:
return None