Jump to content

Linux Bash script to get a list of watched episodes


muzicman0

Recommended Posts

muzicman0

I debated on where to post this, and decided to start a new thread, but the below code is based on this post from @robii@PenkethBoy also helped me find a problem with authentication.

This section of code will work on it's own, but is part of a much larger script that acts on this info, but since I had a hard time finding any Bash examples, I wanted to post this in hopes that it may help someone else.

I am NOT a programmer, and so I am sure there are better ways to do what I am doing, but so far, it seems to work.  If anyone sees something that is just plain stupid, please let me know.  

The following script will use the Emby API to get a API token for a specific user, and then query the API to get a list of current watched episodes.  It will return the Series, Season, Episode #, Episode Name, and path to the file.  

You will need to replace your Username (line 35), User ID (line 34), Password (line 36), and ParentID (line 44 in the MediaUrl variable) - just read the comments in the code.  Also, the BasePath Variable will need to be changed to a valid path on your PC.

oh, and jq needs to be installed (sudo apt install jq for Ubuntu).  This is working on Ubuntu 20.04, and I hope it helps someone!

EDIT: Oh, and I should also mention that I store the JSON in a variable, which from my research should be fine, but if you expect a HUGE response from the server, you may want to write it to a file.

#!/bin/bash 
 
function LineCount {
	#first argument is path to file
	wc -l < "$1"
	}
function WriteLog {
	#First argument is line to write
	NowLongLog="$(date '+%F_%H:%M:%S')"
	echo "${NowLongLog}: $1" >> "${BasePath}/AutoCompressLog.log"
	echo $1
	}
function get_api_key {
	api_key="$(curl -s -H "Authorization: MediaBrowser Client=$client, Device=$device, DeviceId=$deviceid, Version=1.0.0.0, UserId=$userid" -d "username=$username" -d "pw=$password" 'http://127.0.0.1:8096/users/authenticatebyname?format=json'| python -m json.tool | grep 'AccessToken' | sed 's/\"//g; s/AccessToken://g; s/\,//g; s/ //g')"
		if [[ $api_key == "" ]]; then
			echo `date +"%d/%m/%Y-%H:%M:%S"`": could not get API key, check credentials $userid $client $username $password"
			exit
		fi
	}

BasePath="/home/muzicman0/Scripts" #The folder where this script and support files are stored

# credentials
client="Bash-Find Watched Episodes"	#Whatever you want here
device="Bash"	#Whatever you want here
deviceid="MrLunt"	#Whatever you want here (I use the hostname of my PC)
userid="xxxxx"	#this is a long string of characters that I got from the webapp.  Replace with your user account ID
username="xxxxx"	#Self Explanatory
password="xxxxx"	#Self Explanatory

embyServerUrl="http://127.0.0.1:8096"	#I assume this could be run remote if needed, but I am running on the same machine as the Emby server

get_api_key
WriteLog "NEW JSON - API KEY: $api_key"

#CHANGE THE PARENT ID BELOW TO MATCH YOUR LIBRARY!!!!!
MediaUrl="${embyServerUrl}/emby/users/${userid}/items?Fields=width,height,Path&Recursive=true&IncludeItemTypes=Episode&IsPlayed=True&IsMissing=False&ParentId=dfbd84266bb0ba7ec7c4222ea7441f47&SortBy=IndexNumber&SortOrder=Descending&api_key=${api_key}"

WriteLog "NEW JSON - URL: $MediaUrl"

outJSON=$(curl $MediaUrl)

WriteLog "NEW JSON - JSON String: $outJSON"

#it was easier for me to just create my own csv file, because I wanted to use '^' as the delimiter, not ','.
jq -s -r '.[].Items[] | .SeriesName + "^" + (.ParentIndexNumber|tostring) + "^" + (.IndexNumber|tostring) + "^" + .Path + "^" + .Name'  <<< $outJSON > "${BasePath}/FileList.csv"	

FilesToProcess=$(LineCount "${BasePath}/FileList.csv")
  	if [ $FilesToProcess -lt 1 ]; then
  		WriteLog "NEW JSON - NO FILES TO PROCESS, SHOULD EXIT HERE"
  	else
  		WriteLog "NEW JSON - # of Files to process: $FilesToProcess"
  	fi

while IFS= read -r line; do # read input line by line
  # Split line into fields and read into array.
  IFS=^ read -ra fields <<<"$line"
  # Loop over fields from the back, down to the 2nd.
  	for (( i = ${#fields[@]} - 1; i >= 0; --i )); do
  		temp=$(echo ${fields[i]} | tr --delete '"')
  			if [ $i = 0 ]; then
	  			WriteLog "SeriesName: $temp"
	  		fi
	  		if [ $i = 1 ]; then
	  			WriteLog "Season: $temp"
	  		fi
	  		if [ $i = 2 ]; then
	  			WriteLog "Episode: $temp"
	  		fi
	  		if [ $i = 3 ]; then
	  			WriteLog "Path: $temp"
	  		fi
	  		if [ $i = 4 ]; then
	  			WriteLog "Episode Name: $temp"
	  		fi
	  		  	

  	done
  	
done < "${BasePath}/FileList.csv"  #end of test loop

 

Edited by muzicman0
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...