Skip to content

Commit

Permalink
Merge pull request #534 from davidteather/nightly
Browse files Browse the repository at this point in the history
V3.9.4 - Better Download Handling & more did doc
  • Loading branch information
davidteather authored Mar 20, 2021
2 parents 265e750 + 7aa52b7 commit e1d6006
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 115 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ python -m examples.getTrending

## Documentation

You can find the documentation [here](https://davidteather.github.io/TikTok-Api/docs/tiktok), I will be making this documentation more complete overtime as it's not super great right now, but better than just having it in the readme!
You can find the documentation [here](https://davidteather.github.io/TikTok-Api/docs) (you'll likely just need the TikTokApi section of the docs), I will be making this documentation more complete overtime as it's not super great right now, but better than just having it in the readme!

## Built With

Expand Down
2 changes: 1 addition & 1 deletion TikTokApi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from TikTokApi.tiktok import TikTokApi
from TikTokApi.tiktokuser import TikTokUser
from TikTokApi.tiktokuser import TikTokUser
40 changes: 32 additions & 8 deletions TikTokApi/tiktok.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,19 @@ def __init__(self, **kwargs):
raise Exception("Only one TikTokApi object is allowed")
logging.basicConfig(level=kwargs.get("logging_level", logging.WARNING))
logging.info("Class initalized")
self.executablePath = kwargs.get("executablePath", None)

# Some Instance Vars
self.executablePath = kwargs.get("executablePath", None)
self.custom_did = kwargs.get("custom_did", None)
self.userAgent = (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/86.0.4240.111 Safari/537.36"
)
self.proxy = kwargs.get("proxy", None)
self.custom_verifyFp = kwargs.get("custom_verifyFp")
self.signer_url = kwargs.get("external_signer", None)
self.request_delay = kwargs.get("request_delay", None)

if kwargs.get("use_test_endpoints", False):
global BASE_URL
Expand All @@ -47,7 +51,9 @@ def __init__(self, **kwargs):
else:
from .browser import browser

self.signer_url = kwargs.get("external_signer", None)
if kwargs.get("generate_static_did", False):
self.custom_did = "".join(random.choice(string.digits) for num in range(19))

if self.signer_url is None:
self.browser = browser(**kwargs)
self.userAgent = self.browser.userAgent
Expand All @@ -67,6 +73,7 @@ def __init__(self, **kwargs):
self.width = self.browser.width
self.height = self.browser.height
except Exception as e:
logging.error(e)
logging.warning(
"An error ocurred while opening your browser but it was ignored."
)
Expand All @@ -79,8 +86,6 @@ def __init__(self, **kwargs):
self.width = "1920"
self.height = "1080"

self.request_delay = kwargs.get("request_delay", None)

@staticmethod
def get_instance(**kwargs):
"""The TikTokApi class. Used to interact with TikTok. This is a singleton
Expand All @@ -101,13 +106,19 @@ class to prevent issues from arising with playwright
All the methods take this as a optional parameter, however it's cleaner code
to store this at the instance level. You can override this at the specific
methods.
generate_static_did: A parameter that generates a custom_did at the instance level
Use this if you want to download videos from a script but don't want to generate
your own custom_did parameter.
custom_verifyFp: A TikTok parameter needed to work most of the time, optional
To get this parameter look at [this video](https://youtu.be/zwLmLfVI-VQ?t=117)
I recommend watching the entire thing, as it will help setup this package.
All the methods take this as a optional parameter, however it's cleaner code
to store this at the instance level. You can override this at the specific
methods.
You can use the following to generate `"".join(random.choice(string.digits)
for num in range(19))`
use_test_endpoints: Send requests to TikTok's test endpoints, optional
This parameter when set to true will make requests to TikTok's testing
endpoints instead of the live site. I can't guarantee this will work
Expand Down Expand Up @@ -1417,7 +1428,12 @@ def get_suggested_music_id_crawler(
return musics[:count]

def get_video_by_tiktok(self, data, **kwargs) -> bytes:
"""Downloads video from TikTok using a TikTok object
"""Downloads video from TikTok using a TikTok object.
You will need to set a custom_did to do this for anything but trending.
To do this, this is pretty simple you can either generate one yourself or,
you can pass the generate_static_did=True into the constructor of the
TikTokApi class.
Parameters
----------
Expand Down Expand Up @@ -1588,6 +1604,10 @@ def get_secuid(self, username, **kwargs):
"Retrieving the user secUid failed. Likely due to TikTok wanting captcha validation. Try to use a proxy."
)

def generate_did(self):
"""Generates a valid did for other methods. Pass this as the custom_did field to download videos"""
return "".join(random.choice(string.digits) for num in range(19))

#
# PRIVATE METHODS
#
Expand Down Expand Up @@ -1645,10 +1665,14 @@ def __process_kwargs__(self, kwargs):
language = kwargs.get("language", "en")
proxy = kwargs.get("proxy", None)
maxCount = kwargs.get("maxCount", 35)
did = kwargs.get(
"custom_did", "".join(random.choice(string.digits) for num in range(19))
)

if kwargs.get("custom_did", None) != None:
did = kwargs.get("custom_did")
else:
if self.custom_did != None:
did = self.custom_did
else:
did = "".join(random.choice(string.digits) for num in range(19))
return region, language, proxy, maxCount, did

#
Expand Down
Loading

0 comments on commit e1d6006

Please sign in to comment.