diff --git a/.dockerignore b/.dockerignore index 01d675c..f166ac5 100644 --- a/.dockerignore +++ b/.dockerignore @@ -12,6 +12,7 @@ LICENSE .github Dockerfile docker-compose.yml +romaji-renamer-venv venv .env config/tmp diff --git a/Dockerfile b/Dockerfile index cde5ebf..cc2563e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,7 +7,7 @@ RUN echo "**** install system packages ****" \ && apt-get update \ && apt-get upgrade -y --no-install-recommends \ && apt-get install -y tzdata --no-install-recommends \ - && apt-get install -y gcc g++ libxml2-dev libxslt-dev libz-dev libjpeg62-turbo-dev zlib1g-dev wget curl jq \ + && apt-get install -y gcc g++ libxml2-dev libxslt-dev libz-dev libjpeg62-turbo-dev zlib1g-dev wget curl jq gawk \ && wget -O /tini https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini-"$(dpkg --print-architecture | awk -F- '{ print $NF }')" \ && chmod +x /tini \ && pip3 install --no-cache-dir --upgrade --requirement /requirements.txt \ @@ -20,4 +20,4 @@ RUN echo "**** install system packages ****" \ && rm -rf /requirements.txt /tmp/* /var/tmp/* /var/lib/apt/lists/* VOLUME /config VOLUME /pmm -ENTRYPOINT ["/tini", "-s", "bash", "plex-romaji-renamer.sh", "--"] \ No newline at end of file +ENTRYPOINT ["/tini", "-s", "bash", "romaji-renamer.sh", "--"] \ No newline at end of file diff --git a/README.md b/README.md index 7269793..8ba2fe1 100644 --- a/README.md +++ b/README.md @@ -68,21 +68,20 @@ First you need a GNU/Linux OS to run bash script
Requirements: Plex Media Server, Plex-Meta-Manager, Python and JQ
- Install and configure Plex-Meta-Manager: https://github.com/meisnate12/Plex-Meta-Manager
- Install JQ is a json parser see: https://stedolan.github.io/jq/ (Present by default on unRAID 6.10.0 and later.)
- - install python plexapi - ``` - pip install plexapi - ``` - - install python-dotenv - ``` - pip install python-dotenv - ``` ### Step 2 - Download and extract the script Git clone the **main** branch or get lastest release : https://github.com/Arial-Z/Romaji-Renamer/releases/latest -### Step 3 - Configure the script +### Step 3 - Install python modules and configure the script - Extract the script on a desired location.
- Navigate to its location.
+ - install python plexapi and python-dotenv with a virtual environment + ``` + cd into the script folder + python3 -m venv romaji-renamer-venv + source romaji-renamer-venv/bin/activate + pip install plexapi python-dotenv + ``` - Copy default.env to config/.env
- cd into the config folder and edit .env to fill out the variables.
```env @@ -171,7 +170,7 @@ DATA_CACHE_TIME=5 - Within your (PMM) config.yml add the following metadata_path, it should look like this with the default filepath: ```yml Animes: - metadata_path: + metadata_files: - file: config/metadata-animes.yml ``` Configuration finished. diff --git a/VERSION b/VERSION index 9cf2240..26bceff 100644 --- a/VERSION +++ b/VERSION @@ -1,3 +1,3 @@ #!/bin/bash -version=1.24 +version=1.24+ diff --git a/animes-renamer.sh b/animes-renamer.sh index 68d27ab..e117c3b 100755 --- a/animes-renamer.sh +++ b/animes-renamer.sh @@ -30,20 +30,31 @@ fi :> "$MATCH_LOG" printf "%s - Starting animes script\n\n" "$(date +%H:%M:%S)" | tee -a "$LOG" -# Download animes mapping json data +# Download animes mapping json data & anilist userlist download-anime-id-mapping +get-anilist-userlist # export animes list from plex printf "%s - Creating animes list\n" "$(date +%H:%M:%S)" | tee -a "$LOG" printf "%s\t - Exporting Plex animes library\n" "$(date +%H:%M:%S)" | tee -a "$LOG" -python3 "$SCRIPT_FOLDER/plex_animes_export.py" +if [ -f "$SCRIPT_FOLDER/romaji-renamer-venv/bin/python3" ] +then + "$SCRIPT_FOLDER/romaji-renamer-venv/bin/python3" "$SCRIPT_FOLDER/plex_animes_export.py" +else + python3 "$SCRIPT_FOLDER/plex_animes_export.py" +fi printf "%s\t - Done\n" "$(date +%H:%M:%S)" | tee -a "$LOG" # create ID/animes.tsv create-override printf "%s\t - Sorting Plex animes library\n" "$(date +%H:%M:%S)" | tee -a "$LOG" -while IFS=$'\t' read -r tvdb_id anilist_id title_override studio override_seasons_ignore notes +while IFS= read -r line do + tvdb_id=$(printf "%s" "$line" | awk -F"\t" '{print $1}') + anilist_id=$(printf "%s" "$line" | awk -F"\t" '{print $2}') + title_override=$(printf "%s" "$line" | awk -F"\t" '{print $3}') + studio=$(printf "%s" "$line" | awk -F"\t" '{print $4}') + override_seasons_ignore=$(printf "%s" "$line" | awk -F"\t" '{print $5}') if ! awk -F"\t" '{print $1}' "$SCRIPT_FOLDER/config/ID/animes.tsv" | grep -q -w "$tvdb_id" then if awk -F"\t" '{print $1}' "$SCRIPT_FOLDER/config/tmp/plex_animes_export.tsv" | grep -q -w "$tvdb_id" @@ -89,27 +100,34 @@ do -X POST \ -H 'content-type: application/json' \ --data '{ "query": "{ Page(page: '"$ongoingpage"', perPage: 50) { pageInfo { hasNextPage } media(type: ANIME, status_in: RELEASING, sort: POPULARITY_DESC) { id } } }" }' > "$SCRIPT_FOLDER/config/tmp/ongoing-anilist.json" -D "$SCRIPT_FOLDER/config/tmp/anilist-limit-rate.txt" + if grep -q -w '"data": null' "$SCRIPT_FOLDER/config/tmp/ongoing-anilist.json" + then + rm "$SCRIPT_FOLDER/config/tmp/ongoing-anilist.json" + printf "%s - Error AniList API down, exiting\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + exit 1 + fi rate_limit=0 rate_limit=$(grep -oP '(?<=x-ratelimit-remaining: )[0-9]+' "$SCRIPT_FOLDER/config/tmp/anilist-limit-rate.txt") ((wait_time++)) - if [[ -z $rate_limit ]] + if [[ $wait_time == 4 ]] + then + printf "%s - Error can't download anilist data stopping script\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + exit 1 + elif [[ -z $rate_limit ]] then printf "%s\t - Cloudflare limit rate reached watiting 60s\n" "$(date +%H:%M:%S)" | tee -a "$LOG" sleep 61 elif [[ $rate_limit -ge 3 ]] then - sleep 0.8 + sleep 1 printf "%s\t - done\n" "$(date +%H:%M:%S)" | tee -a "$LOG" break elif [[ $rate_limit -lt 3 ]] then printf "%s\t - Anilist API limit reached watiting 30s" "$(date +%H:%M:%S)" | tee -a "$LOG" sleep 30 + printf "%s\t - done\n" "$(date +%H:%M:%S)" | tee -a "$LOG" break - elif [[ $wait_time == 4 ]] - then - printf "%s - Error can't download anilist data stopping script\n" "$(date +%H:%M:%S)" | tee -a "$LOG" - exit 1 fi done jq '.data.Page.media[].id' -r "$SCRIPT_FOLDER/config/tmp/ongoing-anilist.json" >> "$SCRIPT_FOLDER/config/tmp/ongoing-tmp.tsv" # store the mal ID of the ongoing show @@ -139,7 +157,7 @@ do fi fi done < "$SCRIPT_FOLDER/config/tmp/ongoing.tsv" -printf "%s\t - Done\n\n" "$(date +%H:%M:%S)" +printf "%s\t - Done\n" "$(date +%H:%M:%S)" printf "%s - Done\n\n" "$(date +%H:%M:%S)" # write PMM metadata file from ID/animes.tsv and jikan API diff --git a/config/default.env b/config/default.env index 7e5dca7..105070d 100644 --- a/config/default.env +++ b/config/default.env @@ -57,6 +57,12 @@ SORT_TITLE_ENG=No ORIGINAL_TITLE_NATIVE=Yes # Rename season to the anilist title of that season (Yes/No) RENAME_SEASONS=Yes +# Anilist have some full uppercase title, this settings will remove them "86 EIGHTY-SIX" > "86 Eighty-Six" (Yes/No) +REDUCE_TITLE_CAPS=Yes +# Disable anilist tags (Yes/No) +DISABLE_TAGS=No +# Add a default "Anime" tag to everything (Yes/No) +ADD_ANIME_TAG=Yes #Grab anilist tags higher or equal than percentage (0-100) ANILIST_TAGS_P=70 # Download poster (Yes/No) @@ -67,13 +73,19 @@ POSTER_SEASON_DOWNLOAD=Yes POSTER_SOURCE=ANILIST # Ignore seasons title, rating and poster (Yes/No) IGNORE_SEASONS=No -# Ignore season 1 if it's the only season (Yes/No) -IGNORE_S1=Yes -# Anilist have some full uppercase title, this settings will remove them "86 EIGHTY-SIX" > "86 Eighty-Six" (Yes/No) -REDUCE_TITLE_CAPS=Yes #Add the anime season to the season label in plex (Fall 2022, Spring 2021, ...) SEASON_YEAR=No -# Disable anilist tags -DISABLE_TAGS=No +# Ignore season rating for animmes with only 1 season (Yes/No) +IGNORE_S1_ONLY_RATING=Yes +# Add Anime Awards winner for seasons and shows +ANIME_AWARDS=Yes +# Ignore non japanese voice actor awards (Yes/No) +ANIME_AWARDS_NO_FVA=Yes +# Add tags based on userlists from anilist (Completed, wathcing) (Yes/No) +ANILIST_LISTS=No +# Anilist username +ANILIST_USERNAME=Arialz +# For Shows the level tags should be added ("show", "season" or "both") +ANILIST_LISTS_LEVEL=show # Mal Data cache time (in days min : 1) -DATA_CACHE_TIME=5 +DATA_CACHE_TIME=5 \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 747a9ab..a723ed6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,6 @@ --- -version: "2.1" services: - plex-romaji-renamer: + romaji-renamer: image: arialz/romaji-renamer:dev container_name: romaji-renamer environment: @@ -11,4 +10,4 @@ services: volumes: - /path/to/script/config:/config:rw - /path/to/pmm/config:/pmm:rw - restart: no + restart: no \ No newline at end of file diff --git a/functions.sh b/functions.sh index c7853c6..a75e3f4 100644 --- a/functions.sh +++ b/functions.sh @@ -1,7 +1,7 @@ #!/bin/bash #General variables -LOG=$LOG_FOLDER/PRR_$(date +%Y.%m.%d).log +LOG=$LOG_FOLDER/RR_$(date +%Y.%m.%d).log MATCH_LOG=$LOG_FOLDER/${media_type}-missing-id.log # functions @@ -24,20 +24,89 @@ function download-anime-id-mapping () { curl -s "https://raw.githubusercontent.com/Arial-Z/Animes-ID/main/list-movies-id.json" > "$SCRIPT_FOLDER/config/tmp/list-movies-id.json" size=$(du -b "$SCRIPT_FOLDER/config/tmp/list-movies-id.json" | awk '{ print $1 }') fi - ((wait_time++)) - if [[ $size -gt 1000 ]] + ((wait_time++)) + if [[ $wait_time == 4 ]] then - printf "%s - Done\n\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + printf "%s - Error can't download animes mapping file stopping script\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + exit 1 + elif [[ $size -gt 1000 ]] + then + printf "%s - Done\n" "$(date +%H:%M:%S)" | tee -a "$LOG" break fi + sleep 30 + done + wait_time=0 + while [ $wait_time -lt 5 ]; + do + printf "%s - Downloading animes awards data\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + curl -s "https://raw.githubusercontent.com/Arial-Z/Animes-ID/main/cr-award/cr-award.json" > "$SCRIPT_FOLDER/config/tmp/cr-award.json" + size=$(du -b "$SCRIPT_FOLDER/config/tmp/cr-award.json" | awk '{ print $1 }') + ((wait_time++)) if [[ $wait_time == 4 ]] then - printf "%s - Error can't download animes mapping file stopping script\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + printf "%s - Error can't download animes awards file stopping script\n" "$(date +%H:%M:%S)" | tee -a "$LOG" exit 1 + elif [[ $size -gt 1000 ]] + then + printf "%s - Done\n\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + break fi sleep 30 done } +function get-anilist-userlist { + if [[ $ANILIST_LISTS == "Yes" ]] + then + printf "%s - Creating Anilist userlist for : %s\n" "$(date +%H:%M:%S)" "$ANILIST_USERNAME" | tee -a "$LOG" + wait_time=0 + while [ $wait_time -lt 5 ]; + do + printf "%s\t - Downloading Anilist userlist\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + curl -s 'https://graphql.anilist.co/' \ + -X POST \ + -H 'content-type: application/json' \ + --data '{ "query": "{ MediaListCollection(userName: \"'"$ANILIST_USERNAME"'\" type:ANIME) { lists { name entries { mediaId } }}}" }' > "$SCRIPT_FOLDER/config/tmp/anilist-$ANILIST_USERNAME.json" -D "$SCRIPT_FOLDER/config/tmp/anilist-limit-rate.txt" + if grep -q -w '"data": null' "$SCRIPT_FOLDER/config/tmp/anilist-$ANILIST_USERNAME.json" + then + rm "$SCRIPT_FOLDER/config/tmp/anilist-$ANILIST_USERNAME.json" + printf "%s - Error AniList API down, exiting\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + exit 1 + fi + rate_limit=0 + rate_limit=$(grep -oP '(?<=x-ratelimit-remaining: )[0-9]+' "$SCRIPT_FOLDER/config/tmp/anilist-limit-rate.txt") + ((wait_time++)) + if [[ $wait_time == 4 ]] + then + printf "%s - Error can't download anilist data stopping script\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + exit 1 + elif [[ -z $rate_limit ]] + then + printf "%s\t - Cloudflare limit rate reached watiting 60s\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + sleep 61 + elif [[ $rate_limit -ge 3 ]] + then + sleep 1 + printf "%s\t - Done\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + break + elif [[ $rate_limit -lt 3 ]] + then + printf "%s\t - Anilist API limit reached watiting 30s" "$(date +%H:%M:%S)" | tee -a "$LOG" + sleep 30 + printf "%s\t - Done\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + break + fi + done + printf "%s\t - Sorting Anilist userlist\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + jq '.data.MediaListCollection.lists | .[] | select( .name == "Completed" ) | .entries | .[].mediaId ' -r "$SCRIPT_FOLDER/config/tmp/anilist-$ANILIST_USERNAME.json" | paste -s -d, - > "$SCRIPT_FOLDER/config/data/anilist-$ANILIST_USERNAME-Completed.tsv" + jq '.data.MediaListCollection.lists | .[] | select( .name == "Watching" ) | .entries | .[].mediaId ' -r "$SCRIPT_FOLDER/config/tmp/anilist-$ANILIST_USERNAME.json" | paste -s -d, - > "$SCRIPT_FOLDER/config/data/anilist-$ANILIST_USERNAME-Watching.tsv" + jq '.data.MediaListCollection.lists | .[] | select( .name == "Dropped" ) | .entries | .[].mediaId ' -r "$SCRIPT_FOLDER/config/tmp/anilist-$ANILIST_USERNAME.json" | paste -s -d, - > "$SCRIPT_FOLDER/config/data/anilist-$ANILIST_USERNAME-Dropped.tsv" + jq '.data.MediaListCollection.lists | .[] | select( .name == "Paused" ) | .entries | .[].mediaId ' -r "$SCRIPT_FOLDER/config/tmp/anilist-$ANILIST_USERNAME.json" | paste -s -d, - > "$SCRIPT_FOLDER/config/data/anilist-$ANILIST_USERNAME-Paused.tsv" + jq '.data.MediaListCollection.lists | .[] | select( .name == "Planning" ) | .entries | .[].mediaId ' -r "$SCRIPT_FOLDER/config/tmp/anilist-$ANILIST_USERNAME.json" | paste -s -d, - > "$SCRIPT_FOLDER/config/data/anilist-$ANILIST_USERNAME-Planning.tsv" + printf "%s\t - Done\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + printf "%s - done\n\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + fi +} function get-anilist-id () { if [[ $media_type == "animes" ]] then @@ -67,7 +136,13 @@ function get-anilist-infos () { wait_time=0 while [ $wait_time -lt 5 ]; do - if [[ "$season_loop" == 1 ]] + if [[ "$airing_loop" == 1 ]] + then + printf "%s\t\t\t - Downloading airing info for Anilist : %s\n" "$(date +%H:%M:%S)" "$anilist_id" | tee -a "$LOG" + elif [[ $cours_count_total -gt 1 ]] + then + printf "%s\t\t - Downloading data for S%s part-%s Anilist : %s\n" "$(date +%H:%M:%S)" "$season_number" "$cours_count" "$anilist_id" | tee -a "$LOG" + elif [[ "$season_loop" == 1 ]] then printf "%s\t\t - Downloading data for S%s anilist : %s\n" "$(date +%H:%M:%S)" "$season_number" "$anilist_id" | tee -a "$LOG" else @@ -76,28 +151,44 @@ function get-anilist-infos () { curl -s 'https://graphql.anilist.co/' \ -X POST \ -H 'content-type: application/json' \ - --data '{ "query": "{ Media(type: ANIME, id: '"$anilist_id"') { title { romaji(stylised:false), english(stylised:false), native(stylised:false) }, averageScore, genres, tags { name, rank },studios { edges { node { name, isAnimationStudio } } },startDate {year, month} season, seasonYear, coverImage { extraLarge }, status, idMal} }" }' > "$SCRIPT_FOLDER/config/data/anilist-$anilist_id.json" -D "$SCRIPT_FOLDER/config/tmp/anilist-limit-rate.txt" - rate_limit=0 + --data '{ "query": "{ Media(type: ANIME, id: '"$anilist_id"') { relations { edges { relationType node { id type format title { romaji } status } } } title { romaji(stylised: false) english(stylised: false) native(stylised: false) } averageScore genres tags { name rank } studios { edges { node { name isAnimationStudio } } } startDate { year month } season seasonYear coverImage { extraLarge } status idMal} }" }' > "$SCRIPT_FOLDER/config/data/anilist-$anilist_id.json" -D "$SCRIPT_FOLDER/config/tmp/anilist-limit-rate.txt" + if grep -q -w '"data": null' "$SCRIPT_FOLDER/config/data/anilist-$anilist_id.json" + then + rm "$SCRIPT_FOLDER/config/data/anilist-$anilist_id.json" + printf "%s - Error AniList API down, exiting\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + exit 1 + fi rate_limit=$(grep -oP '(?<=x-ratelimit-remaining: )[0-9]+' "$SCRIPT_FOLDER/config/tmp/anilist-limit-rate.txt") - ((wait_time++)) - if [[ -z $rate_limit ]] + ((wait_time++)) + if [[ $wait_time == 4 ]] + then + printf "%s - Error can't download anilist data stopping script\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + exit 1 + elif [[ -z $rate_limit ]] then printf "%s\t\t - Cloudflare limit rate reached watiting 60s\n" "$(date +%H:%M:%S)" | tee -a "$LOG" sleep 61 elif [[ $rate_limit -ge 3 ]] then - sleep 0.8 - printf "%s\t\t - Done\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + sleep 1 + if [[ "$airing_loop" == 1 ]] + then + printf "%s\t\t\t - Done\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + else + printf "%s\t\t - Done\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + fi break elif [[ $rate_limit -lt 3 ]] then printf "%s\t\t - Anilist API limit reached watiting 30s" "$(date +%H:%M:%S)" | tee -a "$LOG" sleep 30 + if [[ "$airing_loop" == 1 ]] + then + printf "%s\t\t\t - Done\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + else + printf "%s\t\t - Done\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + fi break - elif [[ $wait_time == 4 ]] - then - printf "%s - Error can't download anilist data stopping script\n" "$(date +%H:%M:%S)" | tee -a "$LOG" - exit 1 fi done fi @@ -112,7 +203,10 @@ function get-mal-infos () { else if [ ! -f "$SCRIPT_FOLDER/config/data/MAL-$mal_id.json" ] then - if [[ "$season_loop" == 1 ]] + if [[ $cours_count_total -gt 1 ]] + then + printf "%s\t\t - Downloading data for S%s part-%s MAL : %s\n" "$(date +%H:%M:%S)" "$season_number" "$cours_count" "$mal_id" | tee -a "$LOG" + elif [[ "$season_loop" == 1 ]] then printf "%s\t\t - Downloading data for S%s MAL : %s\n" "$(date +%H:%M:%S)" "$season_number" "$mal_id" | tee -a "$LOG" else @@ -124,6 +218,7 @@ function get-mal-infos () { printf "%s - Jikan API limit reached watiting 30s" "$(date +%H:%M:%S)" | tee -a "$LOG" sleep 30 curl -s -o "$SCRIPT_FOLDER/config/data/MAL-$mal_id.json" -w "%{http_code}" "https://api.jikan.moe/v4/anime/$mal_id" > "$SCRIPT_FOLDER/config/tmp/jikan-limit-rate.txt" + printf "%s\t\t - Done\n" "$(date +%H:%M:%S)" | tee -a "$LOG" fi sleep 1.1 printf "%s\t\t - Done\n" "$(date +%H:%M:%S)" | tee -a "$LOG" @@ -201,6 +296,8 @@ function get-score () { if [[ "$anime_score" == "null" ]] || [[ "$anime_score" == "" ]] then anime_score=0 + else + anime_score=$(printf %s "$anime_score" | awk '{print $1 / 10}') fi else anime_score=$(printf %s "$anime_score" | awk '{print $1 / 10}') @@ -254,7 +351,7 @@ function get-studios() { fi } function get-animes-season-year () { - anime_season=$( (jq '.data.Media.season' -r "$SCRIPT_FOLDER/config/data/anilist-$anilist_id.json" && jq '.data.Media.seasonYear' -r "$SCRIPT_FOLDER/config/data/anilist-$anilist_id.json") | paste -sd ' ' | tr '[:upper:]' '[:lower:]' | sed "s/\( \|^\)\(.\)/\1\u\2/g") + anime_season=$( (jq '.data.Media.seasonYear' -r "$SCRIPT_FOLDER/config/data/anilist-$anilist_id.json" && jq '.data.Media.season' -r "$SCRIPT_FOLDER/config/data/anilist-$anilist_id.json") | paste -sd ' ' | tr '[:upper:]' '[:lower:]' | sed "s/\( \|^\)\(.\)/\1\u\2/g") if [ "$anime_season" == "Null Null" ] then year_season=$(jq '.data.Media.startDate.year' -r "$SCRIPT_FOLDER/config/data/anilist-$anilist_id.json") @@ -272,43 +369,25 @@ function get-animes-season-year () { then name_season=Fall fi - anime_season=$(printf "%s %s" "$name_season" "$year_season") + anime_season=$(printf "%s %s" "$year_season" "$name_season") fi } -function download-airing-info () { - if [ ! -f "$SCRIPT_FOLDER/config/data/relations-$anilist_id.json" ] +function get-animes-award () { + award_check="" + cr_awards="" + if [[ $ANIME_AWARDS_NO_FVA == "Yes" ]] then - wait_time=0 - while [ $wait_time -lt 5 ]; - do - printf "%s\t\t\t - Downloading airing info for Anilist : %s\n" "$(date +%H:%M:%S)" "$anilist_id" | tee -a "$LOG" - curl -s 'https://graphql.anilist.co/' \ - -X POST \ - -H 'content-type: application/json' \ - --data '{ "query": "{ Media(type: ANIME, id: '"$anilist_id"') { relations { edges { relationType node { id type format title { romaji } status } } } } }" }' > "$SCRIPT_FOLDER/config/data/relations-$anilist_id.json" -D "$SCRIPT_FOLDER/config/tmp/anilist-limit-rate.txt" - rate_limit=0 - rate_limit=$(grep -oP '(?<=x-ratelimit-remaining: )[0-9]+' "$SCRIPT_FOLDER/config/tmp/anilist-limit-rate.txt") - ((wait_time++)) - if [[ -z $rate_limit ]] - then - printf "%s\t\t\t - Cloudflare limit rate reached watiting 60s\n" "$(date +%H:%M:%S)" | tee -a "$LOG" - sleep 61 - elif [[ $rate_limit -ge 3 ]] - then - sleep 0.8 - printf "%s\t\t\t - Done\n" "$(date +%H:%M:%S)" | tee -a "$LOG" - break - elif [[ $rate_limit -lt 3 ]] - then - printf "%s\t\t\t - Anilist API limit reached watiting 30s" "$(date +%H:%M:%S)" | tee -a "$LOG" - sleep 30 - break - elif [[ $wait_time == 4 ]] - then - printf "%s - Error can't download anilist data stopping script\n" "$(date +%H:%M:%S)" | tee -a "$LOG" - exit 1 - fi - done + award_check=$(jq --arg anilist_id "$anilist_id" '.[] | select( .anilist_id == $anilist_id ) | select(.cr_award | contains("English") or contains("Arabic") or contains("Spanish") or contains("Castilian") or contains("French")or contains("German") or contains("Italian") or contains("Portuguese") or contains("Russian") | not) | "AA " + .year + " " + .cr_award' -r "$SCRIPT_FOLDER/config/tmp/cr-award.json" | paste -s -d, -) > /dev/null + if [[ -n $award_check ]] + then + cr_awards=$award_check + fi + else + award_check=$(jq --arg anilist_id "$anilist_id" '.[] | select( .anilist_id == $anilist_id ) | "AA " + .year + " " + .cr_award' -r "$SCRIPT_FOLDER/config/tmp/cr-award.json" | paste -s -d, -) > /dev/null + if [[ -n $award_check ]] + then + cr_awards=$award_check + fi fi } function get-airing-status () { @@ -316,20 +395,22 @@ function get-airing-status () { then airing_status="Planned" else + airing_loop=0 anilist_backup_id=$anilist_id airing_status="Ended" last_sequel_found=0 sequel_multi_check=0 while [ $last_sequel_found -lt 50 ]; do + airing_loop=1 if [[ $sequel_multi_check -gt 0 ]] then anilist_multi_id_backup=$anilist_id :> "$SCRIPT_FOLDER/config/tmp/airing_sequel_tmp.json" while IFS=$'\n' read -r anilist_id do - download-airing-info - cat "$SCRIPT_FOLDER/config/data/relations-$anilist_id.json" >> "$SCRIPT_FOLDER/config/tmp/airing_sequel_tmp.json" + get-anilist-infos + cat "$SCRIPT_FOLDER/config/data/anilist-$anilist_id.json" >> "$SCRIPT_FOLDER/config/tmp/airing_sequel_tmp.json" done < "$SCRIPT_FOLDER/config/tmp/airing_sequel_tmp.txt" anilist_id=$anilist_multi_id_backup sequel_data=$(jq '.data.Media.relations.edges[] | select ( .relationType == "SEQUEL" ) | .node | select ( .format == "TV" or .format == "ONA" or .format == "MOVIE" or .format == "OVA" )' -r "$SCRIPT_FOLDER/config/tmp/airing_sequel_tmp.json") @@ -359,8 +440,8 @@ function get-airing-status () { fi fi else - download-airing-info - sequel_data=$(jq '.data.Media.relations.edges[] | select ( .relationType == "SEQUEL" ) | .node | select ( .format == "TV" or .format == "ONA" or .format == "MOVIE" or .format == "OVA" )' -r "$SCRIPT_FOLDER/config/data/relations-$anilist_id.json") + get-anilist-infos + sequel_data=$(jq '.data.Media.relations.edges[] | select ( .relationType == "SEQUEL" ) | .node | select ( .format == "TV" or .format == "ONA" or .format == "MOVIE" or .format == "OVA" )' -r "$SCRIPT_FOLDER/config/data/anilist-$anilist_id.json") if [ -z "$sequel_data" ] then airing_status="Ended" @@ -388,6 +469,7 @@ function get-airing-status () { fi fi done + airing_loop=0 anilist_id=$anilist_backup_id if [[ $last_sequel_found -ge 50 ]] then @@ -548,6 +630,30 @@ function get-season-rating-1 () { fi fi } +function get-cour-rating-1 () { + if [[ $RATING_1_SOURCE == "ANILIST" || $RATING_1_SOURCE == "MAL" ]] + then + if [[ $RATING_1_SOURCE == "ANILIST" ]] + then + get-score + score_1_cour=$anime_score + else + get-mal-score + score_1_cour=$anime_score + fi + score_1_cour=$(printf '%.*f\n' 1 "$score_1_cour") + if [[ "$score_1_cour" == 0.0 ]] + then + ((score_1_no_rating_cours++)) + fi + fi +} +function total-cour-rating-1 () { + if [[ $RATING_1_SOURCE == "ANILIST" || $RATING_1_SOURCE == "MAL" ]] + then + total_1_cours_score=$(echo | awk -v v1="$score_1_cour" -v v2="$total_1_cours_score" '{print v1 + v2}') + fi +} function total-rating-1 () { if [[ $RATING_1_SOURCE == "ANILIST" || $RATING_1_SOURCE == "MAL" ]] then @@ -563,11 +669,9 @@ function check-rating-1-valid () { then printf "%s\t\t - invalid rating for Anilist : %s skipping \n" "$(date +%H:%M:%S)" "$anilist_id" | tee -a "$LOG" else - if [[ $mal_id == 'null' ]] || [[ $mal_id == 0 ]] || [[ -z $mal_id ]] + get-mal-id + if [[ $mal_id != 'null' ]] || [[ -n $mal_id ]] then - printf "%s\t\t - Missing MAL ID for Anilist : %s / %s\n" "$(date +%H:%M:%S)" "$anilist_id" "$plex_title" | tee -a "$LOG" - printf "%s - Missing MAL ID for Anilist : %s / %s\n" "$(date +%H:%M:%S)" "$anilist_id" "$plex_title" >> "$MATCH_LOG" - else printf "%s\t\t - invalid rating for MAL : %s skipping \n" "$(date +%H:%M:%S)" "$mal_id" | tee -a "$LOG" fi fi @@ -620,6 +724,30 @@ function get-season-rating-2 () { fi fi } +function get-cour-rating-2 () { + if [[ $RATING_2_SOURCE == "ANILIST" || $RATING_2_SOURCE == "MAL" ]] + then + if [[ $RATING_2_SOURCE == "ANILIST" ]] + then + get-score + score_2_cour=$anime_score + else + get-mal-score + score_2_cour=$anime_score + fi + score_2_cour=$(printf '%.*f\n' 1 "$score_2_cour") + if [[ "$score_2_cour" == 0.0 ]] + then + ((score_2_no_rating_cours++)) + fi + fi +} +function total-cour-rating-2 () { + if [[ $RATING_2_SOURCE == "ANILIST" || $RATING_2_SOURCE == "MAL" ]] + then + total_2_cours_score=$(echo | awk -v v1="$score_2_cour" -v v2="$total_2_cours_score" '{print v1 + v2}') + fi +} function total-rating-2 () { if [[ $RATING_2_SOURCE == "ANILIST" || $RATING_2_SOURCE == "MAL" ]] then @@ -634,12 +762,10 @@ function check-rating-2-valid () { if [[ $RATING_2_SOURCE == "ANILIST" ]] then printf "%s\t\t - invalid rating for Anilist : %s skipping \n" "$(date +%H:%M:%S)" "$anilist_id" | tee -a "$LOG" - else - if [[ $mal_id == 'null' ]] || [[ $mal_id == 0 ]] || [[ -z $mal_id ]] + else + get-mal-id + if [[ $mal_id == 'null' ]] || [[ -n $mal_id ]] then - printf "%s\t\t - Missing MAL ID for Anilist : %s / %s\n" "$(date +%H:%M:%S)" "$anilist_id" "$plex_title" | tee -a "$LOG" - printf "%s - Missing MAL ID for Anilist : %s / %s\n" "$(date +%H:%M:%S)" "$anilist_id" "$plex_title" >> "$MATCH_LOG" - else printf "%s\t\t - invalid rating for MAL : %s skipping \n" "$(date +%H:%M:%S)" "$mal_id" | tee -a "$LOG" fi fi @@ -650,16 +776,23 @@ function check-rating-2-valid () { fi } function get-season-infos () { + override_id="" anilist_backup_id=$anilist_id season_check=$(jq --arg anilist_id "$anilist_id" '.[] | select( .anilist_id == $anilist_id ) | .tvdb_season' -r "$SCRIPT_FOLDER/config/tmp/list-animes-id.json") first_season=$(echo "$seasons_list" | awk -F "," '{print $1}') last_season=$(echo "$seasons_list" | awk -F "," '{print $NF}') total_seasons=$(echo "$seasons_list" | awk -F "," '{print NF}') + valid_anilist_id=$(jq --arg tvdb_id "$tvdb_id" '.[] | select( .tvdb_id == $tvdb_id ) | .anilist_id' -r "$SCRIPT_FOLDER/config/tmp/list-animes-id.json") + if awk -F"\t" '{print $2}' "$SCRIPT_FOLDER/config/$OVERRIDE" | grep -q -w "$anilist_backup_id" && [[ $last_season -eq 1 ]] + then + valid_anilist_id=1 + override_id=$anilist_backup_id + fi if [[ "$first_season" -eq 0 ]] then total_seasons=$((total_seasons - 1)) fi - if [[ $season_check != -1 ]] + if [ -n "$valid_anilist_id" ] && [[ $season_check != -1 ]] then total_1_score=0 total_2_score=0 @@ -668,6 +801,9 @@ function get-season-infos () { score_1_no_rating_seasons=0 score_2_no_rating_seasons=0 season_loop=0 + anime_season="" + award_check="" + cr_awards="" printf " seasons:\n" >> "$METADATA" IFS="," for season_number in $seasons_list @@ -676,59 +812,240 @@ function get-season-infos () { then printf " 0:\n label.remove: score\n" >> "$METADATA" else - if [[ $last_season -eq 1 && $IGNORE_S1 == "Yes" ]] + season_loop=1 + anilist_ids=$(jq --arg tvdb_id "$tvdb_id" --arg season_number "$season_number" '[.[] | select( .tvdb_id == $tvdb_id ) | select( .tvdb_season == $season_number )] | sort_by(.tvdb_epoffset) | .[].anilist_id' -r "$SCRIPT_FOLDER/config/tmp/list-animes-id.json" | paste -s -d, -) + if [ -n "$override_id" ] && [[ $season_number -eq 1 ]] then - anilist_id=$anilist_backup_id - get-season-rating-1 - get-season-rating-2 - if [[ $SEASON_YEAR == "Yes" ]] - then - get-animes-season-year - printf " 1:\n label: %s\n" "$anime_season" >> "$METADATA" - else - printf " 1:\n label.remove: score\n" >> "$METADATA" - fi - total-rating-1 - total-rating-2 - get-season-poster - else - season_loop=1 - anilist_id=$(jq --arg tvdb_id "$tvdb_id" --arg season_number "$season_number" '.[] | select( .tvdb_id == $tvdb_id ) | select( .tvdb_season == $season_number ) | select( .tvdb_epoffset == "0" ) | .anilist_id' -r "$SCRIPT_FOLDER/config/tmp/list-animes-id.json" | head -n 1) + anilist_ids=$anilist_backup_id + fi + cours_count_total=$(printf %s "$anilist_ids" | awk -F "," '{print NF}') + total_1_cours_score=0 + total_2_cours_score=0 + score_1_no_rating_cours=0 + score_2_no_rating_cours=0 + cours_count=0 + cour_status="" + all_cours_anime_season="" + season_userlist_type_add="" + seasons_userlist_type_remove="" + IFS=',' + for anilist_id in $anilist_ids + do + ((cours_count++)) if [[ -n "$anilist_id" ]] then get-anilist-infos - romaji_title=$(get-romaji-title) - english_title=$(get-english-title) - if [[ $MAIN_TITLE_ENG == "Yes" ]] + if jq '.data.Media.status' -r "$SCRIPT_FOLDER/config/data/anilist-$anilist_id.json" | grep -q -w "NOT_YET_RELEASED" + then + ((score_1_no_rating_cours++)) + ((score_2_no_rating_cours++)) + continue + fi + if { [[ $ANILIST_LISTS_LEVEL == "season" ]] || [[ $ANILIST_LISTS_LEVEL == "both" ]]; } && [[ $ANILIST_LISTS == "Yes" ]] + then + for userlist_type in Completed Watching Dropped Paused Planning + do + if grep -q -w "$anilist_id" "$SCRIPT_FOLDER/config/data/anilist-$ANILIST_USERNAME-$userlist_type.tsv" + then + userlist_type_count=$(printf %s "$season_userlist_type_add" | awk -F "," '{print NF}') + if [[ $userlist_type_count -gt 1 ]] + then + season_userlist_type_add=$(printf "%s,%s" "$season_userlist_type_add" "$userlist_type") + else + season_userlist_type_add="$userlist_type" + fi + fi + done + fi + get-cour-rating-1 + get-cour-rating-2 + if [[ $ANIME_AWARDS == "Yes" ]] then - english_title=$romaji_title + get-animes-award fi - get-season-rating-1 - get-season-rating-2 if [[ $SEASON_YEAR == "Yes" ]] then get-animes-season-year - if [[ $ALLOW_RENAMING == "Yes" && $RENAME_SEASONS == "Yes" ]] + if [[ $cours_count -gt 1 ]] + then + all_cours_anime_season=$(printf "%s,%s" "$anime_season" "$all_cours_anime_season") + else + all_cours_anime_season=$anime_season + fi + fi + else + printf "%s\t\t - Missing Anilist ID for tvdb : %s - Season : %s cour : %s / %s\n" "$(date +%H:%M:%S)" "$tvdb_id" "$season_number" "$cours_count" "$plex_title" | tee -a "$LOG" + printf "%s\t\t - Missing Anilist ID for tvdb : %s - Season : %s cour : %s / %s\n" "$(date +%H:%M:%S)" "$tvdb_id" "$season_number" "$cours_count" "$plex_title" >> "$MATCH_LOG" + ((score_1_no_rating_cours++)) + ((score_2_no_rating_cours++)) + fi + total-cour-rating-1 + total-cour-rating-2 + done + anime_season=$all_cours_anime_season + if [[ $RATING_1_SOURCE == "ANILIST" || $RATING_1_SOURCE == "MAL" ]] + then + if [[ "$total_1_cours_score" != 0 ]] + then + total_1_cours=$((cours_count - score_1_no_rating_cours)) + if [[ "$total_1_cours" != 0 ]] + then + score_1_season=$(echo | awk -v v1="$total_1_cours_score" -v v2="$total_1_cours" '{print v1 / v2}') + score_1_season=$(printf '%.*f\n' 1 "$score_1_season") + else + score_1_season=0 + ((score_1_no_rating_seasons++)) + fi + else + score_1_season=0 + ((score_1_no_rating_seasons++)) + fi + fi + if [[ $RATING_2_SOURCE == "ANILIST" || $RATING_2_SOURCE == "MAL" ]] + then + if [[ "$total_2_cours_score" != 0 ]] + then + total_2_cours=$((cours_count - score_2_no_rating_cours)) + if [[ "$total_2_cours" != 0 ]] + then + score_2_season=$(echo | awk -v v1="$total_2_cours_score" -v v2="$total_2_cours" '{print v1 / v2}') + score_2_season=$(printf '%.*f\n' 1 "$score_2_season") + else + score_2_season=0 + ((score_2_no_rating_seasons++)) + fi + else + score_1_season=0 + ((score_1_no_rating_seasons++)) + fi + fi + cours_count_total=0 + anilist_id=$(jq --arg tvdb_id "$tvdb_id" --arg season_number "$season_number" '.[] | select( .tvdb_id == $tvdb_id ) | select( .tvdb_season == $season_number ) | select( .tvdb_epoffset == "0" ) | .anilist_id' -r "$SCRIPT_FOLDER/config/tmp/list-animes-id.json" | head -n 1) + if [ -n "$override_id" ] && [[ $season_number -eq 1 ]] + then + anilist_id=$anilist_backup_id + fi + if [[ -z "$anilist_id" ]] + then + printf "%s\t\t - Missing Anilist ID for tvdb : %s - Season : %s / %s\n" "$(date +%H:%M:%S)" "$tvdb_id" "$season_number" "$plex_title" | tee -a "$LOG" + printf "%s - Missing Anilist ID for tvdb : %s - Season : %s / %s\n" "$(date +%H:%M:%S)" "$tvdb_id" "$season_number" "$plex_title" >> "$MATCH_LOG" + ((score_1_no_rating_seasons++)) + ((score_2_no_rating_seasons++)) + else + romaji_title=$(get-romaji-title) + english_title=$(get-english-title) + printf " %s:\n" "$season_number" >> "$METADATA" + if [[ $ALLOW_RENAMING == "Yes" && $RENAME_SEASONS == "Yes" ]] + then + printf " title: |-\n %s\n" "$romaji_title" >> "$METADATA" + fi + season_label_add="" + season_label_remove="" + if [[ -n "$cr_awards" ]] + then + if [[ -n "$season_label_add" ]] + then + season_label_add=$(printf "%s,AA Winner" "$season_label_add") + else + season_label_add="AA Winner" + fi + fi + if { [[ $ANILIST_LISTS_LEVEL == "season" ]] || [[ $ANILIST_LISTS_LEVEL == "both" ]]; } && [[ $ANILIST_LISTS == "Yes" ]] + then + seasons_userlist_type_remove="Completed,Watching,Dropped,Paused,Planning" + userlist_type_count=$(printf %s "$season_userlist_type_add" | awk -F "," '{print NF}') + if [[ -n $season_userlist_type_add ]] && [[ $userlist_type_count -gt 0 ]] + then + IFS="," + for userlist_type in $season_userlist_type_add + do + seasons_userlist_type_remove=$(printf "%s" "$seasons_userlist_type_remove" | sed s/"$userlist_type"// | sed 's/^,//' | sed 's/,,/,/g') + done + fi + if [[ -n "$season_userlist_type_add" ]] + then + if [[ -n "$season_label_add" ]] + then + season_label_add=$(printf "%s,%s" "$season_label_add" "$season_userlist_type_add") + else + season_label_add="$season_userlist_type_add" + fi + fi + if [[ -n "$seasons_userlist_type_remove" ]] + then + if [[ -n "$season_label_remove" ]] + then + season_label_remove=$(printf "%s,%s" "$season_label_remove" "$seasons_userlist_type_remove") + else + season_label_remove="$seasons_userlist_type_remove" + fi + fi + fi + if [[ -n "$anime_season" ]] + then + if [[ -n "$season_label_add" ]] + then + season_label_add=$(printf "%s,%s" "$season_label_add" "$anime_season") + else + season_label_add="$anime_season" + fi + fi + if [[ -n "$season_label_add" ]] + then + if [[ $last_season -eq 1 ]] + then + if [[ $IGNORE_S1_ONLY_RATING == "Yes" ]] then - printf " %s:\n title: |-\n %s\n user_rating: %s\n label: %s,score\n" "$season_number" "$romaji_title" "$score_1_season" "$anime_season" >> "$METADATA" + printf " label: %s\n" "$season_label_add" >> "$METADATA" else - printf " %s:\n user_rating: %s\n label: %s,score\n" "$season_number" "$score_1_season" "$anime_season" >> "$METADATA" + printf " label: Score,%s\n" "$season_label_add" >> "$METADATA" fi else - if [[ $ALLOW_RENAMING == "Yes" && $RENAME_SEASONS == "Yes" ]] + printf " label: Score,%s\n" "$season_label_add" >> "$METADATA" + fi + else + if [[ $last_season -eq 1 ]] + then + if [[ $IGNORE_S1_ONLY_RATING != "Yes" ]] + then + printf " label: Score\n" >> "$METADATA" + fi + fi + fi + if [[ -n "$season_label_remove" ]] + then + if [[ $last_season -eq 1 ]] + then + if [[ $IGNORE_S1_ONLY_RATING == "Yes" ]] then - printf " %s:\n title: |-\n %s\n user_rating: %s\n label: score\n" "$season_number" "$romaji_title" "$score_1_season" >> "$METADATA" + printf " label.remove: Score,%s\n" "$season_label_remove" >> "$METADATA" else - printf " %s:\n user_rating: %s\n label: score\n" "$season_number" "$score_1_season" >> "$METADATA" + printf " label.remove: %s\n" "$season_label_remove" >> "$METADATA" fi + else + printf " label.remove: %s\n" "$season_label_remove" >> "$METADATA" fi - total-rating-1 - total-rating-2 - get-season-poster else - printf "%s\t\t - Missing Anilist ID for tvdb : %s - Season : %s / %s\n" "$(date +%H:%M:%S)" "$tvdb_id" "$season_number" "$plex_title" | tee -a "$LOG" - printf "%s - Missing Anilist ID for tvdb : %s - Season : %s / %s\n" "$(date +%H:%M:%S)" "$tvdb_id" "$season_number" "$plex_title" >> "$MATCH_LOG" + if [[ $last_season -eq 1 ]] + then + if [[ $IGNORE_S1_ONLY_RATING == "Yes" ]] + then + printf " label.remove: Score\n" >> "$METADATA" + fi + fi fi + if [[ $last_season -eq 1 ]] + then + if [[ $IGNORE_S1_ONLY_RATING != "Yes" ]] + then + printf " user_rating: %s\n" "$score_1_season" >> "$METADATA" + fi + else + printf " user_rating: %s\n" "$score_1_season" >> "$METADATA" + fi + total-rating-1 + total-rating-2 + get-season-poster fi fi done @@ -847,28 +1164,11 @@ function write-metadata () { if [[ $DISABLE_TAGS != "Yes" ]] then anime_tags=$(get-tags) - printf " genre.sync: Anime,%s\n" "$anime_tags" >> "$METADATA" - fi - if [[ $media_type == "animes" ]] - then - printf "%s\t\t - Writing airing status\n" "$(date +%H:%M:%S)" | tee -a "$LOG" - if awk -F"\t" '{print "\""$1"\":"}' "$SCRIPT_FOLDER/config/data/ongoing.tsv" | grep -q -w "$tvdb_id" + if [[ "$ADD_ANIME_TAG" == "No" ]] then - printf " label: Airing\n" >> "$METADATA" - printf " label.remove: Planned,Ended\n" >> "$METADATA" - printf "%s\t\t - Done\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + printf " genre.sync: %s\n" "$anime_tags" >> "$METADATA" else - get-airing-status - if [[ $airing_status == Planned ]] - then - printf " label: Planned\n" >> "$METADATA" - printf " label.remove: Airing,Ended\n" >> "$METADATA" - printf "%s\t\t - Done\n" "$(date +%H:%M:%S)" | tee -a "$LOG" - else - printf " label: Ended\n" >> "$METADATA" - printf " label.remove: Planned,Airing\n" >> "$METADATA" - printf "%s\t\t - Done\n" "$(date +%H:%M:%S)" | tee -a "$LOG" - fi + printf " genre.sync: Anime,%s\n" "$anime_tags" >> "$METADATA" fi fi get-studios @@ -879,7 +1179,7 @@ function write-metadata () { get-poster if [[ $media_type == "animes" ]] then - if [[ $IGNORE_SEASONS == "Yes" ]] || [[ $override_seasons_ignore == "Yes" ]] + if [[ $IGNORE_SEASONS == "Yes" ]] || [[ $override_seasons_ignore == "yes" ]] then get-rating-1 get-rating-2 @@ -892,9 +1192,107 @@ function write-metadata () { get-rating-1 get-rating-2 fi + label_add="" + label_remove="" + if [[ $media_type == "animes" ]] + then + printf "%s\t\t - Writing airing status\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + if awk -F"\t" '{print "\""$1"\":"}' "$SCRIPT_FOLDER/config/data/ongoing.tsv" | grep -q -w "$tvdb_id" + then + label_add="Airing" + label_remove="Planned,Ended" + printf "%s\t\t - Done\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + else + get-airing-status + if [[ $airing_status == Planned ]] + then + label_add="Planned" + label_remove="Airing,Ended" + printf "%s\t\t - Done\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + else + label_add="Ended" + label_remove="Planned,Airing" + printf "%s\t\t - Done\n" "$(date +%H:%M:%S)" | tee -a "$LOG" + fi + fi + fi + if [[ -n $cr_awards ]] + then + if [[ -n "$label_add" ]] + then + label_add=$(printf "AA Winner,%s" "$label_add") + else + label_add="AA Winner" + fi + else + get-animes-award + if [[ -n $cr_awards ]] + then + if [[ -n "$label_add" ]] + then + label_add=$(printf "AA Winner,%s" "$label_add") + else + label_add="AA Winner" + fi + fi + fi + if { [[ $ANILIST_LISTS_LEVEL == "show" ]] || [[ $ANILIST_LISTS_LEVEL == "both" ]]; } && [[ $ANILIST_LISTS == "Yes" ]] + then + all_anilist_ids="" + userlist_type_add="" + userlist_type_remove="Completed,Watching,Dropped,Paused,Planning" + for userlist_type in Completed Watching Dropped Paused Planning + do + if [[ $media_type == "animes" ]] + then + all_anilist_ids=$(jq --arg tvdb_id "$tvdb_id" '.[] | select( .tvdb_id == $tvdb_id ) | .anilist_id' -r "$SCRIPT_FOLDER/config/tmp/list-animes-id.json" | paste -s -d, - | sed 's/,/\\|/g') + else + all_anilist_ids=$anilist_id + fi + if grep -q -w "$all_anilist_ids" "$SCRIPT_FOLDER/config/data/anilist-$ANILIST_USERNAME-$userlist_type.tsv" + then + userlist_type_count=$(printf %s "$userlist_type_add" | awk -F "," '{print NF}') + if [[ $userlist_type_count -gt 1 ]] + then + userlist_type_add=$(printf "%s,%s" "$userlist_type_add" "$userlist_type") + else + userlist_type_add=$userlist_type + fi + userlist_type_remove=$(printf "%s" "$userlist_type_remove" | sed s/"$userlist_type"// | sed 's/^,//' | sed 's/,,/,/g') + fi + done + if [[ -n "$userlist_type_add" ]] + then + if [[ -n "$label_add" ]] + then + label_add=$(printf "%s,%s" "$label_add" "$userlist_type_add") + else + label_add="$userlist_type_add" + fi + fi + if [[ -n "$userlist_type_remove" ]] + then + if [[ -n "$label_remove" ]] + then + label_remove=$(printf "%s,%s" "$label_remove" "$userlist_type_remove") + else + label_remove="$userlist_type_remove" + fi + fi + fi + if [[ -n "$label_add" ]] + then + printf " label: %s\n" "$label_add" >> "$METADATA" + fi + if [[ -n "$label_remove" ]] + then + printf " label.remove: %s\n" "$label_remove" >> "$METADATA" + fi tvdb_id="" imdb_id="" anilist_id="" mal_id="" override_seasons_ignore="" + award_check="" + cr_awards="" } \ No newline at end of file diff --git a/movies-renamer.sh b/movies-renamer.sh index 957a33a..352a01f 100755 --- a/movies-renamer.sh +++ b/movies-renamer.sh @@ -30,14 +30,19 @@ fi :> "$MATCH_LOG" printf "%s - Starting movies script\n\n" "$(date +%H:%M:%S)" | tee -a "$LOG" -# Download anime mapping json data +# Download animes mapping json data & anilist userlist download-anime-id-mapping - +get-anilist-userlist # export movies list from plex printf "%s - Creating animes list\n" "$(date +%H:%M:%S)" | tee -a "$LOG" printf "%s\t - Exporting Plex animes library\n" "$(date +%H:%M:%S)" | tee -a "$LOG" -python3 "$SCRIPT_FOLDER/plex_movies_export.py" +if [ -f "$SCRIPT_FOLDER/romaji-renamer-venv/bin/python3" ] +then + "$SCRIPT_FOLDER/romaji-renamer-venv/bin/python3" "$SCRIPT_FOLDER/plex_movies_export.py" +else + python3 "$SCRIPT_FOLDER/plex_movies_export.py" +fi printf "%s\t - Done\n" "$(date +%H:%M:%S)" | tee -a "$LOG" # create ID/movies.tsv ( imdb_id | mal_id | anime_title | plex_title ) diff --git a/plex_animes_export.py b/plex_animes_export.py index 57f4b09..1e54949 100644 --- a/plex_animes_export.py +++ b/plex_animes_export.py @@ -17,23 +17,23 @@ plex = PlexServer(url, token, timeout=300) animes = plex.library.section(ANIME_LIBRARY_NAME) with open(Path(basedir, "config/tmp/plex_animes_export.tsv"), "w") as export_plex, open(Path(basedir, "config/tmp/plex_failed_animes.tsv"), "w") as export_fail: - for video in animes.search(): - title = str(video.title) - ids = str(video.guids) - tvdbid = re.search("(?<=tvdb://)(\d+)", ids) - if ( tvdbid ) : - tvdb = str(tvdbid.group(1)) - location = str(video.locations)[2:-2] - if (re.match("^.*(\\\\.*)$", location)) : - folder = str(PureWindowsPath(location).name) - else : - folder = str(PurePosixPath(location).name) - seasons = str(video.seasons()) - seasonslist = re.findall("\-(\d*)\>", seasons) - cleanseasonslist = ',' .join(seasonslist) - export=(tvdb + "\t" + title + "\t" + folder + "\t" + str(cleanseasonslist) + "\n") - export_plex.write(export) - else : - export=(title + " no id found" + ids + "\n") - export_fail.write(export) - print(export) \ No newline at end of file + for video in animes.search(): + title = str(video.title) + ids = str(video.guids) + tvdbid = re.search("(?<=tvdb://)(\\d+)", ids) + if ( tvdbid ) : + tvdb = str(tvdbid.group(1)) + location = str(video.locations)[2:-2] + if (re.match("^.*(\\\\.*)$", location)) : + folder = str(PureWindowsPath(location).name) + else : + folder = str(PurePosixPath(location).name) + seasons = str(video.seasons()) + seasonslist = re.findall("\\-(\\d*)\\>", seasons) + cleanseasonslist = ',' .join(seasonslist) + export=(tvdb + "\t" + title + "\t" + folder + "\t" + str(cleanseasonslist) + "\n") + export_plex.write(export) + else : + export=(title + " no id found" + ids + "\n") + export_fail.write(export) + print(export) \ No newline at end of file diff --git a/plex_movies_export.py b/plex_movies_export.py index afa6ace..0c8998d 100644 --- a/plex_movies_export.py +++ b/plex_movies_export.py @@ -19,7 +19,7 @@ for video in movies.search(): title = str(video.title) ids = str(video.guids) - imdbid = re.search("(?<=imdb://)(tt\d+)", ids) + imdbid = re.search("(?<=imdb://)(tt\\d+)", ids) if ( imdbid ) : imdb = str(imdbid.group(1)) location = str(video.locations)[2:-2] diff --git a/plex-romaji-renamer.sh b/romaji-renamer.sh old mode 100755 new mode 100644 similarity index 63% rename from plex-romaji-renamer.sh rename to romaji-renamer.sh index 6290b59..7d9bdf5 --- a/plex-romaji-renamer.sh +++ b/romaji-renamer.sh @@ -7,7 +7,12 @@ SCRIPT_FOLDER=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd) source "$SCRIPT_FOLDER/config/.env" source "$SCRIPT_FOLDER/VERSION" source "$SCRIPT_FOLDER/functions.sh" -printf "%s - Plex-Romaji-Renamer v%s\n" "$(date +%H:%M:%S)" "$version" | tee -a "$LOG" +if ! grep -q -w "$version" "$SCRIPT_FOLDER/config/version_last_run.txt" +then + rm "$SCRIPT_FOLDER/config/data/*.json" +fi +printf "%s" "$version" > "$SCRIPT_FOLDER/config/version_last_run.txt" +printf "%s - Romaji-Renamer v%s\n" "$(date +%H:%M:%S)" "$version" | tee -a "$LOG" locale=$(locale -a | grep -i "utf" | head -n 1) if [ -z "$locale" ] then @@ -23,15 +28,15 @@ then fi if [ ! -f "$SCRIPT_FOLDER/config/default.env" ] then -curl -s "https://raw.githubusercontent.com/Arial-Z/Plex-Romaji-Renamer/dev/config/default.env" > "$SCRIPT_FOLDER/config/default.env" +curl -s "https://raw.githubusercontent.com/Arial-Z/Romaji-Renamer/dev/config/default.env" > "$SCRIPT_FOLDER/config/default.env" fi if [ ! -f "$SCRIPT_FOLDER/config/override-ID-animes.example.tsv" ] then -curl -s "https://raw.githubusercontent.com/Arial-Z/Plex-Romaji-Renamer/dev/config/override-ID-animes.example.tsv" > "$SCRIPT_FOLDER/config/override-ID-animes.example.tsv" +curl -s "https://raw.githubusercontent.com/Arial-Z/Romaji-Renamer/dev/config/override-ID-animes.example.tsv" > "$SCRIPT_FOLDER/config/override-ID-animes.example.tsv" fi if [ ! -f "$SCRIPT_FOLDER/config/override-ID-movies.example.tsv" ] then -curl -s "https://raw.githubusercontent.com/Arial-Z/Plex-Romaji-Renamer/dev/config/override-ID-movies.example.tsv" > "$SCRIPT_FOLDER/config/override-ID-movies.example.tsv" +curl -s "https://raw.githubusercontent.com/Arial-Z/Romaji-Renamer/dev/config/override-ID-movies.example.tsv" > "$SCRIPT_FOLDER/config/override-ID-movies.example.tsv" fi # sleep infinity if [ -f "$SCRIPT_FOLDER/config/.env" ] diff --git a/seasonal-animes-download.sh b/seasonal-animes-download.sh index 276a742..5ebe0e0 100755 --- a/seasonal-animes-download.sh +++ b/seasonal-animes-download.sh @@ -21,9 +21,9 @@ fi printf "%s - Starting script\n\n" "$(date +%H:%M:%S)" | tee -a "$LOG" download-anime-id-mapping printf "%s - checking current season\n" "$(date +%H:%M:%S)" | tee -a "$LOG" -curl -s -L -A "Mozilla/5.0 (X11; Linux x86_64)" "https://livechart.me/" -o "$SCRIPT_FOLDER/config/tmp/this-season.html" -season=$(awk -v IGNORECASE=1 -v RS=']*>/,"");print;exit}' "$SCRIPT_FOLDER/config/tmp/this-season.html" | awk '{print $1}'| tr '[:lower:]' '[:upper:]') -year=$(awk -v IGNORECASE=1 -v RS=']*>/,"");print;exit}' "$SCRIPT_FOLDER/config/tmp/this-season.html" | awk '{print $2}') +curl -s -L -A "Mozilla/5.0 (X11; Linux x86_64)" "https://anidb.net/anime/season/" -o "$SCRIPT_FOLDER/config/tmp/this-season.html" +season=$(awk -v IGNORECASE=1 -v RS=']*>/,"");print;exit}' "$SCRIPT_FOLDER/config/tmp/this-season.html" | awk '{print $4}'| tr '[:lower:]' '[:upper:]') +year=$(awk -v IGNORECASE=1 -v RS=']*>/,"");print;exit}' "$SCRIPT_FOLDER/config/tmp/this-season.html" | awk '{print $5}') printf "%s - Current season : %s %s\n\n" "$(date +%H:%M:%S)" "$season" "$year" | tee -a "$LOG" printf "%s - Creating seasonal list\n" "$(date +%H:%M:%S)" | tee -a "$LOG" printf "%s\t - Downloading anilist season list\n" "$(date +%H:%M:%S)" | tee -a "$LOG"