|
5 | 5 | import os
|
6 | 6 | import time
|
7 | 7 |
|
8 |
| -def get_jsoned(url): |
9 |
| - """ |
10 |
| -
|
11 |
| - :param url: |
12 |
| - :return: json file containing the matadata |
13 |
| - """ |
14 |
| - art = Article(url) |
15 |
| - art.download() |
16 |
| - art.parse() |
17 |
| - soup = BeautifulSoup(art.html) |
18 |
| - for div in soup.findAll('script'): |
19 |
| - if "window.__INIT_PROPS__" in div.text: |
20 |
| - text = div.text[24:] |
21 |
| - text = text.replace('\n', '') |
22 |
| - return json.loads(text) |
23 |
| - |
24 |
| -def get_user_info(name, from_video): |
25 |
| - """ |
26 |
| - :param name of user: |
27 |
| - :return: user Data |
28 |
| - """ |
29 |
| - jsoned = '' |
30 |
| - if from_video: |
31 |
| - start_index = name.find('/video/') |
32 |
| - print(name[:start_index]) |
33 |
| - jsoned = get_jsoned(name[:start_index]) |
34 |
| - else: |
35 |
| - jsoned = get_jsoned(name) |
36 |
| - try: |
37 |
| - return jsoned['/@:uniqueId']['userData'] |
38 |
| - except Exception as E: |
39 |
| - return {} |
40 |
| - |
41 |
| - |
42 |
| -def get_user_videos(name): |
43 |
| - url = 'https://www.tiktok.com/@' + name |
44 |
| - jsoned = get_jsoned(url) |
45 |
| - return jsoned['/@:uniqueId']['itemList'] |
46 |
| - |
47 |
| - |
48 |
| -def get_trending_hashtags(): |
49 |
| - url = 'https://www.tiktok.com/trending' |
50 |
| - jsoned = get_jsoned(url) |
51 |
| - return jsoned['/trending']['challengeList'] |
52 |
| - |
53 |
| - |
54 |
| -def get_trending_posts(): |
55 |
| - url = 'https://www.tiktok.com/trending' |
56 |
| - jsoned = get_jsoned(url) |
57 |
| - return jsoned['/trending']['itemList'] |
58 |
| - |
59 |
| - |
60 |
| -def get_video_info(url): |
61 |
| - jsoned = get_jsoned(url) |
62 |
| - try: |
63 |
| - temp = jsoned['/@:uniqueId/video/:id']['videoData'] |
64 |
| - temp['url'] = url |
65 |
| - return temp |
66 |
| - except Exception as identifier: |
67 |
| - print(identifier) |
68 |
| - return {} |
69 |
| - |
70 |
| - |
71 |
| -def get_hash_recommend_list(name): |
72 |
| - url = 'https://www.tiktok.com/tag/' + name |
73 |
| - jsoned = get_jsoned(url) |
74 |
| - return jsoned['/tag/:name']['recommendList'] |
75 |
| - |
76 |
| - |
77 |
| -def get_hash_videos(name): |
78 |
| - url = 'https://www.tiktok.com/tag/' + name |
79 |
| - jsoned = get_jsoned(url) |
80 |
| - return jsoned['/tag/:name']['itemList'] |
81 |
| - |
82 |
| - |
83 |
| -def get_videos_from_music(music_id, name): |
84 |
| - url = 'https://www.tiktok.com/music/'+name.replace(' ', '-') + music_id |
85 |
| - jsoned = get_jsoned(url) |
86 |
| - return jsoned['/music/*-:id']['itemList'] |
87 |
| - |
88 |
| - |
89 |
| -def get_music_recommend_list(music_id, name): |
90 |
| - url = 'https://www.tiktok.com/music' + name.replace(' ', '-') + music_id |
91 |
| - jsoned = get_jsoned(url) |
92 |
| - return jsoned['/music/*-:id']['recommendList'] |
93 |
| - |
94 |
| - |
95 |
| -def download_video(url, post_name=None): |
96 |
| - r = requests.get(url, stream=True) |
97 |
| - if post_name: |
98 |
| - with open(post_name, 'wb') as f: |
| 8 | +class API(): |
| 9 | + def __init__(self): |
| 10 | + pass |
| 11 | + |
| 12 | + def get_jsoned(self, url): |
| 13 | + """ |
| 14 | +
|
| 15 | + :param url: |
| 16 | + :return: json file containing the matadata |
| 17 | + """ |
| 18 | + art = Article(url) |
| 19 | + art.download() |
| 20 | + art.parse() |
| 21 | + soup = BeautifulSoup(art.html, features="lxml") |
| 22 | + for div in soup.findAll('script'): |
| 23 | + if "window.__INIT_PROPS__" in div.text: |
| 24 | + text = div.text[24:] |
| 25 | + text = text.replace('\n', '') |
| 26 | + return json.loads(text) |
| 27 | + |
| 28 | + def get_user_info(self, name, from_video): |
| 29 | + """ |
| 30 | + :param name of user: |
| 31 | + :return: user Data |
| 32 | + """ |
| 33 | + jsoned = '' |
| 34 | + if from_video: |
| 35 | + start_index = name.find('/video/') |
| 36 | + print(name[:start_index]) |
| 37 | + jsoned = get_jsoned(name[:start_index]) |
| 38 | + else: |
| 39 | + jsoned = get_jsoned(name) |
| 40 | + try: |
| 41 | + return jsoned['/@:uniqueId']['userData'] |
| 42 | + except Exception as E: |
| 43 | + return {} |
| 44 | + |
| 45 | + |
| 46 | + def get_user_videos(self, name): |
| 47 | + url = 'https://www.tiktok.com/@' + name |
| 48 | + jsoned = get_jsoned(url) |
| 49 | + return jsoned['/@:uniqueId']['itemList'] |
| 50 | + |
| 51 | + |
| 52 | + def get_trending_hashtags(self): |
| 53 | + url = 'https://www.tiktok.com/trending' |
| 54 | + jsoned = get_jsoned(url) |
| 55 | + return jsoned['/trending']['challengeList'] |
| 56 | + |
| 57 | + |
| 58 | + def get_trending_posts(self): |
| 59 | + url = 'https://www.tiktok.com/trending' |
| 60 | + jsoned = get_jsoned(url) |
| 61 | + return jsoned['/trending']['itemList'] |
| 62 | + |
| 63 | + |
| 64 | + def get_video_info(self, url): |
| 65 | + jsoned = get_jsoned(url) |
| 66 | + try: |
| 67 | + temp = jsoned['/@:uniqueId/video/:id']['videoData'] |
| 68 | + temp['url'] = url |
| 69 | + return temp |
| 70 | + except Exception as identifier: |
| 71 | + print(identifier) |
| 72 | + return {} |
| 73 | + |
| 74 | + |
| 75 | + def get_hash_recommend_list(self, name): |
| 76 | + url = 'https://www.tiktok.com/tag/' + name |
| 77 | + jsoned = get_jsoned(url) |
| 78 | + return jsoned['/tag/:name']['recommendList'] |
| 79 | + |
| 80 | + |
| 81 | + def get_hash_videos(self, name): |
| 82 | + url = 'https://www.tiktok.com/tag/' + name |
| 83 | + jsoned = get_jsoned(url) |
| 84 | + return jsoned['/tag/:name']['itemList'] |
| 85 | + |
| 86 | + |
| 87 | + def get_videos_from_music(self, music_id, name): |
| 88 | + url = 'https://www.tiktok.com/music/'+name.replace(' ', '-') + music_id |
| 89 | + jsoned = get_jsoned(url) |
| 90 | + return jsoned['/music/*-:id']['itemList'] |
| 91 | + |
| 92 | + |
| 93 | + def get_music_recommend_list(self, music_id, name): |
| 94 | + url = 'https://www.tiktok.com/music' + name.replace(' ', '-') + music_id |
| 95 | + jsoned = get_jsoned(url) |
| 96 | + return jsoned['/music/*-:id']['recommendList'] |
| 97 | + |
| 98 | + |
| 99 | + def download_video(self, url, post_name=None): |
| 100 | + r = requests.get(url, stream=True) |
| 101 | + if post_name: |
| 102 | + with open(post_name, 'wb') as f: |
| 103 | + for chunk in r.iter_content(chunk_size=1024 * 1024): |
| 104 | + if chunk: |
| 105 | + f.write(chunk) |
| 106 | + else: |
| 107 | + return None |
| 108 | + else: |
99 | 109 | for chunk in r.iter_content(chunk_size=1024 * 1024):
|
100 | 110 | if chunk:
|
101 |
| - f.write(chunk) |
| 111 | + return chunk |
102 | 112 | else:
|
103 | 113 | return None
|
104 |
| - else: |
105 |
| - for chunk in r.iter_content(chunk_size=1024 * 1024): |
106 |
| - if chunk: |
107 |
| - return chunk |
108 |
| - else: |
109 |
| - return None |
110 | 114 |
|
111 | 115 |
|
0 commit comments