/
tikTokScraper.py
135 lines (109 loc) · 5.13 KB
/
tikTokScraper.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
import requests
import base64
from urllib.parse import parse_qs
from urllib.parse import urlparse
from bs4 import BeautifulSoup
def b64decode(str):
str = str.replace('_','/')
str += "=" * ((4 - len(str) % 4) % 4)
return base64.b64decode(str)
headers = {
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:92.0) Gecko/20100101 Firefox/92.0',
}
tikTokDomains = (
'http://vt.tiktok.com', 'http://app-va.tiktokv.com', 'http://vm.tiktok.com', 'http://m.tiktok.com', 'http://tiktok.com', 'http://www.tiktok.com', 'http://link.e.tiktok.com', 'http://us.tiktok.com',
'https://vt.tiktok.com', 'https://app-va.tiktokv.com', 'https://vm.tiktok.com', 'https://m.tiktok.com', 'https://tiktok.com', 'https://www.tiktok.com', 'https://link.e.tiktok.com', 'https://us.tiktok.com'
)
def getToken(url):
try:
response = requests.post('https://musicaldown.com/', headers=headers)
cookies = response.cookies
soup = BeautifulSoup(response.content, 'html.parser').find_all('input')
data = {
soup[0].get('name'):url,
soup[1].get('name'):soup[1].get('value'),
soup[2].get('name'):soup[2].get('value')
}
return True, cookies, data
except Exception:
return None, None, None
def getVideo(url):
if not url.startswith('http'):
url = 'https://' + url
if url.lower().startswith(tikTokDomains):
url = url.split('?')[0]
status, cookies, data = getToken(url)
if status:
headers = {
'Cookie': f"session_data={cookies['session_data']}",
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:92.0) Gecko/20100101 Firefox/92.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate',
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': '96',
'Origin': 'https://musicaldown.com',
'Referer': 'https://musicaldown.com/en/',
'Upgrade-Insecure-Requests': '1',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-User': '?1',
'Te': 'trailers'
}
try:
response = requests.post('https://musicaldown.com/download', data=data, headers=headers, allow_redirects=False)
if 'location' in response.headers:
if response.headers['location'] == '/en/?err=url invalid!':
return {
'success': False,
'error': 'invalidUrl'
}
elif response.headers['location'] == '/en/?err=Video is private!':
return {
'success': False,
'error': 'privateVideo'
}
elif response.headers['location'] == '/mp3/download':
response = requests.post('https://musicaldown.com//mp3/download', data=data, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')
link = soup.findAll('a',attrs={'class':'btn waves-effect waves-light orange'})[3]['href']
return {
'success': True,
'type': 'audio',
'description': soup.findAll('h2', attrs={'class':'white-text'})[0].get_text()[13:],
'thumbnail': None,
'link': b64decode(parse_qs(urlparse(link).query)['url'][0]).decode(),
'url': url
}
else:
return {
'success': False,
'error': 'unknownError'
}
else:
soup = BeautifulSoup(response.content, 'html.parser')
link = soup.findAll('a',attrs={'class':'btn waves-effect waves-light orange'})[3]['href']
return {
'success': True,
'type': 'video',
'description': soup.findAll('h2', attrs={'class':'white-text'})[0].get_text()[23:-19],
'thumbnail': soup.findAll('img',attrs={'class':'responsive-img'})[0]['src'],
'link': b64decode(parse_qs(urlparse(link).query)['url'][0]).decode(),
'url': url
}
except Exception as e:
return {
'success': False,
'error': 'exception'
}
else:
return {
'success': False,
'error': 'exception'
}
else:
return {
'success': False,
'error': 'invalidUrl'
}