Skip to content

Commit

Permalink
fix local mode
Browse files Browse the repository at this point in the history
  • Loading branch information
simonsmh authored Jan 2, 2025
1 parent ebb8217 commit b972df7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 25 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ Telegram bot for Bili Feed Helper.

[![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue)](https://www.gnu.org/licenses/gpl-3.0)

## Env Guide

### Official bot api
- Required: `TOKEN`: Telegram bot token
- Recommend: `REDIS_URL`: redis url string like `rediss://usr:pswd@host:port` supported by redis-py
- Supported:
- Listening on `HOST`:`PORT`
- `DATABASE_URL`: file cache db url string supported by tortoise orm
- `FILE_TABLE`: file cache db name

### Self hosted bot api
- See Official bot api
- Extra:
- `LOCAL_MODE`: Set 1 to enable
- `API_BASE_URL`: Self hosted bot api endpoint like `http://127.0.0.1:8081/bot`
- `API_BASE_FILE_URL`: Self hosted bot api file endpoint like `http://127.0.0.1:8081/file/bot`
- `LOCAL_TEMP_FILE_PATH`: Set to docker mounting point like `/var/lib/telegram-bot-api/.tmp/`


## Credit

[Bilibili API](https://github.com/Nemo2011/bilibili-api):
Expand Down
37 changes: 24 additions & 13 deletions biliparser/__main__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import asyncio
import os
import pathlib
import re
import sys
from io import BytesIO
from pathlib import Path
from uuid import uuid4

import httpx
Expand Down Expand Up @@ -99,32 +99,41 @@ async def get_media(
compression: bool = True,
size: int = 320,
media_check_ignore: bool = False,
) -> bytes | pathlib.Path | str:
) -> Path | str:
file_id: str | None = await get_cache_media(filename)
if file_id:
return file_id
header = headers.copy()
header["Referer"] = referer
async with client.stream("GET", url, headers=header) as response:
logger.info(f"下载开始: {url}")
if response.status_code != 200:
raise NetworkError(
f"媒体文件获取错误: {response.status_code} {url}->{referer}"
)
mediatype = response.headers.get("content-type").split("/")
content_type = response.headers.get("content-type")
if content_type is None:
raise NetworkError(f"媒体文件获取错误: 无法获取 content-type {url}->{referer}")
mediatype = content_type.split("/")
filepath = Path(os.environ.get("LOCAL_TEMP_FILE_PATH", ".tmp"))
filepath.mkdir(parents=True, exist_ok=True)
media = filepath / filename
if mediatype[0] in ["video", "audio", "application"]:
if not os.path.exists(".tmp"):
os.mkdir(".tmp")
media = pathlib.Path(os.path.abspath(f".tmp/{filename}"))
with open(f".tmp/{filename}", "wb") as file:
with open(media, "wb") as file:
async for chunk in response.aiter_bytes():
file.write(chunk)
elif media_check_ignore or mediatype[0] == "image":
media = await response.aread()
img = await response.aread()
if compression and mediatype[1] in ["jpeg", "png"]:
logger.info(f"压缩: {url} {mediatype[1]}")
media = compress(BytesIO(media), size).getvalue()
img = compress(BytesIO(img), size).getvalue()
with open(media, "wb") as file:
file.write(img)
else:
raise NetworkError(f"媒体文件类型错误: {mediatype} {url}->{referer}")
logger.info(f"完成下载: {media}")
if LOCAL_MODE:
return media.as_uri()
return media


Expand Down Expand Up @@ -184,7 +193,6 @@ async def parse(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await message.reply_chat_action(ChatAction.TYPING)
except:
pass

for f in await biliparser(urls):
for i in range(1, 5):
if isinstance(f, Exception):
Expand All @@ -197,11 +205,12 @@ async def parse(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await message.reply_text(f.caption, reply_markup=origin_link(f.url))
else:
medias = []
mediathumb = None
try:
async with httpx.AsyncClient(
http2=True, timeout=90, follow_redirects=True
) as client:
if f.mediaraws:
if f.mediaraws or LOCAL_MODE:
mediathumb = (
await get_media(
client,
Expand Down Expand Up @@ -350,8 +359,10 @@ async def parse(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
)
medias = [mediathumb, *media]
finally:
if isinstance(mediathumb, Path):
os.remove(mediathumb)
for item in medias:
if isinstance(item, pathlib.Path):
if isinstance(item, Path):
os.remove(item)
except BadRequest as err:
if (
Expand Down Expand Up @@ -468,7 +479,7 @@ async def fetch(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
)
finally:
for item in medias:
if isinstance(item, pathlib.Path):
if isinstance(item, Path):
os.remove(item)


Expand Down
2 changes: 1 addition & 1 deletion biliparser/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class file_cache(Model):
created = fields.DatetimeField(auto_now=True)

class Meta(Model.Meta):
table = "file"
table = os.environ.get("FILE_TABLE", "file")


async def db_init() -> None:
Expand Down
13 changes: 3 additions & 10 deletions biliparser/strategy/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from telegram.constants import MessageLimit

from ..cache import CACHES_TIMER, RedisCache
from ..utils import BILI_API, escape_markdown, logger
from ..utils import BILI_API, escape_markdown, get_filename, logger


class Feed(ABC):
Expand All @@ -29,13 +29,6 @@ def __init__(self, rawurl: str, client: httpx.AsyncClient):
self.rawurl = rawurl
self.client = client

@staticmethod
def get_filename(url) -> str:
target = re.search(r"\/([^\/]*\.\w{3,4})(?:$|\?)", url)
if target:
return target.group(1)
return url

@staticmethod
def make_user_markdown(user, uid):
return (
Expand Down Expand Up @@ -127,14 +120,14 @@ def mediaurls(self, content):
@cached_property
def mediafilename(self):
return (
[self.get_filename(i) for i in self.__mediaurls]
[get_filename(i) for i in self.__mediaurls]
if self.__mediaurls
else list()
)

@cached_property
def mediathumbfilename(self):
return self.get_filename(self.mediathumb) if self.mediathumb else str()
return get_filename(self.mediathumb) if self.mediathumb else str()

@cached_property
def url(self):
Expand Down
9 changes: 8 additions & 1 deletion biliparser/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,15 @@ def escape_markdown(text: str):
return re.sub(r"([_*\[\]()~`>\#\+\-=|{}\.!\\])", r"\\\1", html.unescape(text))


def get_filename(url) -> str:
target = re.search(r"\/([^\/]*\.\w{3,4})(?:$|\?)", url)
if target:
return target.group(1)
return url


def referer_url(url: str, referer: str):
if not referer:
return url
params = {"url": url, "referer": referer}
return f"https://referer.simonsmh.workers.dev/?{urlencode(params)}"
return f"https://referer.simonsmh.workers.dev/?{urlencode(params)}#{get_filename(url)}"

0 comments on commit b972df7

Please sign in to comment.