Skip to content

Commit

Permalink
Add chat command
Browse files Browse the repository at this point in the history
  • Loading branch information
ihabunek committed Aug 30, 2024
1 parent dc99ee5 commit ec985ef
Show file tree
Hide file tree
Showing 8 changed files with 659 additions and 4 deletions.
7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ packages = [
[tool.setuptools_scm]

[project.optional-dependencies]
# This is made optional because it is not pure python, and when used prevents
# distribution of twitch-dl as a pyz archive while keeping it cross-platform.
chat = [
"pillow>=9",
"fonttools>=4,<5",
]

dev = [
"build",
"pytest",
Expand Down
71 changes: 71 additions & 0 deletions twitchdl/cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import hashlib
import logging
import os
import sys
from pathlib import Path
from typing import Optional

from twitchdl.http import download_file

CACHE_SUBFOLDER = "twitch-dl"


logger = logging.getLogger(__name__)


def download_cached(
url: str,
*,
filename: Optional[str] = None,
subfolder: Optional[str] = None,
) -> Optional[Path]:
base = get_cache_dir()
if not filename:
filename = hashlib.sha256(url.encode()).hexdigest()
target = base / subfolder / filename if subfolder else base / filename

if not target.exists():
download_file(url, target)

return target


def get_text_font() -> Path:
url = "https://cdn.jsdelivr.net/gh/notofonts/notofonts.github.io/fonts/NotoSans/full/ttf/NotoSans-Light.ttf"
path = download_cached(url, subfolder="fonts", filename="NotoSans-Light.ttf")
if not path:
raise ValueError(f"Failed downloading font from {url}")
return path


def get_noto_color_emoji_font() -> Path:
url = "https://github.com/googlefonts/noto-emoji/raw/main/fonts/NotoColorEmoji.ttf"
path = download_cached(url, subfolder="fonts", filename="NotoColorEmoji.ttf")
if not path:
raise ValueError(f"Failed downloading font from {url}")
return path


def get_cache_dir() -> Path:
path = _cache_dir_path()
path.mkdir(parents=True, exist_ok=True)
return path


def _cache_dir_path() -> Path:
"""Returns the path to the cache directory"""

# Windows
if sys.platform == "win32" and "APPDATA" in os.environ:
return Path(os.environ["APPDATA"], CACHE_SUBFOLDER, "cache")

# Mac OS
if sys.platform == "darwin":
return Path.home() / "Library" / "Caches" / CACHE_SUBFOLDER

# Respect XDG_CONFIG_HOME env variable if set
# https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
if "XDG_CACHE_HOME" in os.environ:
return Path(os.environ["XDG_CACHE_HOME"], CACHE_SUBFOLDER)

return Path.home() / ".cache" / CACHE_SUBFOLDER
Loading

0 comments on commit ec985ef

Please sign in to comment.