Skip to content

Commit 8d6d805

Browse files
committed
Pasing cookie params in test
1 parent d9323a2 commit 8d6d805

File tree

8 files changed

+35
-15
lines changed

8 files changed

+35
-15
lines changed

TikTokAPI/tiktokapi.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@
99
class VideoException(Exception):
1010
pass
1111

12+
1213
class TikTokAPI(object):
1314

14-
def __init__(self, cookie={}, language='en', browser_lang="en-US", timezone="Asia/Kolkata", region='IN'):
15+
def __init__(self, cookie=None, language='en', browser_lang="en-US", timezone="Asia/Kolkata", region='IN'):
1516
self.base_url = "https://t.tiktok.com/api"
1617
self.user_agent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:79.0) Gecko/20100101 Firefox/79.0"
1718

19+
if cookie is None:
20+
cookie = {}
1821
self.verifyFp = cookie.get("s_v_web_id", "verify_kjf974fd_y7bupmR0_3uRm_43kF_Awde_8K95qt0GcpBk")
1922
self.tt_webid = cookie.get("tt_webid", "6913027209393473025")
2023

21-
2224
self.headers = {
2325
'Host': 't.tiktok.com',
2426
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:79.0) Gecko/20100101 Firefox/79.0',
@@ -134,7 +136,7 @@ def getVideosByUserName(self, user_name, count=30):
134136
for key, val in self.default_params.items():
135137
params[key] = val
136138
return self.send_get_request(url, params)
137-
139+
138140
def getLikesByUserName(self, user_name, count=30):
139141
user_data = self.getUserByName(user_name)
140142
user_obj = user_data["userInfo"]["user"]
@@ -249,7 +251,7 @@ def downloadVideoByIdNoWatermark(self, video_id, save_path):
249251
raise VideoException("Video without watermark not available in new videos")
250252
video_url_no_wm = "https://api2-16-h2.musical.ly/aweme/v1/play/?video_id={" \
251253
"}&vr_type=0&is_play_url=1&source=PackSourceEnum_PUBLISH&media_type=4" \
252-
.format(video_data[pos+4:pos+36])
254+
.format(video_data[pos + 4:pos + 36])
253255

254256
video_data_no_wm = get_req_content(video_url_no_wm, params=None, headers=self.headers)
255257
with open(save_path, 'wb') as f:

tests/cookie.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"s_v_web_id": "verify_kjf974fd_y7bupmR0_3uRm_43kF_Awde_8K95qt0GcpBk",
3+
"tt_webid": "6913027209393473025"
4+
}

tests/test_hashtag.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import argparse
22
from TikTokAPI import TikTokAPI
3+
from utils import read_json_from_file
34

45

56
def getHashTag(hashTag):
6-
api = TikTokAPI()
7+
api = TikTokAPI(read_json_from_file("cookie.json"))
78
return api.getHashTag(hashTag)
89

910

1011
def getVideosByHashTag(hashTag):
11-
api = TikTokAPI()
12+
api = TikTokAPI(read_json_from_file("cookie.json"))
1213
return api.getVideosByHashTag(hashTag, count=10)
1314

1415

tests/test_music.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import argparse
22
from TikTokAPI import TikTokAPI
3+
from utils import read_json_from_file
34

45

56
def getMusic(music_id):
6-
api = TikTokAPI()
7+
api = TikTokAPI(read_json_from_file("cookie.json"))
78
return api.getMusic(music_id)
89

910

1011
def getVideosByMusic(music_id):
11-
api = TikTokAPI()
12+
api = TikTokAPI(read_json_from_file("cookie.json"))
1213
return api.getVideosByMusic(music_id, count=10)
1314

1415

tests/test_trending.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from TikTokAPI import TikTokAPI
2+
from utils import read_json_from_file
23

3-
api = TikTokAPI()
4+
api = TikTokAPI(read_json_from_file("cookie.json"))
45
retval = api.getTrending(count=5)
56
print("Trending Videos")
67
print(retval)

tests/test_user.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import argparse
22
from TikTokAPI import TikTokAPI
3+
from utils import read_json_from_file
34

45

56
def getUser(user_name):
6-
api = TikTokAPI()
7+
api = TikTokAPI(read_json_from_file("cookie.json"))
78
return api.getUserByName(user_name)
89

910

1011
def getVideosByUserName(user_name):
11-
api = TikTokAPI()
12+
api = TikTokAPI(read_json_from_file("cookie.json"))
1213
return api.getVideosByUserName(user_name, count=1)
1314

1415

1516
def getLikesByUserName(user_name):
16-
api = TikTokAPI()
17+
api = TikTokAPI(read_json_from_file("cookie.json"))
1718
return api.getLikesByUserName(user_name, count=1)
1819

1920

tests/test_video.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import argparse
22
from TikTokAPI import TikTokAPI
3+
from utils import read_json_from_file
34

45

56
def getVideoById(video_id):
6-
api = TikTokAPI()
7+
api = TikTokAPI(read_json_from_file("cookie.json"))
78
return api.getVideoById(video_id)
89

910

1011
def downloadVideoById(video_id):
11-
api = TikTokAPI()
12+
api = TikTokAPI(read_json_from_file("cookie.json"))
1213
api.downloadVideoById(video_id, video_id+".mp4")
1314

1415

1516
def downloadVideoByIdNoWatermark(video_id):
16-
api = TikTokAPI()
17+
api = TikTokAPI(read_json_from_file("cookie.json"))
1718
api.downloadVideoByIdNoWatermark(video_id, video_id+"_no_wm.mp4")
1819

1920

tests/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import json
2+
3+
4+
def read_json_from_file(filepath):
5+
with open(filepath, 'r', encoding='utf-8') as json_file:
6+
data = json.load(json_file)
7+
if type(data) is str:
8+
return json.loads(data)
9+
return data

0 commit comments

Comments
 (0)