Skip to content

Commit

Permalink
Merge pull request #41 from shininome/aio_test
Browse files Browse the repository at this point in the history
0.1.1
  • Loading branch information
shininome authored Oct 24, 2024
2 parents 4fdf2ca + f800d33 commit 159acbf
Show file tree
Hide file tree
Showing 31 changed files with 514 additions and 322 deletions.
25 changes: 5 additions & 20 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
types:
- closed
branches:
- main
- aio_test
push:

jobs:
Expand All @@ -16,7 +16,7 @@ jobs:
- name: Set up Python 3.11
uses: actions/setup-python@v3
with:
python-version: '3.11'
python-version: "3.11"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down Expand Up @@ -121,12 +121,10 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: 20

- uses: pnpm/action-setup@v4
name: Install pnpm
with:
Expand Down Expand Up @@ -185,7 +183,7 @@ jobs:
uses: docker/metadata-action@v4
with:
images: |
estrellaxd/auto_bangumi
shinonomeow/auto_bangumi
ghcr.io/${{ github.repository }}
tags: |
type=raw,value=${{ needs.version-info.outputs.version }}
Expand All @@ -197,7 +195,7 @@ jobs:
uses: docker/metadata-action@v4
with:
images: |
estrellaxd/auto_bangumi
shinonomeow/auto_bangumi
ghcr.io/${{ github.repository }}
tags: |
type=raw,value=${{ needs.version-info.outputs.version }}
Expand Down Expand Up @@ -258,7 +256,7 @@ jobs:
builder: ${{ steps.buildx.output.name }}
platforms: linux/amd64,linux/arm64,linux/arm/v7
push: false
tags: estrellaxd/auto_bangumi:test
tags: shinonomeow/auto_bangumi:test
cache-from: type=gha, scope=${{ github.workflow }}
cache-to: type=gha, scope=${{ github.workflow }}

Expand Down Expand Up @@ -328,16 +326,3 @@ jobs:
backend/app-v${{ needs.version-info.outputs.version }}.zip
env:
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}

telegram:
runs-on: ubuntu-latest
needs: [release]
steps:
- name: send telegram message on push
uses: appleboy/telegram-action@master
with:
to: ${{ secrets.TELEGRAM_TO }}
token: ${{ secrets.TELEGRAM_TOKEN }}
message: |
New release: ${{ needs.release.outputs.version }}
Link: ${{ needs.release.outputs.url }}
1 change: 1 addition & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
anyio==3.7.0
bencode.py ==4.0.0
bs4==0.0.1
certifi==2023.5.7
charset-normalizer==3.1.0
Expand Down
3 changes: 2 additions & 1 deletion backend/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from module.conf import VERSION, settings, setup_logger
from module.utils import load_image

