Skip to content

Commit

Permalink
Fix types issues
Browse files Browse the repository at this point in the history
  • Loading branch information
captaincolonelfox committed Apr 7, 2024
1 parent c87a132 commit 0b556c3
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 16 deletions.
4 changes: 2 additions & 2 deletions app/bot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from aiogram import Dispatcher, F, Bot
from aiogram import Bot, Dispatcher, F
from aiogram.types import BufferedInputFile, Message

from settings import settings
Expand Down Expand Up @@ -29,7 +29,7 @@ async def handle_tiktok_request(message: Message, bot: Bot) -> None:
]

async for tiktok in TikTokAPI.download_tiktoks(urls):
if tiktok.is_empty:
if not tiktok.video:
continue

video = BufferedInputFile(tiktok.video, filename="video.mp4")
Expand Down
2 changes: 1 addition & 1 deletion app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Settings:


def parse_env_list(key: str) -> list[int]:
return json.loads(os.getenv(key, "[]"))
return list(map(int, json.loads(os.getenv(key, "[]"))))


def parse_env_bool(key: str, default: str = "false") -> bool:
Expand Down
2 changes: 1 addition & 1 deletion app/tiktok/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import asyncio
from typing import AsyncIterable
from collections.abc import AsyncIterable

from tiktok.client import AsyncTikTokClient
from tiktok.data import Tiktok
Expand Down
10 changes: 5 additions & 5 deletions app/tiktok/client.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import json
import random
import string
from datetime import datetime, UTC
from datetime import UTC, datetime

import httpx
from bs4 import BeautifulSoup

from tiktok.data import ItemStruct
from utils import RetryingError, retries
from utils import DifferentPageError, NoDataError, NoScriptError, retries


class AsyncTikTokClient(httpx.AsyncClient):
Expand Down Expand Up @@ -39,15 +39,15 @@ async def get_page_data(self, url: str) -> ItemStruct:
if script := soup.select_one('script[id="__UNIVERSAL_DATA_FOR_REHYDRATION__"]'):
script = json.loads(script.text)
else:
raise RetryingError("no script")
raise NoScriptError

try:
data = script["__DEFAULT_SCOPE__"]["webapp.video-detail"]["itemInfo"]["itemStruct"]
except KeyError as ex:
raise RetryingError("no data") from ex
raise NoDataError from ex

if data["id"] != page_id:
raise RetryingError("tiktok_id is different from page_id")
raise DifferentPageError
return ItemStruct.parse(data)

async def get_video(self, url: str) -> bytes | None:
Expand Down
4 changes: 0 additions & 4 deletions app/tiktok/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ class Tiktok:
description: str = ""
video: bytes | None = None

@property
def is_empty(self) -> bool:
return not self.video

@property
def caption(self) -> str:
return f"{self.description}\n\n{self.url}"
Expand Down
31 changes: 28 additions & 3 deletions app/utils.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,47 @@
import asyncio
import logging
from collections.abc import Awaitable, Callable
from functools import wraps
from typing import ParamSpec, TypeVar


class RetryingError(Exception):
pass


def retries(times: int):
def decorator(func):
class NoScriptError(RetryingError):
def __init__(self) -> None:
super().__init__("no script")


class NoDataError(RetryingError):
def __init__(self) -> None:
super().__init__("no data")


class DifferentPageError(RetryingError):
def __init__(self) -> None:
super().__init__("tiktok_id is different from page_id")


P = ParamSpec("P")
T = TypeVar("T")

Wrapper = Callable[P, Awaitable[T | None]]
Decorator = Callable[[Callable[P, Awaitable[T]]], Wrapper]


def retries(times: int) -> Decorator:
def decorator(func: Callable[P, Awaitable[T]]) -> Wrapper:
@wraps(func)
async def wrapper(*args, **kwargs):
async def wrapper(*args: P.args, **kwargs: P.kwargs) -> T | None:
for _ in range(times):
try:
return await func(*args, **kwargs)
except RetryingError:
logging.exception("Retrying")
await asyncio.sleep(0.5)
return None

return wrapper

Expand Down

0 comments on commit 0b556c3

Please sign in to comment.