Compare commits

...

6 Commits

2 changed files with 202 additions and 0 deletions

View File

@ -0,0 +1,74 @@
#!/bin/bash
#Check if requests is installed, and if not install it
if [[ -f /tmp/request_install_lock ]]; then
while [[ -f /tmp/requests_install_lock ]]; do
sleep 1
done
else
python -c "import requests"
if [[ "$?" == "1" ]]; then
echo '' > /tmp/requests_install_lock
apt update
apt install python-requests
rm /tmp/requests_install_lock
fi
fi
#Gets the last part of the current directory
function get_bottom_dir() {
IFS='/';
read -ra ADDR <<< "$PWD";
echo "${ADDR[-1]}";
IFS=' ';
}
#Removes braces, parenteres along with everything in them and strips leading and trailing whitespace
function name_clean() {
local _out=$(echo "$1" | sed -e 's/\[[^][]*\]//g');
_out=$(echo "$_out" | sed -e 's/([^()]*)//g');
_out=$(echo "$_out" | sed 's/_/ /g');
echo $(echo "$_out" | xargs);
}
function get_series() {
local sanitized_name=$(name_clean "$1");
local output=$(python /scripts/seasons.py "$sanitized_name");
if [[ "$?" != "0" ]]; then
return 1;
fi
IFS="|";
read -ra STR <<< "$output"
TITLE_ROMAJI="${STR[0]}";
TITLE_ENGLISH="${STR[1]}";
SEASON="${STR[2]}";
IFS=" ";
return 0;
}
cd "$1";
bottom_dir=$(get_bottom_dir);
cleaned_bottom_dir=$(name_clean "$bottom_dir");
get_series "$cleaned_bottom_dir";
if [[ "$?" == "0" ]]; then
if [[ -d "$JF_DIR/$TITLE_ROMAJI" ]]; then
cleaned_dir="$TITLE_ROMAJI/Season $SEASON";
elif [[ -d "$JF_DIR/$TITLE_ENGLISH" ]]; then
cleaned_dir="$TITLE_ENGLISH/Season $SEASON";
else
cleaned_dir="$TITLE_ROMAJI/Season $SEASON";
fi
else
cleaned_dir="$cleaned_bottom_dir";
fi
mkdir -p "$JF_DIR/$cleaned_dir";
for i in *; do
cleaned_name=$(name_clean "$i");
ln "$PWD/$i" "$JF_DIR/$cleaned_dir/$cleaned_name" >/dev/null 2>/dev/null;
done;
echo "$JF_DIR/$cleaned_dir";

128
anime_scripts/seasons.py Normal file
View File

@ -0,0 +1,128 @@
from requests import post
from sys import argv
BASE_URL = "https://graphql.anilist.co"
class SortableAnime:
def __init__(self, id, year, reltype, title, frmt):
self.id = id
self.year = year if year else 9999
self.type = reltype
self.title = title
self.frmt = frmt
def dict(self):
return {
"id": self.id,
"year": self.year,
"type": self.type,
"title": self.title,
"format": self.frmt
}
def __eq__(self, other):
return self.id == other.id
def __hash__(self):
return self.id
def __str__(self):
return str(self.title)
def search(query):
response = post(BASE_URL, json={
'query': """
query ($q: String) {
Media (search: $q) {
id
}
}
""",
'variables': {"q": query}
})
if response.ok:
return response.json()["data"]["Media"]["id"]
raise ValueError("Show does not exist")
def get_show(id):
response = post(BASE_URL, json={
'query': """
query ($id: Int) {
Media (id: $id) {
id
title {
english
romaji
}
startDate {
year
}
format
relations {
nodes {
id
format
startDate {
year
}
title {
english
romaji
}
}
edges{
relationType
}
}
}
}
""",
'variables': {"id": id}
})
if response.ok:
return response.json()
raise ValueError("Bad show")
def get_base_show(res):
base = res["data"]["Media"]
return SortableAnime(base["id"], base["startDate"]["year"], "BASE", base["title"], base["format"])
def process_shows(res):
ls = []
ls.append(get_base_show(res))
for i,v in enumerate(res["data"]["Media"]["relations"]["nodes"]):
ls.append(SortableAnime(v["id"], v["startDate"]["year"], res["data"]["Media"]["relations"]["edges"][i]["relationType"], v["title"], v["format"]))
pass
return ls
def main(query):
items = []
show_id = search(query)
res = get_show(show_id)
items.extend(process_shows(res))
base_show = get_base_show(res)
if "PREQUEL" not in [i.type for i in items]:
season = 1
else:
ignore = []
while True:
f = False
for i in items:
if i.type == "PREQUEL" and i not in ignore:
fi = [i for i in process_shows(get_show(i.id)) if i not in items]
items.extend(fi)
ignore.append(i)
f = True
if not f:
break
items = [i for i in items if i.frmt == "TV" and (i.type == "PREQUEL" or i.type == "SEQUEL" or i.type == "BASE")]
items.sort(key=lambda i: i.year)
season = items.index(base_show)+1
return season, base_show, items
if __name__ == "__main__":
season, show, items = main(argv[1])
base_title = items[0].title
print("{}|{}|{}".format(base_title["romaji"], base_title["english"], season))