Spaces:
Runtime error
Runtime error
File size: 5,789 Bytes
7e4b742 |
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
from __future__ import annotations
from ..exceptions import *
from typing import TYPE_CHECKING, ClassVar, Iterator, Optional
if TYPE_CHECKING:
from ..tiktok import TikTokApi
from .user import User
from .video import Video
class Sound:
"""
A TikTok Sound/Music/Song.
Example Usage
.. code-block:: python
song = api.song(id='7016547803243022337')
"""
parent: ClassVar[TikTokApi]
id: str
"""TikTok's ID for the sound"""
title: Optional[str]
"""The title of the song."""
author: Optional[User]
"""The author of the song (if it exists)"""
duration: Optional[int]
"""The duration of the song in seconds."""
original: Optional[bool]
"""Whether the song is original or not."""
def __init__(self, id: Optional[str] = None, data: Optional[str] = None):
"""
You must provide the id of the sound or it will not work.
"""
if data is not None:
self.as_dict = data
self.__extract_from_data()
elif id is None:
raise TypeError("You must provide id parameter.")
else:
self.id = id
async def info(self, **kwargs) -> dict:
"""
Returns all information sent by TikTok related to this sound.
Returns:
dict: The raw data returned by TikTok.
Raises:
InvalidResponseException: If TikTok returns an invalid response, or one we don't understand.
Example Usage:
.. code-block:: python
sound_info = await api.sound(id='7016547803243022337').info()
"""
id = getattr(self, "id", None)
if not id:
raise TypeError(
"You must provide the id when creating this class to use this method."
)
url_params = {
"msToken": kwargs.get("ms_token"),
"musicId": id,
}
resp = await self.parent.make_request(
url="https://www.tiktok.com/api/music/detail/",
params=url_params,
headers=kwargs.get("headers"),
session_index=kwargs.get("session_index"),
)
if resp is None:
raise InvalidResponseException(resp, "TikTok returned an invalid response.")
self.as_dict = resp
self.__extract_from_data()
return resp
async def videos(self, count=30, cursor=0, **kwargs) -> Iterator[Video]:
"""
Returns Video objects of videos created with this sound.
Args:
count (int): The amount of videos you want returned.
cursor (int): The the offset of videos from 0 you want to get.
Returns:
async iterator/generator: Yields TikTokApi.video objects.
Raises:
InvalidResponseException: If TikTok returns an invalid response, or one we don't understand.
Example Usage:
.. code-block:: python
async for video in api.sound(id='7016547803243022337').videos():
# do something
"""
id = getattr(self, "id", None)
if id is None:
raise TypeError(
"You must provide the id when creating this class to use this method."
)
found = 0
while found < count:
params = {
"musicID": id,
"count": 30,
"cursor": cursor,
}
resp = await self.parent.make_request(
url="https://www.tiktok.com/api/music/item_list/",
params=params,
headers=kwargs.get("headers"),
session_index=kwargs.get("session_index"),
)
if resp is None:
raise InvalidResponseException(
resp, "TikTok returned an invalid response."
)
for video in resp.get("itemList", []):
yield self.parent.video(data=video)
found += 1
if not resp.get("hasMore", False):
return
cursor = resp.get("cursor")
def __extract_from_data(self):
data = self.as_dict
keys = data.keys()
if "musicInfo" in keys:
author = data.get("musicInfo").get("author")
if isinstance(author, dict):
self.author = self.parent.user(data=author)
elif isinstance(author, str):
self.author = self.parent.user(username=author)
if data.get("musicInfo").get("music"):
self.title = data.get("musicInfo").get("music").get("title")
self.id = data.get("musicInfo").get("music").get("id")
self.original = data.get("musicInfo").get("music").get("original")
self.play_url = data.get("musicInfo").get("music").get("playUrl")
self.cover_large = data.get("musicInfo").get("music").get("coverLarge")
self.duration = data.get("musicInfo").get("music").get("duration")
if "music" in keys:
self.title = data.get("music").get("title")
self.id = data.get("music").get("id")
self.original = data.get("music").get("original")
self.play_url = data.get("music").get("playUrl")
self.cover_large = data.get("music").get("coverLarge")
self.duration = data.get("music").get("duration")
if "stats" in keys:
self.stats = data.get("stats")
if getattr(self, "id", None) is None:
Sound.parent.logger.error(f"Failed to create Sound with data: {data}\n")
def __repr__(self):
return self.__str__()
def __str__(self):
return f"TikTokApi.sound(id='{getattr(self, 'id', None)}')"
|