-
Notifications
You must be signed in to change notification settings - Fork 1
/
ttdownloader.py
68 lines (55 loc) · 2.07 KB
/
ttdownloader.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
import aiohttp
from bs4 import BeautifulSoup
import asyncio
import aiofiles
url = "https://www.tiktok.com/@thaovy1590/video/7164747239729024282"
async def tt_get_link(url):
async with aiohttp.ClientSession() as session:
async with session.get('https://ttdownloader.com/') as response:
cookie = response.headers['Set-Cookie']
soup = BeautifulSoup(await response.text(), 'html.parser')
token = soup.find('input', {'id': 'token'})['value']
data_post = {
'url': url,
'format': '',
'token': token
}
async with session.post('https://ttdownloader.com/search/', headers={
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
'origin': 'https://ttdownloader.com',
'referer': 'https://ttdownloader.com/',
'cookie': cookie
}, data=data_post) as res:
soup2 = BeautifulSoup(await res.text(), 'html.parser')
if soup2 is not None:
nowm_class = soup2.find(
'a', {'class': 'download-link'})
if nowm_class is not None:
nowm = nowm_class['href']
return {
'nowm': nowm,
}
print(url, 'soup none')
print(nowm_class)
print(res.text())
return None
else:
return None
async def download(id, data, path):
async with aiohttp.ClientSession() as session:
if data is not None:
try:
async with session.get(data['nowm']) as res:
f = await aiofiles.open(f"{path}/{id}.mp4", mode="wb")
await f.write(await res.read())
await f.close()
except:
print('error')
else:
print(id, "error")
async def main():
data = await tt_get_link(url)
await download(11135, data)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())