-
Notifications
You must be signed in to change notification settings - Fork 8
/
TikTok.py
61 lines (54 loc) · 1.58 KB
/
TikTok.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
from TikTokApi import *
import time, sys, os
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
import zipfile
def zipdir(path, ziph):
# Fucntion to Zip the folder
for root, dirs, files in os.walk(path):
for file in files:
ziph.write(os.path.join(root, file))
def gdrive_upload(str):
g_login = GoogleAuth()
g_login.LocalWebserverAuth()
drive = GoogleDrive(g_login)
file2 = drive.CreateFile()
file2.SetContentFile(str)
file2.Upload()
api = TikTokApi()
results = 1900
vid=""
username=""
trending=""
if(sys.argv[1]=="u"):
username=sys.argv[2]
trending = api.byUsername(username,results)
elif(sys.argv[1]=="h"):
username=sys.argv[2]
trending = api.byHashtag(username,results)
if(not os.path.exists(username)):
os.mkdir(username)
videoname="_video.mp4"
count=0
for tiktok in trending:
# Prints the text of the tiktok
count=count+1
if count%10==0:
print("Please wait for 5 sec to continue....")
time.sleep(5)
if(sys.argv[1]=="u"):
vid=tiktok['id']
elif(sys.argv[1]=="h"):
vid=tiktok['itemInfos']['id']
url='https://www.tiktok.com/@'+username+'/video/'+vid+'?lang=en'
print("Downloaded: " + str(count))
# Below is used if you want no watermarks
tiktokData = api.get_Video_No_Watermark(url,return_bytes=1)
with open(username+"/"+vid+"_"+username+videoname,'wb') as out:
out.write(tiktokData)
#print(api.getUserObject(username))
print(len(trending))
zipf = zipfile.ZipFile(username + '.zip', 'w', zipfile.ZIP_DEFLATED)
zipdir(username+"/", zipf)
zipf.close()
gdrive_upload(username + '.zip')