setup_logger()
setup_logger(reset=True)
logger = logging.getLogger(__name__)
uvicorn_logging_config = {
"version": 1,
Expand Down Expand Up @@ -42,6 +42,7 @@ def create_app() -> FastAPI:

@app.get("/posters/{path:path}", tags=["posters"])
async def posters(path: str):
#FIX: windown path
post_path = f"data/posters/{path}"
if not os.path.exists(post_path):
await load_image(path)
Expand Down
1 change: 0 additions & 1 deletion backend/src/module/api/rss.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from fastapi import APIRouter, Depends
from fastapi.responses import JSONResponse

from module.downloader import DownloadClient
from module.manager import SeasonCollector
from module.models import (
APIResponse,
Expand Down
7 changes: 6 additions & 1 deletion backend/src/module/conf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
from .log import LOG_PATH, setup_logger
from .search_provider import SEARCH_CONFIG

PLATFORM = "Windows" if "\\" in settings.downloader.path else "Unix"
TMDB_API = "291237f90b24267380d6176c98f7619f"
DATA_PATH = "sqlite:///data/data.db"
LEGACY_DATA_PATH = Path("data/data.json")
VERSION_PATH = Path("config/version.info")
POSTERS_PATH = Path("data/posters")

PLATFORM = "Windows" if "\\" in settings.downloader.path else "Unix"

if PLATFORM == "Windows":
from pathlib import PureWindowsPath as Path
else:
from pathlib import Path

__all__ = [
"VERSION",
Expand Down
78 changes: 48 additions & 30 deletions backend/src/module/core/aiocore.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from abc import abstractmethod

from module.conf import settings
from module.downloader import AsyncDownloadController
from module.manager import Renamer, eps_complete
from module.rss import RSSEngine
from module.downloader import AsyncDownloadController

logger = logging.getLogger(__name__)

Expand All @@ -22,9 +22,15 @@ async def stop(self):
for task in self.tasks:
task.cancel()
try:
logger.debug(f"[AioCore]{task.get_name()} start cancel")
await task
except asyncio.CancelledError:
logger.info(f"{task.get_name()} has canceled")
logger.info(f"[AioCore]{task.get_name()} has canceled")
except Exception as e:
logger.debug(f"[AioCore] other Exception {e}")

# logger.info(f"[AioCore]{self.tasks}")
self.tasks.clear()


class AsyncRenamer(AsyncProgram):
Expand All @@ -34,15 +40,18 @@ def __init__(self):

async def run(self):
await self.stop()
task = asyncio.create_task(self.rename_task_loop(),name="renamer_loop")
task = asyncio.create_task(
self.rename_task_loop(),
name="renamer_loop",
)
self.tasks.append(task)

async def rename_task(self):
renamer = Renamer()
task = asyncio.create_task(renamer.rename())
self.tasks.append(task)
await task
self.tasks.remove(task)
try:
renamer = Renamer()
await renamer.rename()
except TimeoutError:
logging.error("[Renamer Task] can not connect to downloader")

async def rename_task_loop(self):
while True:
Expand All @@ -59,22 +68,25 @@ def __init__(self) -> None:

async def run(self):
await self.stop()
task = asyncio.create_task(self.rss_task_loop(),name="rss_loop"
)
task = asyncio.create_task(
self.rss_task_loop(),
name="rss_loop",
)
self.tasks.append(task)

async def rss_task(self):
rss_engine = RSSEngine()
await rss_engine.refresh_all_rss()
if settings.bangumi_manage.eps_complete:
await eps_complete()

async def rss_task_loop(self):
while True:
task = asyncio.create_task(self.rss_task())
self.tasks.append(task)
await asyncio.sleep(settings.program.rss_time)
self.tasks.remove(task)

async def rss_task(self):
rss_engine = RSSEngine()
await rss_engine.refresh_all_rss()
if settings.bangumi_manage.eps_complete:
await eps_complete()

class AsyncDownload(AsyncProgram):
def __init__(self) -> None:
Expand All @@ -83,31 +95,37 @@ def __init__(self) -> None:

async def run(self):
await self.stop()
task = asyncio.create_task(self.download_task_loop(),name="download_loop")
task = asyncio.create_task(self.download_task_loop(), name="download_loop")
self.tasks.append(task)

async def download_task_loop(self):
while True:
await self.download_task()

async def download_task(self):
downloader = AsyncDownloadController()
await downloader.download()
try:
downloader = AsyncDownloadController()
await downloader.download()
except TimeoutError:
logging.error("[Renamer Task] can not connect to downloader")


if __name__ == "__main__":
import asyncio

from module.conf import setup_logger

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
handlers=[logging.StreamHandler()],
)

settings.log.debug_enable = True
setup_logger()

asyncio.run(AsyncRenamer().rename_task_loop())
if settings.bangumi_manage.eps_complete:
print(1)
# logger = logging.getLogger(__name__)
# logger.setLevel(logging.DEBUG)
# logging.basicConfig(
# level=logging.INFO,
# format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
# handlers=[logging.StreamHandler()],
# )
#
# settings.log.debug_enable = True
# setup_logger()
#
# asyncio.run(AsyncRenamer().rename_task_loop())
2 changes: 1 addition & 1 deletion backend/src/module/core/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async def startup(self):
logger.info(
"[Core] Legacy data detected, starting data migration, please wait patiently."
)
data_migration()
await data_migration()
elif self.program_status.version_update:
# Update database
await from_30_to_31()
Expand Down
10 changes: 10 additions & 0 deletions backend/src/module/database/bangumi.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ def __init__(self, session: Session):
self.session = session

def add(self, data: Bangumi):
"""link 相同, official_title相同,就只补充,主要是为了
一些会改名的
Args:
data: [TODO:description]
Returns:
[TODO:return]
"""
# 如果official_title 一致,将title_raw,group,更新
statement = select(Bangumi).where(
and_(
Expand Down Expand Up @@ -122,6 +131,7 @@ def search_url(self, rss_link: str) -> Bangumi | None:
logger.debug(f"[Database] Find bangumi id: {rss_link}.")
return self.session.exec(statement).first()


def search_id(self, _id: int) -> Bangumi | None:
statement = select(Bangumi).where(Bangumi.id == _id)
bangumi = self.session.exec(statement).first()
Expand Down
63 changes: 44 additions & 19 deletions backend/src/module/database/torrent.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import logging

from sqlmodel import Session, false, select, true
from sqlalchemy.sql import func
from sqlmodel import Session, delete, false, select, true

from module.models import Torrent, bangumi
from module.models import Torrent
from module.utils import get_hash

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -46,8 +48,27 @@ def update_one_user(self, data: Torrent):
def search(self, _id: int) -> Torrent:
return self.session.exec(select(Torrent).where(Torrent.id == _id)).first()

def search_bangumi(self,bangumi_id:int):
return self.session.exec(select(Torrent).where(Torrent.bangumi_id == bangumi_id)).all()
def search_hash(self, _hash: str) -> Torrent | None:
statement = select(Torrent).where(func.instr(Torrent.url, _hash) > 0)
return self.session.exec(statement).first()

def search_torrent(self, _hash, _name=None):
if plain_hash := get_hash(_hash):
_hash = plain_hash

torrent_item = self.search_hash(_hash)
if not torrent_item and _name:
torrent_item = self.search_name(_name)
return torrent_item

def search_name(self, name: str):
statement = select(Torrent).where(Torrent.name == name)
return self.session.exec(statement).first()

def search_bangumi(self, bangumi_id: int):
return self.session.exec(
select(Torrent).where(Torrent.bangumi_id == bangumi_id)
).all()

def search_all(self) -> list[Torrent]:
return self.session.exec(select(Torrent)).all()
Expand All @@ -67,28 +88,32 @@ def search_rss(self, rss_id: int) -> list[Torrent]:

def check_new(self, torrents_list: list[Torrent]) -> list[Torrent]:
new_torrents = []
old_torrents = self.search_all_downloaded()
old_urls = [t.url for t in old_torrents]
for torrent in torrents_list:
if torrent.url not in old_urls:
torrent_item = self.search_torrent(torrent.url, torrent.name)
if not torrent_item or not torrent_item.downloaded:
new_torrents.append(torrent)
return new_torrents

def delete(self, _id: int) -> bool:
condition = delete(Torrent).where(Torrent.id == _id)
try:
self.session.exec(condition)
self.session.commit()
return True
except Exception as e:
logger.error(f"Delete RSS Item failed. Because: {e}")
return False


if __name__ == "__main__":
from module.database import Database,engine

from module.database import Database, engine
from module.models.bangumi import Bangumi

name = "[ANi] 物语系列 第外季&第怪季 - 06 [1080P][Baha][WEB-DL][AAC AVC][CHT].mp4"
hash = "1ae27b047005e097b74b66e27c37610aa5a0f5a2"
with Database() as db:
test = TorrentDatabase(db)
bangumis = test.search (2)
bangumis.downloaded = True
test = bangumis

print(bangumis)
with Database() as db2:
test2 = TorrentDatabase(db2)
test2.add_all([bangumis])
# test.delete_one()
t_name = db.torrent.search_name(name)
t_hash = db.torrent.search_hash(hash)
db.torrent.delete(t_hash.id)
# print(f"{t_name=}")
# print(f"{t_hash=}")
Loading

0 comments on commit 159acbf

Please sign in to comment.