Skip to content

Commit

Permalink
Merge pull request #551 from davidteather/nightly
Browse files Browse the repository at this point in the history
V3.9.5 - Part 2 Fix #550
  • Loading branch information
davidteather authored Apr 1, 2021
2 parents 0771212 + 113cc61 commit 14ac7b3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ python -m examples.getTrending

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

* [Python 3.7](https://www.python.org/) - The web framework used

## Authors

* **David Teather** - *Initial work* - [davidteather](https://github.com/davidteather)
Expand Down
29 changes: 15 additions & 14 deletions TikTokApi/tiktok.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,10 +869,8 @@ def get_music_object_full(self, id, **kwargs):
proxies=self.__format_proxy(kwargs.get("proxy", None)),
cookies=self.get_cookies(**kwargs),
)
t = r.text
j_raw = t.split(
'<script id="__NEXT_DATA__" type="application/json" crossorigin="anonymous">'
)[1].split("</script>")[0]

j_raw = self.__extract_tag_contents(r.text)
return json.loads(j_raw)["props"]["pageProps"]["musicInfo"]

def by_hashtag(self, hashtag, count=30, offset=0, **kwargs) -> dict:
Expand Down Expand Up @@ -1104,9 +1102,7 @@ def get_tiktok_by_html(self, url, **kwargs) -> dict:

t = r.text
try:
j_raw = t.split(
'<script id="__NEXT_DATA__" type="application/json" crossorigin="anonymous">'
)[1].split("</script>")[0]
j_raw = self.__extract_tag_contents(r.text)
except IndexError:
if not t:
logging.error("TikTok response is empty")
Expand All @@ -1118,7 +1114,7 @@ def get_tiktok_by_html(self, url, **kwargs) -> dict:

if data["serverCode"] == 404:
raise TikTokNotFoundError(
"TikTok with that url doesn't exist".format(username)
"TikTok with that url doesn't exist"
)

return data
Expand Down Expand Up @@ -1206,9 +1202,7 @@ def get_user(self, username, **kwargs) -> dict:
t = r.text

try:
j_raw = t.split(
'<script id="__NEXT_DATA__" type="application/json" crossorigin="anonymous">'
)[1].split("</script>")[0]
j_raw = self.__extract_tag_contents(r.text)
except IndexError:
if not t:
logging.error("Tiktok response is empty")
Expand Down Expand Up @@ -1563,9 +1557,7 @@ def get_music_title(self, id, **kwargs):
cookies=self.get_cookies(**kwargs),
)
t = r.text
j_raw = t.split(
'<script id="__NEXT_DATA__" type="application/json" crossorigin="anonymous">'
)[1].split("</script>")[0]
j_raw = self.__extract_tag_contents(r.text)

music_object = json.loads(j_raw)["props"]["pageProps"]["musicInfo"]
if not music_object.get("title", None):
Expand Down Expand Up @@ -1659,6 +1651,15 @@ def __add_new_params__(self) -> str:
}
return urlencode(query)

def __extract_tag_contents(self, html):
nonce_start = '<head nonce="'
nonce_end = '">'
nonce = html.split(nonce_start)[1].split(nonce_end)[0]
j_raw = html.split(
'<script id="__NEXT_DATA__" type="application/json" nonce="%s" crossorigin="anonymous">' % nonce
)[1].split("</script>")[0]
return j_raw

# Process the kwargs
def __process_kwargs__(self, kwargs):
region = kwargs.get("region", "US")
Expand Down

0 comments on commit 14ac7b3

Please sign in to comment.