Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@theKAKAN
Created February 22, 2021 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theKAKAN/dc18614e4a6fd9bfd15a8856640e237f to your computer and use it in GitHub Desktop.
Save theKAKAN/dc18614e4a6fd9bfd15a8856640e237f to your computer and use it in GitHub Desktop.
Fetch your "Completed" section of anime or manga and their IDs as well as their ratings using bash and put them into a CSV
#!/bin/bash
# Config
USERNAME="KAKAN"
mediaType="ANIME"
# Let's get it using HTTP
AFTER=$( http --pretty none -b -f https://kitsu.io/api/graphql query="query{findProfileBySlug(slug: \"$USERNAME\"){library{completed(first:100, mediaType:$mediaType){pageInfo{startCursor}}}}}" \
| jq .data.findProfileBySlug.library.completed.pageInfo.startCursor )
hasNextPage=true
# Let's reset the CSV file
echo "Anime ID, Rating" > info.csv
while $hasNextPage ; do
# Remove the first and last quotes; "Mx" becomes Mx
AFTER="${AFTER%\"}"
AFTER="${AFTER#\"}"
query="query {
findProfileBySlug(slug:\"$USERNAME\"){
library {
completed( first: 100, mediaType:$mediaType, after:\"$AFTER\" ){
pageInfo {
hasNextPage
endCursor
}
nodes {
media {
id
}
rating
}
}
}
}
}"
# The script should be one liner with updated variables
script="$(echo $query)"
output=$( http --pretty none --style=bw -b -f https://kitsu.io/api/graphql query="$script" )
hasNextPage=$( echo "$output" | jq .data.findProfileBySlug.library.completed.pageInfo.hasNextPage )
AFTER=$( echo "$output" | jq .data.findProfileBySlug.library.completed.pageInfo.endCursor )
# Anime ID
id=$(echo "$output" | jq .data.findProfileBySlug.library.completed.nodes[].media.id)
# Remove the quotes from here as well
# Also makes it a one-liner separated by spaces
# Also, turn it into an array
id=( $( echo $id | sed 's/"//g' ) )
# Rating
rating=$( echo "$output" | jq .data.findProfileBySlug.library.completed.nodes[].rating)
# Make it a one liner and turn it into an array
rating=( $( echo $rating ) )
# Let's get array length
length=${#id[@]}
for(( i=0; i < ${length}; i++ ));
do
echo "${id[i]},${rating[i]}" >> info.csv
done
echo "Inserted round $AFTER"
# PHEW
done
echo "Inserted everything successfully"
@theKAKAN
Copy link
Author

I hate myself for this horrible code, but this took me some time to write as well xD

So, to use it: download the file, set your username and the mediaType( can be either ANIME or MANGA, in caps ) and run the file.
It should put everything in a info.csv file
Do note that subsequent runs overwrites that file, so keep it safe.

@theKAKAN
Copy link
Author

Oh, you'll need HTTPie and jq for this to work. I plan to switch to curl someday

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment