From 049f985c2d1c973eec74f6c998a62698610fe7ed Mon Sep 17 00:00:00 2001 From: WorldTeacher Date: Fri, 23 May 2025 16:20:56 +0200 Subject: [PATCH 1/4] Add Gitea CI workflow and enhance AnilistAPI with new queries and data handling - Introduced a Gitea CI workflow for building and publishing the package. - Updated AnilistAPI to support additional queries for genres and tags. - Improved request handling and response parsing in the API. - Enhanced Manga schema to ensure proper type annotations and data structure. --- .gitea/workflows/build.yaml | 88 +++++++++++++++++++++++++++++++++ src/anilistapi/api.py | 52 ++++++++++++++++--- src/anilistapi/queries/manga.py | 88 +++++++++++++++++++++++++++++++++ src/anilistapi/schemas/manga.py | 8 +-- 4 files changed, 226 insertions(+), 10 deletions(-) create mode 100644 .gitea/workflows/build.yaml diff --git a/.gitea/workflows/build.yaml b/.gitea/workflows/build.yaml new file mode 100644 index 0000000..4588b3e --- /dev/null +++ b/.gitea/workflows/build.yaml @@ -0,0 +1,88 @@ +on: + workflow_dispatch: + inputs: + release_notes: + description: Release notes (use \n for newlines) + type: string + required: false + github_release: + description: 'Create Gitea Release' + default: true + type: boolean + bump: + description: 'Bump type' + required: true + default: 'patch' + type: choice + options: + - 'major' + - 'minor' + - 'patch' + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@master + - name: Install uv + uses: astral-sh/setup-uv@v5 + - name: Set up Python + run: uv python install + - name: Set Git identity + run: | + git config user.name "Gitea CI" + git config user.email "ci@git.theprivateserver.de" + - name: Bump version + id: bump + run: | + uv tool install bump-my-version + uv tool run bump-my-version bump ${{ github.event.inputs.bump }} + # echo the version to github env, the version is shown by using uv tool run bump-my-version show current_version + echo "VERSION<> $GITHUB_ENV + echo "$(uv tool run bump-my-version show current_version)" >> $GITHUB_ENV + echo "EOF" >> $GITHUB_ENV + - name: Push changes + uses: ad-m/github-push-action@master + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + branch: ${{ github.ref }} + + - name: Add release notes to environment + id: add_release_notes + run: | + echo "RELEASE_NOTES<> $GITHUB_ENV + echo "${{ github.event.inputs.release_notes }}" >> $GITHUB_ENV + echo "EOF" >> $GITHUB_ENV + - name: Build package + run: uv build + - name: Publish package + env: + USERNAME: ${{ github.repository_owner }} + run: uv publish --publish-url https://git.theprivateserver.de/api/packages/$USERNAME/pypi/ -t ${{ secrets.TOKEN }} + - name: Generate changelog + id: changelog + uses: metcalfc/changelog-generator@v4.6.2 + with: + token: ${{ secrets.TOKEN }} + - name: Get the changelog + run: | + cat << "EOF" + ${{ steps.changelog.outputs.changelog }} + EOF + + - name: Create release + id: create_release + if: ${{ github.event.inputs.github_release == 'true' }} + uses: softprops/action-gh-release@master + with: + tag_name: ${{ env.VERSION }} + release_name: Release ${{ env.VERSION }} + body: ${{ steps.changelog.outputs.changelog }} + draft: false + prerelease: false + make_latest: true + files: | + dist/* + env: + GITHUB_TOKEN: ${{ secrets.TOKEN }} \ No newline at end of file diff --git a/src/anilistapi/api.py b/src/anilistapi/api.py index 3508ba9..eff2728 100644 --- a/src/anilistapi/api.py +++ b/src/anilistapi/api.py @@ -1,12 +1,19 @@ import requests -from .schemas.manga import Manga -from .queries.manga import MANGA_QUERY, MANGA_ID_QUERY +from .schemas.manga import Manga, Tag +from .queries.manga import ( + MANGA_QUERY, + MANGA_ID_QUERY, + REQUESTS_QUERY, + GENRES_QUERY, + TAGS_QUERY, +) from limit import limit from anilistapi import __version__, __contact__ import os +import time -REQUEST_LIMIT = 1 -REQUEST_PERIOD = 2 +REQUEST_LIMIT = 90 +REQUEST_PERIOD = 60 class AnilistAPI: @@ -19,14 +26,15 @@ class AnilistAPI: def request(self, query: str, variables: dict) -> dict: url = "https://graphql.anilist.co" response = requests.post(url, json={"query": query, "variables": variables}) + time.sleep(1) if response.status_code != 200: return {} # raise Exception(f"Error: {response}, response: {response.json()}, query: {query}, variables: {variables}") return response.json() def search_manga(self, search: str) -> list[Manga]: - variables = {"search": search} - response = self.request(MANGA_QUERY, variables) + variables = {"search": search, "format": "MANGA"} + response = self.request(REQUESTS_QUERY, variables) # check if reponse has data Page and media if not response.get("data", {}).get("Page", {}).get("media"): return [] @@ -42,3 +50,35 @@ class AnilistAPI: if not response.get("data", {}).get("Media"): return None return Manga(**response["data"]["Media"]) + + def kompage_search(self, search: str) -> list[Manga]: + variables = {"search": search} + response = self.request(REQUESTS_QUERY, variables) + # check if reponse has data Page and media + if not response.get("data", {}).get("Page", {}).get("media"): + return [] + res = [] + for manga in response["data"]["Page"]["media"]: + res.append(Manga(**manga)) + return res + + def get_genres(self) -> list[str]: + variables = {} + response = self.request(GENRES_QUERY, variables) + if not response.get("data", {}).get("genres"): + return [] + res = [] + for genre in response["data"]["genres"]: + res.append(genre) + return res + + def get_tags(self): + variables = {} + response = self.request(TAGS_QUERY, variables) + if not response.get("data", {}).get("tags"): + return [] + res = [] + for tag in response["data"]["tags"]: + ctag = Tag(**tag) + res.append(ctag.name) + return res diff --git a/src/anilistapi/queries/manga.py b/src/anilistapi/queries/manga.py index d557b7f..500ce2f 100644 --- a/src/anilistapi/queries/manga.py +++ b/src/anilistapi/queries/manga.py @@ -3,6 +3,10 @@ query media($search: String) { Page { pageInfo { hasNextPage + total + perPage + currentPage + lastPage } media(type: MANGA, search: $search) { id @@ -52,3 +56,87 @@ Media (type: MANGA, id:$id) { # Insert our variables into the query arguments (i } } """ + +REQUESTS_QUERY = """query query($search: String, $genres:[String], $tags:[String], $format: MediaFormat) { +Page(perPage: 100) { + pageInfo { + hasNextPage + total + perPage + currentPage + lastPage + } + media(type: MANGA, search: $search, genre_in: $genres, tag_in: $tags, sort: SEARCH_MATCH, format: $format) { + id + title { + romaji + english + native + } + synonyms + format + type + status(version:2) + genres + tags{ + name + isAdult + } + description + coverImage { + large + + } + isAdult + chapters + volumes + externalLinks { + site + url + type + } + countryOfOrigin + siteUrl + } + +} +}""" + +REQUESTED_QUERY = """query query($search: Int) { +Media(type: MANGA, id: $search, sort: SEARCH_MATCH) { + id + title { + romaji + english + native + } + synonyms + format + type + status(version:2) + genres + tags{ + name + isAdult + } + description + coverImage { + large + + } + isAdult + chapters + volumes + externalLinks { + site + url + type + } + countryOfOrigin + } + +}""" + +GENRES_QUERY = """query query{genres:GenreCollection}""" + +TAGS_QUERY = """query query{tags:MediaTagCollection{name}}""" diff --git a/src/anilistapi/schemas/manga.py b/src/anilistapi/schemas/manga.py index 2e4b56d..c1bef8d 100644 --- a/src/anilistapi/schemas/manga.py +++ b/src/anilistapi/schemas/manga.py @@ -2,7 +2,6 @@ from dataclasses import dataclass from .title import Title from .externalLinks import ExternalLinks from .tag import Tag -from typing import Union, Any @dataclass @@ -14,14 +13,15 @@ class Manga: type: str = None format: str = None genres: list[str] = None - tags: list | list[Tag] = None + tags: list[Tag] = None description: str = None - coverImage: dict = None + coverImage: dict[str, str] = None isAdult: bool = False chapters: int = None volumes: int = None - externalLinks: list | list[ExternalLinks] = None + externalLinks: list[ExternalLinks] = None countryOfOrigin: str = None + siteUrl: str = None def __post_init__(self): self.title = Title(**self.title) if self.title else None -- 2.49.1 From 70c8f33df03db73fd3f194bbdbf5f728809982b5 Mon Sep 17 00:00:00 2001 From: WorldTeacher Date: Fri, 23 May 2025 16:21:12 +0200 Subject: [PATCH 2/4] Add issue templates for bug reports and feature requests --- .gitea/ISSUE_TEMPLATE/bug.yml | 34 +++++++++++++++++++++++++++++++ .gitea/ISSUE_TEMPLATE/feature.yml | 23 +++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 .gitea/ISSUE_TEMPLATE/bug.yml create mode 100644 .gitea/ISSUE_TEMPLATE/feature.yml diff --git a/.gitea/ISSUE_TEMPLATE/bug.yml b/.gitea/ISSUE_TEMPLATE/bug.yml new file mode 100644 index 0000000..8f78e72 --- /dev/null +++ b/.gitea/ISSUE_TEMPLATE/bug.yml @@ -0,0 +1,34 @@ +name: Bug Report +description: Report a bug in this project +labels: + - kind/bug + - triage +body: + + - type: textarea + id: bug + attributes: + label: Describe the bug + description: | + A clear and concise description of what the bug is. + What did you expect to happen? What happened instead? + Include screenshots if applicable. + validations: + required: true + - type: textarea + id: reproduction + attributes: + label: Steps to reproduce + description: | + A clear and concise description of how to reproduce the bug. + Include steps, code snippets, or screenshots if applicable. + validations: + required: true + - type: textarea + id: additional-info + attributes: + label: Additional information + description: | + Add any other context or screenshots about the bug here. + + \ No newline at end of file diff --git a/.gitea/ISSUE_TEMPLATE/feature.yml b/.gitea/ISSUE_TEMPLATE/feature.yml new file mode 100644 index 0000000..cb6338e --- /dev/null +++ b/.gitea/ISSUE_TEMPLATE/feature.yml @@ -0,0 +1,23 @@ +name: Feature request +description: Suggest an idea for this project +labels: + - kind/feature + - triage + +body: + - type: textarea + id: feature + attributes: + label: Describe the feature + description: | + A clear and concise description of what the feature is. + What is the problem it solves? What are you trying to accomplish? + Include screenshots if applicable. + validations: + required: true + - type: textarea + id: additional-info + attributes: + label: Additional information + description: | + Add any other context or screenshots about the feature request here. \ No newline at end of file -- 2.49.1 From 6fa79516b7e60197c509bd64a1b0d3831b6e58a0 Mon Sep 17 00:00:00 2001 From: WorldTeacher Date: Fri, 23 May 2025 16:42:30 +0200 Subject: [PATCH 3/4] Fix changelog generator token reference in Gitea CI workflow --- .gitea/workflows/build.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/build.yaml b/.gitea/workflows/build.yaml index 4588b3e..8373538 100644 --- a/.gitea/workflows/build.yaml +++ b/.gitea/workflows/build.yaml @@ -64,7 +64,7 @@ jobs: id: changelog uses: metcalfc/changelog-generator@v4.6.2 with: - token: ${{ secrets.TOKEN }} + myToken: ${{ secrets.GITHUB_TOKEN }} - name: Get the changelog run: | cat << "EOF" -- 2.49.1 From 56bb67f8aea92b8cfc2a7991b9c1e5e14074d2a8 Mon Sep 17 00:00:00 2001 From: WorldTeacher Date: Fri, 23 May 2025 16:55:12 +0200 Subject: [PATCH 4/4] Refactor release notes handling in Gitea CI workflow --- .gitea/workflows/build.yaml | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/.gitea/workflows/build.yaml b/.gitea/workflows/build.yaml index 8373538..a77bb37 100644 --- a/.gitea/workflows/build.yaml +++ b/.gitea/workflows/build.yaml @@ -48,28 +48,20 @@ jobs: github_token: ${{ secrets.GITHUB_TOKEN }} branch: ${{ github.ref }} - - name: Add release notes to environment - id: add_release_notes + - name: Create release notes run: | - echo "RELEASE_NOTES<> $GITHUB_ENV - echo "${{ github.event.inputs.release_notes }}" >> $GITHUB_ENV - echo "EOF" >> $GITHUB_ENV + mkdir release_notes + echo -e "${{ inputs.release_notes }}" >> release_notes/release_notes.md + echo "Release notes:" + cat release_notes/release_notes.md + echo "" - name: Build package run: uv build - name: Publish package env: USERNAME: ${{ github.repository_owner }} run: uv publish --publish-url https://git.theprivateserver.de/api/packages/$USERNAME/pypi/ -t ${{ secrets.TOKEN }} - - name: Generate changelog - id: changelog - uses: metcalfc/changelog-generator@v4.6.2 - with: - myToken: ${{ secrets.GITHUB_TOKEN }} - - name: Get the changelog - run: | - cat << "EOF" - ${{ steps.changelog.outputs.changelog }} - EOF + - name: Create release id: create_release @@ -78,7 +70,7 @@ jobs: with: tag_name: ${{ env.VERSION }} release_name: Release ${{ env.VERSION }} - body: ${{ steps.changelog.outputs.changelog }} + body_path: release_notes/release_notes.md draft: false prerelease: false make_latest: true -- 2.49.1