Skip to content

Commit

Permalink
Refactor MAX_SIZE context variable handling
Browse files Browse the repository at this point in the history
The get_max_size function was removed and the context variable MAX_SIZE was redefined to include a default setting of a float type right on its declaration. Afterward, all get_max_size calls in youtube.py and tiktok.py were replaced with MAX_SIZE.get calls to adapt with the updated implementation.

Signed-off-by: Jag_k <[email protected]>
  • Loading branch information
jag-k committed May 12, 2024
1 parent 4005d99 commit 2b53ec2
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 13 deletions.
9 changes: 1 addition & 8 deletions media_parser/context.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
from contextvars import ContextVar

MAX_SIZE: ContextVar[str] = ContextVar("max-size", default="inf") # float(str or "inf")


def get_max_size() -> float:
try:
return float(MAX_SIZE.get("inf"))
except ValueError:
return float("inf")
MAX_SIZE: ContextVar[float] = ContextVar("max-size", default=float("inf")) # float(str or "inf")
4 changes: 2 additions & 2 deletions media_parser/parsers/tiktok.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from aiohttp import ClientSession
from pydantic import Field

from media_parser.context import get_max_size
from media_parser.context import MAX_SIZE
from media_parser.models import Image, Media, ParserType, Video
from media_parser.parsers.base import BaseParser, MediaCache
from media_parser.utils import generate_timer
Expand Down Expand Up @@ -118,7 +118,7 @@ async def _parse(
def _process_video(self, data: dict, original_url: str) -> list[Video]:
max_quality_url = data.get("video_data", {}).get("nwm_video_url_HQ")

max_size = get_max_size()
max_size = MAX_SIZE.get()

try:
url: str | None = max(
Expand Down
4 changes: 2 additions & 2 deletions media_parser/parsers/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pytube import StreamQuery
from pytube.exceptions import PytubeError

from media_parser.context import get_max_size
from media_parser.context import MAX_SIZE
from media_parser.models import Media, ParserType, Video
from media_parser.parsers.base import BaseParser, MediaCache

Expand Down Expand Up @@ -69,7 +69,7 @@ def cpy_bound_request(self, original_url: str) -> list[Media]:
max_quality_url = stream.url
max_fs = 0

max_size = get_max_size()
max_size = MAX_SIZE.get()

for st in streams:
logger.info("Stream: %s", st)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "media-parser"
version = "2.1.0"
version = "2.1.1"
description = "API for parsing media from social networks"
authors = ["Jag_k <[email protected]>"]
maintainers = ["Jag_k <[email protected]>"]
Expand Down

0 comments on commit 2b53ec2

Please sign in to comment.