Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug api sound error #3

Merged
merged 17 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ Please add any relevant code that is giving you unexpected results.

Preferably the smallest amount of code to reproduce the issue.


**SET LOGGING LEVEL TO INFO BEFORE POSTING CODE OUTPUT**
```py
import logging
TikTokApi(logging_level=logging.INFO) # SETS LOGGING_LEVEL TO INFO
# Hopefully the info level will help you debug or at least someone else on the issue
```

```py
# Code Goes Here
```

Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ test2.py
build
MANIFEST
src
.vscode
.vscode
.env
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ authors:
orcid: "https://orcid.org/0000-0002-9467-4676"
title: "TikTokAPI"
url: "https://github.com/davidteather/tiktok-api"
version: 5.0.0
date-released: 2022-2-11
version: 5.1.1
date-released: 2022-3-21
88 changes: 72 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,64 @@ This is an unofficial api wrapper for TikTok.com in python. With this api you ar
## Sponsors
These sponsors have paid to be placed here and beyond that I do not have any affiliation with them, the TikTokAPI package will always be free and open-source. If you wish to be a sponsor of this project check out my [GitHub sponsors page](https://github.com/sponsors/davidteather).

[![TikAPI](https://raw.githubusercontent.com/davidteather/TikTok-Api/master/imgs/logo128.png)](https://tikapi.io/?ref=davidteather) | **[TikAPI](https://tikapi.io/?ref=davidteather)** is a paid TikTok API service providing an full out-of-the-box solution for developers, trusted by 100+ companies. [Learn more](https://tikapi.io/?ref=davidteather)
:-------------------------:|:-------------------------:
<div align="center">
<p>
<a href="https://tikapi.io/?ref=davidteather" target="_blank">
<div>
<img src="https://raw.githubusercontent.com/davidteather/TikTok-Api/master/imgs/logo128.png" width="100" alt="TikApi">
</div>
<b></b>
<div>
TikApi is a paid TikTok API service providing an full out-of-the-box solution for developers, trusted by 100+ companies.
</div>
</a>
</p>
</div>

<br>

<div align="center">
<p>
<a href="https://trendpop.social/?ref=github-davidteather-tiktokapi" target="_blank">
<div>
<img src="https://raw.githubusercontent.com/davidteather/TikTok-Api/master/imgs/trendpop.png" width="100" alt="Trendpop">
</div>
<div>
Trendpop builds software to help creators and businesses go viral on social video platforms.
</div>
<div>
<sub>
Excited about building in this space?
<a href="https://trendpop.social/careers?ref=github-davidteather-tiktokapi">
<sub>We're hiring engineers across all roles</sub>
</a>
<a href="https://trendpop.social/careers?ref=github-davidteather-tiktokapi" target="_blank">
<sub>- shoot us a message at </sub>
</a>
<a href="mailto:[email protected]" target="_blank">
<sub><code>[email protected]</code></sub>
</a>
</sub>
</div>
</a>
</p>
</div>

<br>

<div align="center">
<p>
<a href="https://influencerhunters.com/docs.html?utm_source=github&utm_medium=githubpage&utm_campaign=david_thea_github&utm_id=david_t" target="_blank">
<div>
<img src="https://raw.githubusercontent.com/andrearama/TikTok-Api/master/imgs/IH_LOGO.png" width="100" alt="IH_logo">
</div>
<b></b>
<div>
TikTok data through APIs, providing 10+ Million posts / day to the largest Marketing and Social listening platforms.
</div>
</a>
</p>
</div>

## Table of Contents
- [Documentation](#documentation)
Expand Down Expand Up @@ -43,7 +99,9 @@ If you run into an issue please check the closed issues on the github, although
pip install TikTokApi
python -m playwright install
```
If you would prefer a video walk through of setting up this package I created a currently semi-outdated [YouTube video](https://www.youtube.com/watch?v=-uCt1x8kINQ) just for that.
If you would prefer a video walk through of setting up this package [YouTube video](https://www.youtube.com/watch?v=-uCt1x8kINQ) just for that.

If you want a quick video to listen for [TikTok Live](https://www.youtube.com/watch?v=307ijmA3_lc) events in python.

#### Docker Installation

Expand All @@ -70,17 +128,15 @@ Here's a quick bit of code to get the most recent trending videos on TikTok. The
```py
from TikTokApi import TikTokApi

# In your web browser you will need to go to TikTok, check the cookies
# and under www.tiktok.com s_v_web_id should exist, and use that value
# as input to custom_verify_fp
# Or watch https://www.youtube.com/watch?v=-uCt1x8kINQ for a visual
api = TikTokApi(custom_verify_fp="")

for trending_video in api.trending.videos(count=50):
# Prints the author's username of the trending video.
print(trending_video.author.username)
# Watch https://www.youtube.com/watch?v=-uCt1x8kINQ for a brief setup tutorial
with TikTokApi() as api:
for trending_video in api.trending.videos(count=50):
# Prints the author's username of the trending video.
print(trending_video.author.username)
```

**Note**: Jupyter (ipynb) only works on linux, see [microsoft/playwright-python #178](https://github.com/microsoft/playwright-python/issues/178)

To run the example scripts from the repository root, make sure you use the `-m` option on python.
```sh
python -m examples.get_trending
Expand Down Expand Up @@ -125,10 +181,10 @@ Here's a few more examples that help illustrate the differences in the flow of t
api = TikTokApi.get_instance()
trending_videos = api.by_trending()

#V5
api = TikTokApi() # .get_instance no longer exists
for trending_video in api.trending.videos():
# do something
#V5.1
with TikTokApi() as api: # .get_instance no longer exists
for trending_video in api.trending.videos():
# do something
```

Where in V4 you had to extract information yourself, the package now handles that for you. So it's much easier to do chained related function calls.
Expand Down
2 changes: 1 addition & 1 deletion TikTokApi/api/hashtag.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def info_full(self, **kwargs) -> dict:
data = self.parent.get_data(path, **kwargs)

if data["challengeInfo"].get("challenge") is None:
raise TikTokNotFoundError("Challenge {} does not exist".format(self.name))
raise NotFoundException("Challenge {} does not exist".format(self.name))

return data

Expand Down
19 changes: 16 additions & 3 deletions TikTokApi/api/sound.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def info(self, use_html=False, **kwargs) -> dict:
sound_data = api.sound(id='7016547803243022337').info()
```
"""
self.__ensure_valid()
if use_html:
return self.info_full(**kwargs)["musicInfo"]

Expand All @@ -73,7 +74,7 @@ def info(self, use_html=False, **kwargs) -> dict:
res = self.parent.get_data(path, **kwargs)

if res.get("statusCode", 200) == 10203:
raise TikTokNotFoundError()
raise NotFoundException()

return res["musicInfo"]["music"]

Expand All @@ -89,6 +90,7 @@ def info_full(self, **kwargs) -> dict:
sound_data = api.sound(id='7016547803243022337').info_full()
```
"""
self.__ensure_valid()
r = requests.get(
"https://www.tiktok.com/music/-{}".format(self.id),
headers={
Expand Down Expand Up @@ -119,6 +121,7 @@ def videos(self, count=30, offset=0, **kwargs) -> Iterator[Video]:
# do something
```
"""
self.__ensure_valid()
processed = self.parent._process_kwargs(kwargs)
kwargs["custom_device_id"] = processed.device_id

Expand Down Expand Up @@ -154,18 +157,28 @@ def __extract_from_data(self):
data = self.as_dict
keys = data.keys()

if data.get("id") == "":
self.id = ""

if "authorName" in keys:
self.id = data["id"]
self.title = data["title"]

if data.get("authorName") is not None:
if data["authorName"] is not None:
self.author = self.parent.user(username=data["authorName"])

self.id = data.get("id")
self.title = data.get("title")

if self.id is None:
Sound.parent.logger.error(
f"Failed to create Sound with data: {data}\nwhich has keys {data.keys()}"
)

def __ensure_valid(self):
if self.id == "":
raise SoundRemovedException("This sound has been removed!")

def __repr__(self):
return self.__str__()

Expand All @@ -178,4 +191,4 @@ def __getattr__(self, name):
self.__extract_from_data()
return self.__getattribute__(name)

raise AttributeError(f"{name} doesn't exist on TikTokApi.api.Sound")
# raise AttributeError(f"{name} doesn't exist on TikTokApi.api.Sound")
2 changes: 1 addition & 1 deletion TikTokApi/api/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def info_full(self, **kwargs) -> dict:

user_props = user["props"]["pageProps"]
if user_props["statusCode"] == 404:
raise TikTokNotFoundError(
raise NotFoundException(
"TikTok user with username {} does not exist".format(self.username)
)

Expand Down
12 changes: 8 additions & 4 deletions TikTokApi/api/video.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from __future__ import annotations

from urllib.parse import urlencode

from ..helpers import extract_video_id_from_url

import logging
from typing import TYPE_CHECKING, ClassVar, Optional
from datetime import datetime

if TYPE_CHECKING:
from ..tiktok import TikTokApi
Expand All @@ -28,6 +26,10 @@ class Video:

id: Optional[str]
"""TikTok's ID of the Video"""
create_time: Optional[datetime]
"""The creation time of the Video"""
stats: Optional[dict]
"""TikTok's stats of the Video"""
author: Optional[User]
"""The User who created the Video"""
sound: Optional[Sound]
Expand Down Expand Up @@ -116,6 +118,8 @@ def __extract_from_data(self) -> None:

if "author" in keys:
self.id = data["id"]
self.create_time = datetime.fromtimestamp(data["createTime"])
self.stats = data["stats"]
self.author = self.parent.user(data=data["author"])
self.sound = self.parent.sound(data=data["music"])

Expand All @@ -137,7 +141,7 @@ def __str__(self):

def __getattr__(self, name):
# Handle author, sound, hashtags, as_dict
if name in ["author", "sound", "hashtags", "as_dict"]:
if name in ["author", "sound", "hashtags", "stats", "create_time", "as_dict"]:
self.as_dict = self.info()
self.__extract_from_data()
return self.__getattribute__(name)
Expand Down
Loading