Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ server:
ipa_cache_global_workers: 8
ipa_cache_part_size: 8388608
ipa_cache_max_mb: 1024
icon_cache_dir: /data/icon-cache
icon_cache_dir: /data/icon-cache

channels:
- blatants
Expand Down Expand Up @@ -97,10 +97,10 @@ server:
ipa_cache_global_workers: 8
ipa_cache_part_size: 8388608
ipa_cache_max_mb: 1024
icon_cache_dir: /data/icon-cache
icon_cache_dir: /data/icon-cache
```

`telegram.parallel_connections` (default 8) opens extra Telegram connections per download so large files bypass Telegram's per-connection speed cap; set `1` to disable. Source JSON is cached for `server.cache_seconds`. IPA downloads are cached under `server.ipa_cache_dir` after the first request. Range requests then serve from local disk instead of re-fetching the same bytes from Telegram. On cold range requests, `server.ipa_cache_workers` downloads cache parts concurrently using `server.ipa_cache_part_size` byte chunks. `server.ipa_cache_global_workers` caps total Telegram cache downloads across files. `server.ipa_cache_max_mb` (default 1024 MB, `0` disables) caps total cache disk usage; whenever a new file is cached, older files are automatically deleted until usage is under the limit. Channel icons are fetched once and saved under `server.icon_cache_dir` (default `/data/icon-cache`, empty disables disk storage); they are never refreshed. Failed fetches are not saved and are retried on the next request.
`telegram.parallel_connections` (default 8) opens extra Telegram connections per download so large files bypass Telegram's per-connection speed cap; set `1` to disable. Source JSON is cached for `server.cache_seconds`. IPA downloads are cached under `server.ipa_cache_dir` after the first request. Range requests then serve from local disk instead of re-fetching the same bytes from Telegram. On cold range requests, `server.ipa_cache_workers` downloads cache parts concurrently using `server.ipa_cache_part_size` byte chunks. `server.ipa_cache_global_workers` caps total Telegram cache downloads across files. `server.ipa_cache_max_mb` (default 1024 MB, `0` disables) caps total cache disk usage; whenever a new file is cached, older files are automatically deleted until usage is under the limit. Channel icons are fetched once and saved under `server.icon_cache_dir` (default `/data/icon-cache`, empty disables disk storage); they are never refreshed. Icons are stored per channel, and icons of channels removed from the config are deleted, so the cache holds at most one file per configured channel. Failed fetches are not saved and are retried after 5 minutes.

### Config UI

Expand Down
1 change: 0 additions & 1 deletion config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ channels:
- Sa1y0miii
- IAppsBestLibrary
- ASJTweaks
- ethModsGroup
- c/2162963305
- isignspace
- lPAUniverse
Expand Down
176 changes: 143 additions & 33 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
import logging
from pathlib import Path
from re import compile as re_compile
from re import sub
from time import monotonic
from urllib.parse import parse_qs
Expand All @@ -20,7 +21,14 @@
from src import __version__ as APP_VERSION
from src.assets import DEFAULT_ICON_PNG
from src import settings as settings_module
from src.settings import APP_PORT, display_channel, load_settings, normalize_channel
from src.settings import (
APP_PORT,
channel_identity,
channel_key,
display_channel,
load_settings,
normalize_channel,
)
from src.source_builder import build_source
from src.telegram_client import TelegramService

Expand All @@ -30,7 +38,10 @@
source_caches: dict[str, dict[str, object]] = {
source.slug: {"expires_at": 0.0, "value": None} for source in settings.sources
}
# Icon state is keyed by _icon_key(channel), so it survives config reloads safely.
source_icon_caches: dict[str, bytes] = {}
source_icon_locks: dict[str, asyncio.Lock] = {}
source_icon_retry_after: dict[str, float] = {}
ipa_cache_locks: dict[str, asyncio.Lock] = {}
ipa_cache_lock_refs: dict[str, int] = {}
ipa_cache_global_semaphore = asyncio.Semaphore(settings.ipa_cache_global_workers)
Expand Down Expand Up @@ -59,15 +70,21 @@ async def _form(request: Request) -> dict[str, str]:


def _runtime_refresh() -> None:
global settings, source_caches, source_icon_caches, sources_by_slug, ipa_cache_global_semaphore
global settings, source_caches, sources_by_slug, ipa_cache_global_semaphore
settings_module.reload_config()
settings = load_settings()
telegram.settings = settings
telegram._channel_entities.clear()
source_caches = {source.slug: {"expires_at": 0.0, "value": None} for source in settings.sources}
source_icon_caches = {}
configured = {_icon_key(source.channel) for source in settings.sources}
for icon_state in (source_icon_caches, source_icon_retry_after):
for key in [key for key in icon_state if key not in configured]:
icon_state.pop(key, None)
for key in [key for key, lock in source_icon_locks.items() if key not in configured and not lock.locked()]:
source_icon_locks.pop(key, None)
sources_by_slug = {source.slug: source for source in settings.sources}
ipa_cache_global_semaphore = asyncio.Semaphore(settings.ipa_cache_global_workers)
_prune_icon_cache()


def _config_ui_enabled() -> None:
Expand All @@ -80,11 +97,15 @@ def _source_config_rows() -> list[tuple[int, object]]:
if not isinstance(channels, list):
return []

# Mirrors _load_sources: skips empty entries and repeats of an earlier channel.
rows = []
source_index = 0
seen: set[str] = set()
for config_index, raw_source in enumerate(channels):
if not isinstance(raw_source, str) or not normalize_channel(raw_source):
channel = normalize_channel(raw_source) if isinstance(raw_source, str) else ""
if not channel or channel_identity(channel) in seen:
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
continue
seen.add(channel_identity(channel))
if source_index >= len(settings.sources):
break
rows.append((config_index, settings.sources[source_index]))
Expand Down Expand Up @@ -435,11 +456,50 @@ def _icon_response(icon: bytes, request: Request) -> Response:
return Response(content=icon, media_type=media_type, headers=headers)


def _icon_cache_path(source_slug: str) -> Path | None:
ICON_CACHE_SUFFIX = ".img"
ICON_RETRY_SECONDS = 300.0
ICON_MAX_BYTES = 2 * 1024 * 1024
# Only files with these names are ever pruned: current channel-keyed icons,
# legacy slug-named icons, and their leftover temp files. Anything else in the
# directory (e.g. a shared /data holding telegram.session) is left alone.
_ICON_CACHE_FILE = re_compile(r"^\.?[A-Za-z0-9_-]+\.img(\.tmp)?$")


def _icon_key(channel: str) -> str:
# Icon state is keyed by channel identity, not slug: a slug can be reassigned
# to another channel when the config changes, and must never inherit its icon.
return channel_key(channel)[:32]


def _icon_cache_path(channel: str) -> Path | None:
if not settings.icon_cache_dir:
return None
# Slugs are sanitised in settings to [A-Za-z0-9_-], so they are safe as filenames.
return Path(settings.icon_cache_dir) / f"{source_slug}.img"
return Path(settings.icon_cache_dir) / f"{_icon_key(channel)}{ICON_CACHE_SUFFIX}"


def _prune_icon_cache() -> None:
# Bounds the disk cache to one file per configured channel by removing
# icon files of channels that are no longer configured. Runs on startup and
# on every config change, so a failed run is retried on the next one.
if not settings.icon_cache_dir:
return
cache_dir = Path(settings.icon_cache_dir)
keep = {f"{_icon_key(source.channel)}{ICON_CACHE_SUFFIX}" for source in settings.sources}
try:
entries = list(cache_dir.iterdir())
except FileNotFoundError:
return
except OSError as exc:
logger.warning("Channel icon cache prune skipped, cannot list %s: %s", cache_dir, exc)
return
for entry in entries:
if entry.name in keep or not _ICON_CACHE_FILE.match(entry.name):
continue
try:
if entry.is_file():
entry.unlink()
Comment thread
yazdipour marked this conversation as resolved.
Resolved
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
except OSError as exc:
logger.warning("Channel icon cache prune failed for %s: %s", entry.name, exc)


def _read_icon_cache(path: Path | None) -> bytes | None:
Expand All @@ -463,35 +523,82 @@ def _write_icon_cache(path: Path | None, icon: bytes) -> None:
temp_path.replace(path)
except OSError as exc:
logger.warning("Channel icon cache write failed for %s: %s", path.name, exc)
temp_path.unlink(missing_ok=True)
try:
temp_path.unlink(missing_ok=True)
except OSError:
pass


async def _source_channel_icon(source) -> bytes | None:
# Icons are fetched once and kept forever (memory, then disk). Only successful
# fetches are stored, so a failed attempt is retried on the next request.
cached = source_icon_caches.get(source.slug)
def _cached_channel_icon(channel: str) -> bytes | None:
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
key = _icon_key(channel)
cached = source_icon_caches.get(key)
if isinstance(cached, bytes):
return cached
path = _icon_cache_path(source.slug)
from_disk = _read_icon_cache(path)
from_disk = _read_icon_cache(_icon_cache_path(channel))
if from_disk:
source_icon_caches[source.slug] = from_disk
return from_disk
if not await telegram.is_authorized():
source_icon_caches[key] = from_disk
return from_disk


def _source_still_current(source) -> bool:
# True when the slug still belongs to the same channel after any config reload.
current = sources_by_slug.get(source.slug)
return current is not None and channel_identity(current.channel) == channel_identity(source.channel)


async def _download_channel_icon(source) -> bytes | None:
try:
icon = await telegram.download_channel_photo(source)
except Exception as exc:
logger.warning("Channel icon download failed for %s: %s", source.slug, exc)
return None
icon = await telegram.download_channel_photo(source)
if icon:
source_icon_caches[source.slug] = icon
_write_icon_cache(path, icon)
return icon
return icon if icon and len(icon) <= ICON_MAX_BYTES else None


async def _source_channel_icon(source) -> bytes | None:
# Icons are fetched once and kept (memory, then disk), keyed by channel.
# A failed fetch is retried only after ICON_RETRY_SECONDS.
key = _icon_key(source.channel)
cached = _cached_channel_icon(source.channel)
if cached:
return cached
if monotonic() < source_icon_retry_after.get(key, 0.0):
return None

lock = source_icon_locks.setdefault(key, asyncio.Lock())
async with lock:
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
# Another request may have fetched it while this one waited.
cached = _cached_channel_icon(source.channel)
if cached:
return cached
if monotonic() < source_icon_retry_after.get(key, 0.0):
return None
if not await telegram.is_authorized():
return None

icon = await _download_channel_icon(source)
# The config may have changed while Telegram was answering. If this
# channel no longer owns the slug, record nothing and serve the fallback.
if not _source_still_current(source):
return None
if icon is None:
source_icon_retry_after[key] = monotonic() + ICON_RETRY_SECONDS
return None

source_icon_retry_after.pop(key, None)
source_icon_caches[key] = icon
_write_icon_cache(_icon_cache_path(source.channel), icon)
return icon


def _source_url(source) -> str:
return f"{settings.base_url}/{source.slug}.json"


def _source_icon_url(source) -> str:
return f"{settings.base_url}/{source.slug}-icon.png"
# Versioned by channel so browsers drop a publicly cached icon when the
# slug is reassigned to another channel.
return f"{settings.base_url}/{source.slug}-icon.png?v={channel_key(source.channel)[:12]}"


def _source_links(include_channel: bool = False) -> str:
Expand All @@ -517,6 +624,7 @@ def _source_links(include_channel: bool = False) -> str:

@asynccontextmanager
async def lifespan(app: FastAPI):
_prune_icon_cache()
await telegram.start()
if await telegram.is_authorized():
for source in settings.sources:
Expand Down Expand Up @@ -726,6 +834,8 @@ async def config_add_channel(request: Request):
channels = []
else:
channels = list(channels)
if any(channel_identity(source.channel) == channel_identity(entry) for source in settings.sources):
raise ValueError(f"{display_channel(entry)} is already configured.")
channels.append(entry)
_save_channels(channels)
except Exception as exc:
Expand All @@ -743,17 +853,17 @@ async def config_remove_channel(request: Request):
channels = settings_module.CONFIG.get("channels")
if not isinstance(channels, list):
raise ValueError("channels must be a YAML list.")
configured_channels = [
item
for item in channels
if isinstance(item, str) and normalize_channel(item)
]
if len(configured_channels) <= 1:
def identity(item) -> str:
channel = normalize_channel(item) if isinstance(item, str) else ""
return channel_identity(channel) if channel else ""

removed = identity(channels[index])
if not removed:
raise ValueError("Unknown channel.")
if len({identity(item) for item in channels} - {"", removed}) < 1:
raise ValueError("At least one channel must remain configured.")
removed = channels[index]
next_channels = list(channels)
del next_channels[index]
_save_channels(next_channels)
# Drop every entry for this channel, so a duplicated one goes away in one click.
_save_channels([item for item in channels if identity(item) != removed])
except Exception as exc:
return _html_page(_config_page_body(error=str(exc), authorized=await telegram.is_authorized()), 400)

Expand Down
19 changes: 18 additions & 1 deletion src/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ def telethon_channel_ref(channel: str) -> str | int:
return channel


def channel_identity(channel: str) -> str:
# Usernames and numeric ids are case-insensitive; invite hashes are not.
# "+<hash>" and "joinchat/<hash>" are the same invite.
if channel.startswith("joinchat/"):
return f"+{channel.removeprefix('joinchat/')}"
if is_invite_channel(channel):
return channel
return channel.lower()


def channel_key(channel: str) -> str:
# Stable, filename- and URL-safe digest of a channel's identity.
return sha256(channel_identity(channel).encode("utf-8")).hexdigest()


def display_channel(channel: str) -> str:
if is_invite_channel(channel) or is_private_id_channel(channel):
return channel
Expand Down Expand Up @@ -181,13 +196,15 @@ def _load_sources() -> tuple[SourceConfig, ...]:

sources: list[SourceConfig] = []
used_slugs: set[str] = set()
seen_channels: set[str] = set()
for index, raw_source in enumerate(channels):
if not isinstance(raw_source, str):
raise RuntimeError(f"channels[{index}] must be a string")

channel = normalize_channel(raw_source)
if not channel:
if not channel or channel_identity(channel) in seen_channels:
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
continue
Comment thread
yazdipour marked this conversation as resolved.
Resolved
seen_channels.add(channel_identity(channel))

name = channel
slug = _dedupe_slug(_slug(name), used_slugs)
Expand Down
6 changes: 4 additions & 2 deletions src/source_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from telethon.tl.types import Message

from src.settings import SOURCE_DESCRIPTION, SOURCE_SUBTITLE, Settings, SourceConfig
from src.settings import SOURCE_DESCRIPTION, SOURCE_SUBTITLE, Settings, SourceConfig, channel_key
from src.telegram_client import TelegramService


Expand Down Expand Up @@ -47,7 +47,9 @@ def _app_download_url(settings: Settings, source: SourceConfig, message_id: int,


def _source_icon_url(settings: Settings, source: SourceConfig) -> str:
return f"{settings.base_url}/{source.slug}-icon.png"
# The version changes when a slug is reassigned to another channel, so
# clients holding the old (publicly cached) icon fetch the new one.
return f"{settings.base_url}/{source.slug}-icon.png?v={channel_key(source.channel)[:12]}"


def _message_icon_url(settings: Settings, source: SourceConfig, message_id: int) -> str:
Expand Down
Loading