I introduce advancedplexapi.sh; walk through the plex api without ever using an api token or a key

Hey there,
First of all, I want to say that I don’t have a question or problem. I made this to show you something that I made. I think that other people will find it usefull as well, so I’m sharing it in this post.

What do you mean with it ?
I made a bash script.

What does it do?
After changing the settings to you liking and inserting a few things (api token, log folder), you can run the script. The first thing you see is the /library/sections output of the api (the first output can be changed). With under it a “bar” as I call it. The bar gives you options on what to do. You can choose one of the sections or go back to “root”.

When you choose one of the sections and press enter… you go that section (e.g. /library/sections/2/all) and get the output of the api. Want to go back to /library/sections? Just type “back” (that is an option as it is visible in the bar) and you’ll see the sections output again. Want to get to the api page of the 5th episode of the 2nd season of Modern Family? Just type the following (output of the api between the bars are not shown here, but are when you use the script):

[[api output of /library/sections]]
Choose exit | back | Films/TV Series: TV Series
[[api output of /library/sections/2/all]]
Choose back | exit | series: series
--------------------------------
Agatha Christie's Poirot
Another Life
Arrow
Avatar: The Last Airbender
Avatar: The Legend of Korra
Cutthroat Kitchen
Designated Survivor
Devs
ER
Friends
Initial D
Into the Night
Mad Men
Mayday
Modern Family
Origin
Outer Banks
Pokémon
Prison Break
Scorpion
The Man in the High Castle
The Office
--------------------------------
cancel | Choose one of these series: Modern Family
[[api output of /library/metadata/1823/children]]
Choose back | exit | [season number]: 2
[[api output of /library/metadata/1849]]
Choose back | exit | [episode number]: 5
[[api output of /library/metadata/1854]]
Choose back | exit | subtitle:

And now you want to go back to see information about the movie 1917?

Choose back | exit | subtitle: back
[[api output of /library/metadata/1849]]
Choose back | exit | [episode number]: back
[[api output of /library/metadata/1823/children]]
Choose back | exit | [season number]: back
[[api output of /library/sections/2/all]]
Choose back | exit | series: back
[[api output of /library/sections]]
Choose exit | back | Films/TV Series: Films
[[api output of /library/sections/1/all]]
Choose back | exit | movies: movies
--------------------------------
1917
2 Fast 2 Furious
6 Underground
Baby Driver
Back to the Future
Back to the Future Part II
Back To The Future Part III
--------------------------------
cancel | choose one of these movies: this is a test
cancel | choose one of these movies: 1918
cancel | choose one of these movies: 1917
[[api output of /library/metadata/4647]]
Choose back | exit | subtitle:

The plex api manual says that starting from /library/sections, you can “walk through it”. But when I used the api, you had to make an api request, find what you wanted in to massive list, copy the key, make a new request where you replace the old key with the new one. I found it very inefficient. So I made a script that makes it as easy as possible to walk through it. Want to go the the series Prison Break? Just type series and Prison Break and done.

So what I have showed you now is a script that can walk through the /library section of the api. But I’m not done. When you’re at the /library/sections page, you can type “back” to go to the home page of the script.

--------------------------------
/
pagesearcher
sessions
library
history
search
servers
refresh
onDeck
prefs
account
system
agents
--------------------------------
exit | Choose one of these options:

Here you see every api option that the plex api has. Just type history, and you’ll be taken to /status/sessions/history/all automatically. And there, the bar gives you special options, unique for the history page, but you can always type “back” to go back to “root” as I call this.

Another feature of the plex api is being able to give the section ID of a section and being able to refresh it. This can also easily be done using “refresh”.

exit | Choose one of these options: refresh
Choose Films/TV Series: Films

And done. You refreshed the Films folder. Without using the script, you’d be finding the sectionID out of the folder you wanted to refresh and then doing /library/sections/[section ID]/refresh.
With the script I made, the whole api is much easier to navigate and use.

Everything is case insensitive (servers, prefs, back) unless the name is using uppercase letters (onDeck, Prison Break, Films). Then it’s not case insensitive.

That’s it?
The script has many features. Alot are specific to the location (history, sessions (when someone is watching)), but when they’re available, you’ll be able to see the option in the bar. I’m not explaining everything the script can do in this post. You can easily find it out yourself if you copy the script and try it.

