Skip to content

Commit

Permalink
Type hinting updated to support 3.8.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Fluxticks committed Jan 23, 2024
1 parent ec5d858 commit 1d51292
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 22 deletions.
11 changes: 10 additions & 1 deletion tiktokdl/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
from tiktokdl import *
from tiktokdl import *

import sys
if sys.version_info[0] == 3 and sys.version_info[1] < 9:
import logging
logging.warning(
"Python 3.8.x is only supported using insecure SSL. Please update python to 3.9.x!"
)
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
10 changes: 6 additions & 4 deletions tiktokdl/captcha.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from tiktokdl.image_processing import find_position, image_from_url

from typing import Tuple, Union


def __parse_captcha_params_from_url(url: str) -> dict:
parsed_url = urlparse(url, allow_fragments=False)
Expand All @@ -30,7 +32,7 @@ async def __get_captcha_response_headers(request: Request) -> dict:
return all_headers


def __generate_random_captcha_steps(piece_position: tuple[int, int],
def __generate_random_captcha_steps(piece_position: Tuple[int, int],
tip_y_value: int):
x_position = piece_position[0]

Expand Down Expand Up @@ -80,15 +82,15 @@ def __calculate_captcha_solution(captcha_get_data: dict) -> dict:

async def handle_captcha(playwright_page: Page,
attempts: int = 3,
timeout: float | None = 5000) -> bool:
timeout: Union[float, None] = 5000) -> bool:
captcha_success_status = False
attempt_count = 0

while not captcha_success_status and attempt_count < attempts:
try:
async with playwright_page.expect_request(
lambda x: "/captcha/get?" in x.url,
timeout=timeout) as request:
lambda x: "/captcha/get?" in x.url,
timeout=timeout) as request:
await playwright_page.wait_for_load_state("networkidle")
request_value = await request.value
response = await request_value.response()
Expand Down
33 changes: 18 additions & 15 deletions tiktokdl/download_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
ResponseParseException, TikTokBaseException)
from tiktokdl.post_data import TikTokPost, TikTokSlide, TikTokVideo

from typing import Union

__all__ = ["get_post"]


Expand All @@ -28,7 +30,7 @@ def __filter_kwargs(function: callable, all_kwargs: dict):
return valid_kwargs


def __validate_download_path(download_path: str | None):
def __validate_download_path(download_path: Union[str, None]):
if download_path is None:
download_path = f"{curdir}{PATH_SEP}"

Expand All @@ -42,7 +44,8 @@ def __post_is_slideshow(data: dict) -> bool:
return data.get("imagePost") is not None


def __parse_api_response(api_response: dict):
def __parse_api_response(
api_response: dict) -> Union[TikTokSlide, TikTokVideo]:
root_data = api_response.get("itemInfo").get("itemStruct")

author_data = root_data.get("author")
Expand Down Expand Up @@ -77,7 +80,7 @@ def __parse_api_response(api_response: dict):


async def download_video(playwright_page: Page, video_info: TikTokVideo,
timeout: float, download_path: str | None):
timeout: float, download_path: Union[str, None]):
"""Uses the the browser request for the video to download the video. Valid for any download setting but less reliable.
Args:
Expand All @@ -92,7 +95,7 @@ async def download_video(playwright_page: Page, video_info: TikTokVideo,

response_base_url = video_source.split("?")[0]
async with playwright_page.expect_request_finished(
lambda x: response_base_url in x.url, timeout=timeout) as request:
lambda x: response_base_url in x.url, timeout=timeout) as request:
request_value = await request.value
response = await request_value.response()
save_path = f"{download_path}{video_info.post_id}.mp4"
Expand All @@ -102,7 +105,7 @@ async def download_video(playwright_page: Page, video_info: TikTokVideo,


async def download_slideshow(video_info: TikTokSlide,
download_path: str | None):
download_path: Union[str, None]):
"""For a given Slideshow post, download the images associated with it.
Args:
Expand All @@ -123,12 +126,12 @@ async def download_slideshow(video_info: TikTokSlide,

async def __get_post(url: str,
download: bool = True,
proxy: dict | None = None,
proxy: Union[dict, None] = None,
request_timeout: float = 5000,
download_path: str | None = None,
headless: bool | None = None,
slow_mo: float | None = None,
**kwargs):
download_path: Union[str, None] = None,
headless: Union[bool, None] = None,
slow_mo: Union[float, None] = None,
**kwargs) -> Union[TikTokSlide, TikTokVideo]:
async with async_playwright() as playwright:
# TODO: Find a way to use Chromium and be able to recive the download_video response body
# browser = await playwright.chromium.launch(headless=headless, slow_mo=slow_mo, args=["--disable-http2"])
Expand Down Expand Up @@ -175,14 +178,14 @@ async def __get_post(url: str,

async def get_post(url: str,
download: bool = True,
proxy: dict | None = None,
proxy: Union[dict, None] = None,
retries: int = 3,
retry_delay: float = 500,
request_timeout: float = 5000,
download_path: str | None = None,
headless: bool | None = None,
slow_mo: float | None = None,
**kwargs):
download_path: Union[str, None] = None,
headless: Union[bool, None] = None,
slow_mo: Union[float, None] = None,
**kwargs) -> Union[TikTokSlide, TikTokVideo]:
"""Get the information about a given video URL. If the `download` param is set to True, also download the video as an mp4 file or slideshow images as JPEG files.
Args:
Expand Down
4 changes: 3 additions & 1 deletion tiktokdl/image_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import numpy as np
from urllib.request import urlopen

from typing import Tuple


def preprocess(image: Mat) -> Mat:
"""Preprocess a given image for better results in `find_position`.
Expand Down Expand Up @@ -43,7 +45,7 @@ def image_from_url(url: str) -> Mat:
return cv.imdecode(image_array, -1)


def find_position(background_image: Mat, piece_image: Mat, method: int = cv.TM_SQDIFF) -> tuple[int, int]:
def find_position(background_image: Mat, piece_image: Mat, method: int = cv.TM_SQDIFF) -> Tuple[int, int]:
"""For a given puzzle piece image, find it's corresponding position in the background image.
Args:
Expand Down
4 changes: 3 additions & 1 deletion tiktokdl/post_data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from dataclasses import dataclass
from datetime import datetime

from typing import List


@dataclass()
class TikTokPost:
Expand All @@ -27,4 +29,4 @@ class TikTokVideo(TikTokPost):

@dataclass()
class TikTokSlide(TikTokPost):
images: list[str] = None
images: List[str] = None

0 comments on commit 1d51292

Please sign in to comment.