But why?
I hated how inefficient it was to navigate the api. And I always forgot the options or how they should by typed (I always forgot /status/sessions/history/all). So I wanted to make it as easy and efficient as possible.

How long did it take?
Way to long (probably about 25 hours), but I’m still learning and I learned greatly from making this script.

What do you mean “I’m still learning”?
I’m 15 years old, not an 35 year old IT specialist that has been scripting since there were 2 years old that has studied at an university.

Are you giving the code or what?
Before I’m giving the code, there’s something important that I should mention.

The code is inside a file named advancedplexapi.sh. Then there’s a second file, advancedplexapivar.sh, that is also needed. You’ll never run the advancedplexapivar.sh file. Only advancedplexapi.sh. The script can be everywhere on your computer, but the two files need to be in the same folder and advancedplexapi.sh needs to have read and write access to advancedplexapivar.sh.

Advancedplexapi.sh

#! /bin/bash

plexapitoken="" #your plex api token without X-Plex-Token=, just the string
ipplexserver="192.168.2.15" #internal ip adress of your plex server
logfolder="/home/cas/scripts/logs" # complete path to the FOLDER to save the log file in; without / at the end.

echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | user started script" >> $logfolder/advancedplexapi.log 2>&1

n=0
re='^[0-9]+$'
while [ $n = 0 ]
do
#commenting the clear command below will have the following effect:
#commented/inactive=after choosing an option, leading to a new page, the previous page won't be cleared. You can scroll back up to see your history
#un-commented/working as a command=after choosing an option, leading to a new page, the previous page will be cleared. You cant sroll back, but it will make it cleaner and easier to navigate or view the the output of the page you're on.
clear

        path=$(cat advancedplexapivar.sh | head -n 1)
        location="http://$ipplexserver:32400$path?X-Plex-Token=$plexapitoken"
        echo "$(date '+%d/%m/%Y %H:%M:%S') |     path      | $path" >> $logfolder/advancedplexapi.log 2>&1
        if ! [[ "$path" = "root" ]]
        then
                GET http://$ipplexserver:32400$path?X-Plex-Token=$plexapitoken
        fi

        if [[ $path = "root" ]]
        then
                echo "--------------------------------"
                echo -e "/\npagesearcher\nsessions\nlibrary\nhistory\nsearch\nservers\nrefresh\nonDeck\nprefs\naccount\nsystem\nagents"
                echo "--------------------------------"
                read -rp "exit | Choose one of these options: " option

        elif [[ $path = /library/sections/$(curl -sSL http://$ipplexserver:32400/library/sections?X-Plex-Token=$plexapitoken | grep -Eo "key\=\"[0-9]{1,4}\"\ type\=\"show\"" | grep -Eo "[0-9]{1,4}")/all ]]
        then
                read -rp "Choose back | exit | series: " option
                if ! [[ $option = back || exit || series ]]
                then
                        echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | that option is not possible here" >> $logfolder/advancedplexapi.log
                fi

        elif [[ $path = /library/onDeck ]]
        then
                read -rp "Choose back | exit: " option

        elif [[ $(curl -sSL $location | grep -o "streamType\=\"3\"" | head -n1) ]]
        then
                if [[ $(curl -sSL $location | grep "streamType\=\"3\"" | grep -o key | head -n1) ]]
                then
                        read -rp "Choose back | exit | subtitle: " option
                else
                        read -rp "Choose back | exit: " option
                fi

        elif [[ $path = /system || \
                $path = /system/agents || \
                $path = /myplex/account || \
                $path = "/:/prefs" || \
                $path = / || \
                $path = /status/sessions || \
                $path = /servers || \
                $(curl -sSL $location | grep -o "streamType" | head -n1) || \
                $(curl -sSL $location | grep -o '\-->' | head -n1) ]]
        then
                r=0
                while [ $r = 0 ]
                do
                read -rp "Choose back | exit: " suboption
                if [[ $suboption = "back" ]] || [[ $suboption = "exit" ]]
                then
                        option=$suboption
                        r=1
                else
                        r=0
                        echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | that option is not possible here" >> $logfolder/advancedplexapi.log
                fi
                done

        elif [[ $path = /status/sessions/history/all ]]
        then
                read -rp "Choose back | exit | list: " option

        elif [[ $path = /library/sections ]]
        then
                read -rp "Choose exit | back | $(curl -sSL $location | grep -o "title\=\".*\"\ agent" | grep -o "\".*\"" | grep -o "[a-zA-Z0-9].*[a-zA-Z0-9]" | paste -sd/): " option

        elif [[ $(curl -sSL $location | grep -o "type\=\"season\"" | head -n1) ]]
        then
                read -rp "Choose back | exit | [season number]: " option

        elif [[ $(curl -sSL $location | grep -o "type\=\"episode\"" | head -n1) ]]
        then
                read -rp "Choose back | exit | [episode number]: " option

        elif [[ $(curl -sSL $location | grep -o "type\=\"movie\"" | head -n1) ]]
        then
                read -rp "Choose back | exit | movies: " option

        fi

        echo "$(date '+%d/%m/%Y %H:%M:%S') |     option    | $option" >> $logfolder/advancedplexapi.log 2>&1

                if [[ $option =~ $re ]]
                then
                        if [[ $(curl -sSL "$location" | grep -o "type\=\"episode\"" | head -n1) ]] || [[ $(curl -sSL "$location" | grep -o "type\=\"season\"" | head -n1) ]]
                        then
                                if [[ $(curl -sSL "$location" | grep "index\=\"$option\"") ]]
                                then
                                        if [[ $(curl -sSL "$location" | grep -o "type\=\"episode\"" | head -n1) ]]
                                        then
                                                newlocation=$(curl -sSL "$location" | grep "index\=\"$option\"" | grep -Eo "key\=\"/library/metadata/[0-9]{1,5}\"" | grep -Eo "/.*[0-9]")
                                                sed -i "s|$path|$newlocation|g" advancedplexapivar.sh
                                        else
                                                newlocation=$(curl -sSL "$location" | grep "index\=\"$option\"" | grep -o "key\=\".*/children" | grep -o "/.*/children")
                                                sed -i "s|$path|$newlocation|g" advancedplexapivar.sh
                                        fi
                                else
                                        if [[ $(curl -sSL "$location" | grep -o "type\=\"episode\"" | head -n1) ]]
                                        then
                                                echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | that episode number doesn't exist" >> $logfolder/advancedplexapi.log 2>&1
                                        else
                                                echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | that season number doesn't exist" >> $logfolder/advancedplexapi.log 2>&1
                                        fi
                                fi
                        else
                                echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | inputing a number here is not an option" >> $logfolder/advancedplexapi.log 2>&1
                        fi

                elif [[ ${option,,} = library ]]
                then
                        newlocation=/library/sections
                        sed -i "s|$path|$newlocation|g" advancedplexapivar.sh

                elif [[ ${option,,} = / ]]
                then
                        newlocation=/
                        sed -i "s|$path|$newlocation|g" advancedplexapivar.sh

                elif [[ ${option,,} = history ]]
                then
                        newlocation=/status/sessions/history/all
                        sed -i "s|$path|$newlocation|g" advancedplexapivar.sh

                elif [[ ${option,,} = agents ]]
                then
                        newlocation=/system/agents
                        sed -i "s|$path|$newlocation|g" advancedplexapivar.sh

                elif [[ ${option,,} = system ]]
                then
                        newlocation=/system
                        sed -i "s|$path|$newlocation|g" advancedplexapivar.sh

                elif [[ ${option,,} = account ]]
                then
                        newlocation=/myplex/account
                        sed -i "s|$path|$newlocation|g" advancedplexapivar.sh

                elif [[ ${option,,} = prefs ]]
                then
                        newlocation=/:/prefs
                        sed -i "s|$path|$newlocation|g" advancedplexapivar.sh

                elif [[ $option = onDeck ]]
                then
                        newlocation=/library/onDeck
                        sed -i "s|$path|$newlocation|g" advancedplexapivar.sh

                elif [[ ${option,,} = pagesearcher ]]
                then
                        p=0
                        while [ $p = 0 ]
                        do
                        read -rp "cancel | Give key: " pagesearcherkey
                        if [[ ${pagesearcherkey,,} = cancel ]]
                        then
                                p=1
                                newlocation="root"
                                sed -i "s|$path|$newlocation|g" advancedplexapivar.sh
                        else
                                if [[ $(curl -sSL http://$ipplexserver:32400$pagesearcherkey?X-Plex-Token=$plexapitoken | grep -o MediaContainer) ]] 2>/dev/null
                                then
                                        p=1
                                        newlocation=$pagesearcherkey
                                        sed -i "s|$path|$newlocation|g" advancedplexapivar.sh
                                else
                                        echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | not a valid path" >> $logfolder/advancedplexapi.log 2>&1
                                fi
                        fi
                        done

                elif [[ ${option,,} = servers ]]
                then
                        newlocation=/servers
                        sed -i "s|$path|$newlocation|g" advancedplexapivar.sh

                elif [[ ${option,,} = refresh ]]
                then
                        read -rp "Choose $(curl -sSL "http://$ipplexserver:32400/library/sections?X-Plex-Token=$plexapitoken" | grep -o "title\=\".*\"\ agent" | grep -o "\".*\"" | grep -o "[a-zA-Z0-9].*[a-zA-Z0-9]" | paste -sd/): " refreshoption
                        if [[ $refreshoption == $(curl -sSL "http://$ipplexserver:32400/library/sections?X-Plex-Token=$plexapitoken" | grep -o "title\=\".*\"\ agent" | grep -o "\".*\"" | grep -o "$refreshoption") ]]
                        then
                                curl -sSL "http://$ipplexserver:32400/library/sections/$(curl -sSL "http://$ipplexserver:32400/library/sections?X-Plex-Token=$plexapitoken" | grep "$refreshoption" | grep -Eo key\=\""[0-9]{1,4}"\" | grep -Eo "[0-9]{1,4}")/refresh?X-Plex-Token=$plexapitoken"
                                newlocation="root"
                                sed -i "s|$path|$newlocation|g" advancedplexapivar.sh
                        fi

                elif [[ ${option,,} = search ]]
                then
                        read -rp "cancel | String to search for: " searchstring
                        if [[ ${searchstring,,} = cancel ]]
                        then
                                newlocation="root"
                                sed -i "s|$path|$newlocation|g" advancedplexapivar.sh
                        else
                                GET "http://$ipplexserver:32400/search?query=$searchstring&X-Plex-Token=$plexapitoken"
                                e=0
                                while [ $e = 0 ]
                                do
                                read -rp "Choose back | exit: " searchoption
                                if [[ ${searchoption,,} = "exit" ]]
                                then
                                        e=1
                                        echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | user quitted the script" >> $logfolder/advancedplexapi.log
                                        n=1

                                elif [[ ${searchoption,,} = back ]]
                                then
                                        e=1
                                        newlocation="root"
                                        sed -i "s|$path|$newlocation|g" advancedplexapivar.sh
                                else
                                        echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | that option is not possible here" >> $logfolder/advancedplexapi.log
                                fi
                                done
                        fi

                elif [[ ${option,,} = list ]]
                then
                        l=2
                        while [ $l -le 50 ]
                        do
                        echo "$(GET "http://$ipplexserver:32400/status/sessions/history/all?X-Plex-Token=$plexapitoken" | tail -n$l | head -n1 | grep -o grandparentTitle\=\".*\"\ type | grep -o \".*\" | grep -o [a-zA-Z0-9].*[a-zA-Z0-9])                | $(GET "http://$ipplexserver:32400/status/sessions/history/all?X-Plex-Token=$plexapitoken" | tail -n$l | head -n1 | grep -o title\=\".*\"\ grandparentTitle | grep -o \".*\")          | $(GET "http://$ipplexserver:32400/status/sessions/history/all?X-Plex-Token=$plexapitoken" | tail -n$l | head -n1 | grep -Eo "key\=\"/library/metadata/[0-9]{1,6}\"" | grep -Eo "/library/metadata/[0-9]{1,6}")"
                        l=$(( l+1 ))
                        done
                        l2=0
                        while [ $l2 = 0 ]
                        do
                        read -rp "Choose back | exit: " listoption
                        if [[ ${listoption,,} = "exit" ]]
                        then
                                echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | user quitted the script" >> $logfolder/advancedplexapi.log
                                l2=1
                                n=1
                        elif [[ ${listoption,,} = back ]]
                        then
                                l2=1
                                newlocation="root"
                                sed -i "s|$path|$newlocation|g" advancedplexapivar.sh
                        else
                                echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | that option is not possible here" >> $logfolder/advancedplexapi.lo
                        fi
                        done

                elif [[ ${option,,} = sessions ]]
                then
                        s=0
                        while [ $s = 0 ]
                        do
                        clear
                        GET http://$ipplexserver:32400/status/sessions?X-Plex-Token=$plexapitoken
                        if [[ $(GET "http://192.168.2.15:32400/status/sessions?X-Plex-Token=HfXBs23FhKBGj3msHjAz" | grep -Eo "key\=\"/library/metadata/[0-9]{1,6}\"") ]]
                        then
                                read -rp "Choose back | exit | mediainfo: " sesoption
                        else
                                read -rp "Choose back | exit: " sesoption
                        fi

                        if [[ ${sesoption,,} = "exit" ]]
                        then
                                echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | user quitted the script" >> $logfolder/advancedplexapi.log
                                s=1
                                n=1

                        elif [[ ${sesoption,,} = back ]]
                        then
                                s=1
                                newlocation="root"
                                sed -i "s|$path|$newlocation|g" advancedplexapivar.sh

                        elif [[ ${sesoption,,} = mediainfo ]]
                        then
                                s=1
                                newlocation=$(curl -sSL "http://192.168.2.15:32400/status/sessions?X-Plex-Token=HfXBs23FhKBGj3msHjAz" | grep -Eo "key\=\"/library/metadata/[0-9]{1,6}\"" | grep -o /.*[0-9])
                                sed -i "s|$path|$newlocation|g" advancedplexapivar.sh
                        else
                                echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | that option is not possible here" >> $logfolder/advancedplexapi.log
                        fi
                        done

                elif [[ ${option,,} = series ]]
                then
                        echo "--------------------------------"
                        curl -sSL "$location" | grep -o "title\=\".*\"\ contentRating" | grep -o "\".*\"" | grep -o "[a-zA-Z0-9].*[a-zA-Z0-9]" | sort | sed 's|amp;||g' | sed "s|\'|\'|g" | sed "s|\&\#233\;|é|"
                        echo "--------------------------------"
                        z=0
                        while [ $z = 0 ]
                        do
                        read -rp "cancel | Choose one of these series: " Series
                        echo "$(date '+%d/%m/%Y %H:%M:%S') |    series     | $Series" >> $logfolder/advancedplexapi.log 2>&1
                        if [[ ${Series,,} = cancel ]]
                        then
                                z=1
                                echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | user canceled series selection" >> $logfolder/advancedplexapi.log 2>&1

                        elif [[ $Series =~ $re ]]
                        then
                                if [[ -z $(curl -sSL $location | grep -Eo "title\=\"$(echo $Series | sed 's|&|&|g' | sed "s|'|\'|g" | sed "s|é|\&\#233\;|")\"" | head -n1) ]]
                                then
                                        echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | invalid series title [numbers]" >> $logfolder/advancedplexapi.log 2>&1
                                else
                                        z=1
                                        Series=$(echo $Series | sed 's|&|&|g' | sed "s|'|\'|g" | sed "s|é|\&\#233\;|")
                                        newlocation=$(curl -sSL $location | grep "title\=\"$Series\"" | grep -o "key\=\"/library/metadata/.*/children\"" | grep -Eo "/library.*/children")
                                        sed -i "s|$path|$newlocation|g" advancedplexapivar.sh
                                fi

                        else
                                if [[ -z $(curl -sSL $location | grep -Eo "title\=\"$(echo $Series | sed 's|&|&|g' | sed "s|'|\'|g" | sed "s|é|\&\#233\;|")\"" | head -n1) ]]
                                then
                                        echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | invalid series title [letters]" >> $logfolder/advancedplexapi.log 2>&1
                                else
                                        z=1
                                        Series=$(echo $Series | sed 's|&|&|g' | sed "s|'|\'|g" | sed "s|é|\&\#233\;|")
                                        newlocation=$(curl -sSL $location | grep "title\=\"$Series\"" | grep -o "key\=\"/library/metadata/.*/children\"" | grep -o "/library.*/children")
                                        sed -i "s|$path|$newlocation|g" advancedplexapivar.sh
                                fi
                        fi
                        done

                elif [[ $option == $(curl -sSL $location | grep -o "title\=\".*\"\ agent" | grep -o "\".*\"" | grep -o "$option") ]]
                then
                        newlocation=/library/sections/$(curl -sSL $location | grep "$option" | grep -Eo key\=\""[0-9]{1,4}"\" | grep -Eo "[0-9]{1,4}")/all
                        sed -i "s|$path|$newlocation|g" advancedplexapivar.sh

                elif [[ ${option,,} = movies ]]
                then
                        echo "--------------------------------"
                        printf "$(curl -sSL $location | grep -o "title\=\".*\"\ contentRating" | grep -o "\".*\"" | grep -v titleSort && curl -sSL $location | grep -o "title\=\".*\"\ contentRating" | grep -o "\".*\"" | grep titleSort | grep -o "\".*\"\ titleSort" | grep -o "\".*\"")" | grep -o "[a-zA-Z0-9].*[a-zA-Z0-9]" | sort | sed 's|amp;||g' | sed "s|\'|\'|g" | sed "s|\&\#233\;|é|"
                        echo "--------------------------------"
                        y=0
                        while [ $y = 0 ]
                        do
                        read -rp "cancel | choose on of these movies: " Movies
                        echo "$(date '+%d/%m/%Y %H:%M:%S') |     movie     | $Movies" >> $logfolder/advancedplexapi.log 2>&1
                        if [[ ${Movies,,} = cancel ]]
                        then
                                y=1
                                echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | user canceled movie selection" >> $logfolder/advancedplexapi.log 2>&1

                        elif [[ $Movies =~ $re ]]
                        then
                                if [[ -z $(curl -sSL $location | grep -Eo "title\=\"$(echo $Movies | sed 's|&|&|g' | sed "s|'|\'|g" | sed "s|é|\&\#233\;|")\"" | head -n1) ]]
                                then
                                        echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | invalid movie title [numbers]" >> $logfolder/advancedplexapi.log 2>&1
                                else
                                        y=1
                                        Movies=$(echo $Movies | sed 's|&|&|g' | sed "s|'|\'|g" | sed "s|é|\&\#233\;|")
                                        newlocation=$(curl -sSL $location | grep "title\=\"$Movies\"" | grep -Eo "key\=\"/library/metadata/[0-9]{1,5}\"" | grep -Eo "/library/metadata/[0-9]{1,5}")
                                        sed -i "s|$path|$newlocation|g" advancedplexapivar.sh
                                fi

                        else
                                if [[ -z $(curl -sSL $location | grep -Eo "title\=\"$(echo $Movies | sed 's|&|&|g' | sed "s|'|\'|g" | sed "s|é|\&\#233\;|")\"" | head -n1) ]]
                                then
                                        echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | invalid movie title [letters]" >> $logfolder/advancedplexapi.log 2>&1
                                else
                                        y=1
                                        Movies=$(echo $Movies | sed 's|&|&|g' | sed "s|'|\'|g" | sed "s|é|\&\#233\;|")
                                        newlocation=$(curl -sSL $location | grep "title\=\"$Movies\"" | grep -Eo "key\=\"/library/metadata/[0-9]{1,5}\"" | grep -Eo "/library/metadata/[0-9]{1,5}")
                                        sed -i "s|$path|$newlocation|g" advancedplexapivar.sh
                                fi
                        fi
                        done

                elif [[ ${option,,} = subtitle ]]
                then
                        suboldlocation=$path
                        newlocation=$(curl -sSL "$location" | grep -Eo "key\=\"/library/streams/[0-9]{1,9}\"" | grep -Eo "/library/streams/[0-9]{1,9}")
                        sed -i "s|$path|$newlocation|g" advancedplexapivar.sh

                elif [[ ${option,,} = back ]]
                then
                        if [[ "$path" = /library/sections ]]
                        then
                                newlocation="root"
                                sed -i "s|$path|$newlocation|g" advancedplexapivar.sh

                        elif [[ "$path" = "/:/prefs" ]]
                        then
                                newlocation="root"
                                sed -i "s|$path|$newlocation|g" advancedplexapivar.sh

                        elif [[ "$path" = /system/agents ]]
                        then
                                newlocation="root"
                                sed -i "s|$path|$newlocation|g" advancedplexapivar.sh

                        elif [[ "$path" = /system ]]
                        then
                                newlocation="root"
                                sed -i "s|$path|$newlocation|g" advancedplexapivar.sh

                        elif [[ "$path" = /myplex/account ]]
                        then
                                newlocation="root"
                                sed -i "s|$path|$newlocation|g" advancedplexapivar.sh

                        elif [[ "$path" = /library/onDeck ]]
                        then
                                newlocation="root"
                                sed -i "s|$path|$newlocation|g" advancedplexapivar.sh

                        elif [[ "$path" = / ]]
                        then
                                newlocation="root"
                                sed -i "s|$path|$newlocation|g" advancedplexapivar.sh

                        elif [[ "$path" = /status/sessions ]]
                        then
                                newlocation="root"
                                sed -i "s|$path|$newlocation|g" advancedplexapivar.sh

                        elif [[ "$path" = /status/sessions/history/all ]]
                        then
                                newlocation="root"
                                sed -i "s|$path|$newlocation|g" advancedplexapivar.sh

                        elif [[ "$path" = /servers ]]
                        then
                                newlocation="root"
                                sed -i "s|$path|$newlocation|g" advancedplexapivar.sh
                        else
                                case $(curl -sSL $location | grep -o "type\=\".*\"\ title\=" | grep -o "\".*\"" | grep -o "[a-zA-Z].*[a-zA-Z]" | head -n1) in
                                "show" )
                                        #from all shows folder to all media folder
                                        newlocation=/library/sections
                                        sed -i "s|$path|$newlocation|g" advancedplexapivar.sh
                                        ;;
                                "season" )
                                        #from series (list of seasons) folder to all shows folder
                                        newlocation=/library/sections/$(curl -sSL $location | grep -Eo librarySectionID\=\""[0-9]{1,6}"\" | grep -Eo "[0-9]{1,6}" | head -n1)/all
                                        sed -i "s|$path|$newlocation|g" advancedplexapivar.sh
                                        ;;
                                "episode" )
                                        #from season (list of episodes in season) folder to series (list of seasons) folder
                                        newlocation=/library/metadata/$(curl -sSL $location | grep -Eo parentRatingKey\=\""[0-9]{1,6}"\" | grep -Eo "[0-9]{1,6}" | head -n1)/children
                                        sed -i "s|$path|$newlocation|g" advancedplexapivar.sh
                                        ;;
                                "movie" )
                                        if ! curl -sSL $location | grep -q streamType
                                        then
                                                #from all movies folder to all media folder
                                                newlocation=/library/sections
                                                sed -i "s|$path|$newlocation|g" advancedplexapivar.sh
                                        else
                                                #from movie information to all movies folder
                                                newlocation=/library/sections/$(curl -sSL $location | grep -Eo librarySectionID\=\""[0-9]{1,6}"\" | grep -Eo "[0-9]{1,6}" | head -n1)/all
                                                sed -i "s|$path|$newlocation|g" advancedplexapivar.sh
                                        fi
                                        ;;
                                * )
                                        if [[ $(curl -sSL $location | grep -o '\-->' | head -n1) = "-->" ]]
                                        then
                                                newlocation=$suboldlocation
                                                sed -i "s|$path|$newlocation|g" advancedplexapivar.sh
                                        else
                                                echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | you can't go back here" >> $logfolder/advancedplexapi.log
                                        fi
                                        ;;
                                esac
                        fi

                elif [[ ${option,,} = exit ]]
                then
                        echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | user quitted the script" >> $logfolder/advancedplexapi.log 2>&1
                        n=1
                else
                        echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | user chose invalid option" >> $logfolder/advancedplexapi.log 2>&1
                fi
done

#commenting the sed command below will have the following effect:
#commented/inactive= When you exit the script and run it again, you begin where you left the previous time.
#un-commented/working as a command= Everytime you run the script, you will begin at the start.
#you can replace /library/sections with something else. What you replace it with will be the place you start at when running the script. The starting point doesn't effect your options/actions in any way. Do "root" to start at root.
sed -i "s|$path|/library/sections|g" advancedplexapivar.sh 2>/dev/null

#commenting the clear command below will have the following effect:
#commented/inactive= after you exit the script, you can scroll back up to see the last output of the script before you left.
#un-commented/working as a command= when you exit the script, it clear's the terminal so you start with a empty screen.
clear

Advancedplexapivar.sh

/library/sections

yes, the script is very big. What did you expect.

Before running the script:
There are some options at the bottom and top of the script that you should check out and change if you want to.

If you’re reading this and you’re not convinced:
Just copy the code and try it out. Just believe me. Try it out. It’s really great. If you tried it and you’re not convinced after, then I’m fine with it. But don’t you comment “This is useless” without trying it first.

Last point:
If you find ANY bugs or things that should work differently, please tell me. I really want to perfect the code.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.