Jump to content


Photo

Find 'IsWatched' episodes in a specific directory?


  • Please log in to reply
47 replies to this topic

#21 muzicman0 OFFLINE  

muzicman0

    Advanced Member

  • Members
  • 134 posts
  • Local time: 01:00 AM

Posted 09 September 2019 - 05:40 PM

Got it!  Took me a bit to figure out my actual user ID.  I assumed it was my actual username.  I got it from /Users/Public, and not it works in a browser...not to figure out how to parse it and save all the paths to a text file or an array.



#22 PenkethBoy ONLINE  

PenkethBoy

    Advanced Member

  • Members
  • 3925 posts
  • Local time: 09:00 AM
  • LocationWarrington,UK

Posted 09 September 2019 - 06:06 PM

To get you going here is a simple script - This Authenticates against your server using a Users login details - no need to set an API key in the dashboard as it gets one from Emby for the user - user access rights are maintained so use an Admin user to sart with then you can test restricted users when you get more confident

 

Change the first three lines with your user details

 

then migrate in the powershell window to the location of where you saved the script e.g. c:\test

 

and run it like this

 

PS C:\Test> .\TVEpisodes-Test.ps1

 

assuming you save the file in the c:\test folder - the PS C:\Test> is the powershell prompt - and yes the . is important before the filename

 

It will save the JSON returned by Emby to the same location as the script as a TVEpisodes.json file

 

Attached File  TVEpisodes-Test.zip   1.1KB   6 downloads - unzip it first! :)

 

NB: you will probably need to change your Powershell Execution Policy - see google on how to do this

 

Good Luck


Edited by PenkethBoy, 09 September 2019 - 06:07 PM.

  • muzicman0 likes this

#23 muzicman0 OFFLINE  

muzicman0

    Advanced Member

  • Members
  • 134 posts
  • Local time: 01:00 AM

Posted 09 September 2019 - 06:25 PM

Thanks a ton.  I had this going:

$request = 'http://10.1.0.122:8096/emby/users/{user ID]/items?Fields=Path&Recursive=true&IncludeItemTypes=Episode&IsPlayed=True&api_key=xxxxxxxxxxxxxxxx'
Invoke-WebRequest $request |
ConvertFrom-Json |
Select -expand Items|
Select SeriesName, SeasonName, Name, Path

It did return the info I was after, but I hadn't figured out how to get the path into a variable, etc yet.  I will look over your code (which is much more elegant), and see what I can do. 


Edited by muzicman0, 09 September 2019 - 06:25 PM.


#24 PenkethBoy ONLINE  

PenkethBoy

    Advanced Member

  • Members
  • 3925 posts
  • Local time: 09:00 AM
  • LocationWarrington,UK

Posted 10 September 2019 - 07:29 AM

Tip - dont use Aliases in Powershell use the full command (there is a special place in Hell for the people that do) - if you dont it will come back and bite you in the bum - i.e. select is and alias for Select-Object

 

if you assign the webrequest to a variable as i do in the code i gave you its a lot easier to manipulate 

 

e.g. $Result = Invoke-Webrequest ..... so result contains the info returned by Emby - the json is then contained in the $Result.Content - this is what you convert from Json into a PS object so you can then manipulate it how you need

 

Also use the parameters for Invoke-Webrequest -Uri -Method etc etc - buy default it worked for you as the default method is GET

 

Also it better to keep pipeline stuff on one line to make it clearer and not have it over multiline as it makes it hard to read

 

e.g. $Media | ConvertTo-Json -Verbose -Depth 100 | Out-File "$PSScriptRoot\TVEpisode.json"

 

Final tip - Download "Visual Studio Code" (it free) add the Powershell plugin and you will get a much better editor/debugger/IDE the the verrrrrrrrrrry basic Powershel ISE

 

Have fun



#25 PenkethBoy ONLINE  

PenkethBoy

    Advanced Member

  • Members
  • 3925 posts
  • Local time: 09:00 AM
  • LocationWarrington,UK

Posted 10 September 2019 - 07:44 AM

To Access the data in powershell contained in the $Media variable in my simple example and get you started with a very basic writing to screen of variable info

 

add the below to my script after line 47 before the Exit line and run the script and look at the screen you will see all the episode paths and ID's list

 

 

#hint its ".items" which is how the individual episode are defined in the json file

foreach ($Episode in $Media.Items) 
 
{
 
   $Path = $Episode.Path
 
   $Id = $Episode.ID
 
   Write-Host "Episode ID:" $Id "Path to File:" $Path
 
}
 
$Episode can be anything you like $Banana $Purple etc - usually best to make it descriptive so its easy to understand - especially 9 months later when you come back it and WHAT was i thinking :)

  • muzicman0 likes this

#26 muzicman0 OFFLINE  

muzicman0

    Advanced Member

  • Members
  • 134 posts
  • Local time: 01:00 AM

Posted 10 September 2019 - 10:01 AM

Thanks!  This is great, I should be able to get it from here!  I really appreciate all the help and examples!



#27 muzicman0 OFFLINE  

muzicman0

    Advanced Member

  • Members
  • 134 posts
  • Local time: 01:00 AM

Posted 08 February 2020 - 01:19 PM

I have everything working pretty well now, and am able to do everything I want.  So, thanks for all the help over the last year!  

 

One more quick question, my query right now is basically:

/items?Fields=Path&Recursive=true&IncludeItemTypes=Episode&IsPlayed=True

BUT, is there a way to filter by either Library name, or path?

 

Currently, it pulls ALL watched episodes, writes it to a CSV file, then another script will read that CSV file and process.  If I could filter on ONLY items in my 'Recorded TV' library, or only items that have "\\10.1.0.121\" in the path, then it would be more efficient.  I currently pull over 4000 episodes, then my second script has to filter to the above criteria.  Not a huge deal, but would be nice to get it down a bit.

 

Any way to do that?  I tried looking at the API docs online, but couldn't find what I was looking for.

 

Once I have this done, I plan on writing a tutorial for anyone who wants to do the same.  I am using Kodi as a front end, auto compressing watched episodes to h.265, AND have EDL support, so that means non destructive commercial skipping.  



#28 Luke OFFLINE  

Luke

    System Architect

  • Administrators
  • 152846 posts
  • Local time: 04:00 AM

Posted 08 February 2020 - 01:24 PM

Yes using ParentId 



#29 muzicman0 OFFLINE  

muzicman0

    Advanced Member

  • Members
  • 134 posts
  • Local time: 01:00 AM

Posted 08 February 2020 - 01:46 PM

Thanks, that seemed to mostly work, but I did get the following back (I am using a '^' as the delimiter in the CSV since apostrophes can screw up my code if there are apostrophes in the actual content IE: Marvel's):

 

"Suit Yourself"^"340d61099851442d9615694cc77f1e26"^"787249"^^^"9"^"18"^"False"^"Episode"^"@{PlaybackPositionTicks=0; PlayCount=6; IsFavorite=False; LastPlayedDate=2020-02-07T23:47:28.0000000+00:00; Played=True}"^"Project Runway"^"764830"^"764915"^"50f766bdfd403abd051b63bf38017706"^"Season 18"^""^"System.Object[]"^"Video"

and I am not sure why.  A normal watched episode looks like:

"Sanditon on Masterpiece S01E05"^"340d61099851442d9615694cc77f1e26"^"764932"^"\\10.1.0.121\rtv\Sanditon on Masterpiece\Season 1\Sanditon on Masterpiece S01E05.ts"^"37136994550"^"5"^"1"^"False"^"Episode"^"@{PlaybackPositionTicks=0; PlayCount=1; IsFavorite=False; LastPlayedDate=2020-02-06T06:35:36.0000000+00:00; Played=True}"^"Sanditon on Masterpiece"^"764832"^"764930"^"e28ec758f494bdd070f6713a7a9821f4"^"Season 1"^"@{Primary=bf84fea1c0de41cea1f0d466bfeb9442}"^"System.Object[]"^"Video"

I have no current Project Runway episodes on the server, although I did within the last few days, but they have all been deleted.  I'm sure I can get rid of the erroneous data by deleting the show completely, but I would like to know why it happened so I can avoid it in the future!

 

 



#30 Luke OFFLINE  

Luke

    System Architect

  • Administrators
  • 152846 posts
  • Local time: 04:00 AM

Posted 08 February 2020 - 01:51 PM

What is the problem?



#31 muzicman0 OFFLINE  

muzicman0

    Advanced Member

  • Members
  • 134 posts
  • Local time: 01:00 AM

Posted 08 February 2020 - 02:25 PM

The"Suit Yourself"is not an actual episode.  Or at least, it isn't a current episode.  There is no path listed, etc.  It doesn't exist on the Server.


Edited by muzicman0, 08 February 2020 - 02:26 PM.


#32 PenkethBoy ONLINE  

PenkethBoy

    Advanced Member

  • Members
  • 3925 posts
  • Local time: 09:00 AM
  • LocationWarrington,UK

Posted 08 February 2020 - 02:40 PM

Emby keeps the watched status of deleted episodes - IIRC - so if you add back the watched status is maintained or something

 

Think this is why you are seeing the deleted episode returned i believe

 

Have you tried adding "&IsMissing=False"



#33 muzicman0 OFFLINE  

muzicman0

    Advanced Member

  • Members
  • 134 posts
  • Local time: 01:00 AM

Posted 08 February 2020 - 03:21 PM

No, but that seems to be a spectacular idea!  I will do that now.



#34 muzicman0 OFFLINE  

muzicman0

    Advanced Member

  • Members
  • 134 posts
  • Local time: 01:00 AM

Posted 08 February 2020 - 03:26 PM

Seems to work well!  Thanks again to all those who helped.  My final PowerShell Script is:

 

$embyServerUrl = "http://127.0.0.1:8096"
$BasePath = "D:\OneDrive\ExportTools.bundle\WatchedTVEpisodes.csv"
$Emby_User_Name = "xxxxxxxxxxx"
$Emby_User_Pwd = 'xxxxxxx' # if you dont have a password leave as ''


Function Get-StringHash([String] $String,$HashName = "MD5") 
{ 
    $StringBuilder = New-Object System.Text.StringBuilder 
    [System.Security.Cryptography.HashAlgorithm]::Create($HashName).ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String))|ForEach-Object{ 
        [Void]$StringBuilder.Append($_.ToString("x2")) 
    } 
    $StringBuilder.ToString() 
}

$embyClientName = "PS-Testing"
$embyDeviceName = "PowerShell"
$embyDeviceId = "3"
$embyApplicationVersion = " - 0.0.1 Alpha"


Function Get-EmbyAccessToken ($Username, $Pwd)
{
    $authUrl = "{0}/Users/AuthenticateByName?format=json" -f $embyServerUrl
    $sha1Pass = Get-StringHash -String $Pwd -HashName "SHA1"
    $md5Pass = Get-StringHash -String $Pwd -HashName "MD5"
    $postParams = (@{Username="$username";pw=$Pwd;password="$sha1Pass";passwordMd5="$md5Pass"} | ConvertTo-Json)
    $headers = @{"Authorization"="Emby Client=`"$embyClientName`", Device=`"$embyDeviceName`", DeviceId=`"$embyDeviceId`", Version=`"$embyApplicationVersion`""}

    Write-Verbose ("authUrl={0},Username={1},sha1Pass={2},md5Pass={3},params={4}" -f $authUrl, $Username,$sha1Pass,$md5Pass,$postParams)
    return (Invoke-WebRequest -Uri $authUrl -Method POST -Body $postParams -ContentType "application/json" -Headers $headers)
} 

$authResult = Get-EmbyAccessToken -Username $Emby_User_Name -Pwd $Emby_User_Pwd
$user = $authResult.Content | ConvertFrom-Json

$MediaUrl = $embyServerUrl + "/emby/users/" + $User.User.Id + "/items?Fields=Path&Recursive=true&IncludeItemTypes=Episode&IsPlayed=True&IsMissing=False&ParentId=6f25559632c2aca7578b4fdc30acb587" + "&api_key=" + $User.AccessToken

Invoke-WebRequest -method GET -uri $MediaUrl | ConvertFrom-Json | Select-Object -ExpandProperty  Items | ConvertTo-Csv -NoTypeInformation -Delimiter '^' | Set-Content $BasePath

Exit

Edited by muzicman0, 08 February 2020 - 03:27 PM.


#35 muzicman0 OFFLINE  

muzicman0

    Advanced Member

  • Members
  • 134 posts
  • Local time: 01:00 AM

Posted 22 February 2020 - 11:26 AM

Things keep changing in the output.  Why is that?  Server version has not changed.  Last night, I stopped getting episode info.  below is the info the query is returning:

 

"Name"^"ServerId"^"Id"^"Path"^"RunTimeTicks"^"IsFolder"^"Type"^"UserData"^"SeriesName"^"SeriesId"^"SeriesPrimaryImageTag"^"SeasonName"^"ImageTags"^"BackdropImageTags"^"MediaType"

 

It used to return a lot more info, not least of which was the episode info.  It was labeled as "IndexNumber".  Why would this change?



#36 Luke OFFLINE  

Luke

    System Architect

  • Administrators
  • 152846 posts
  • Local time: 04:00 AM

Posted 22 February 2020 - 02:53 PM

Example?

#37 muzicman0 OFFLINE  

muzicman0

    Advanced Member

  • Members
  • 134 posts
  • Local time: 01:00 AM

Posted 22 February 2020 - 03:11 PM

Here is an example that was returned.  I am using '^' as a separator.  I put some space between each row.  I don't think I have an older example, but as of at least  days ago it would return a lot more info, including IndexNumber, ParentIndex, etc. Again, no updates have been done to the Emby server.

 

"Name"^"ServerId"^"Id"^"Path"^"RunTimeTicks"^"IsFolder"^"Type"^"UserData"^"SeriesName"^"SeriesId"^"SeriesPrimaryImageTag"^"SeasonName"^"ImageTags"^"BackdropImageTags"^"MediaType"
 
 
"Our Little Secret: Everybody Wants a Porch"^"340d61099851442d9615694cc77f1e26"^"831511"^"\\10.1.0.130\Local Cloud\OneDrive\Recorded TV\Home Town (2017)\Home Town 2020_02_17_20_00_00 - Our Little Secret  Everybody Wants a Porch.ts"^"37067291220"^"False"^"Episode"^"@{PlaybackPositionTicks=0; PlayCount=2; IsFavorite=False; LastPlayedDate=2020-02-20T22:04:50.0000000+00:00; Played=True}"^"Home Town"^"827373"^"e200366702ef5c8f5688af7f1bb6cc20"^"Season Unknown"^"@{Primary=d38e03a8a9772171723437e43154a3e7}"^"System.Object[]"^"Video"
 
 
"Restoring a Craftsman"^"340d61099851442d9615694cc77f1e26"^"831542"^"\\10.1.0.130\Local Cloud\OneDrive\Recorded TV\Home Town (2017)\Season 4\Home Town S04E07 Restoring a Craftsman.ts"^"37085414010"^"False"^"Episode"^"@{PlaybackPositionTicks=0; PlayCount=1; IsFavorite=False; LastPlayedDate=2020-02-22T17:45:23.0000000+00:00; Played=True}"^"Home Town"^"827373"^"e200366702ef5c8f5688af7f1bb6cc20"^"Season 4"^"@{Primary=859e5bf41663f794619c2c6ec433c72c}"^"System.Object[]"^"Video"


#38 PenkethBoy ONLINE  

PenkethBoy

    Advanced Member

  • Members
  • 3925 posts
  • Local time: 09:00 AM
  • LocationWarrington,UK

Posted 22 February 2020 - 04:05 PM

just tried you code from post #34

 

what i see is that mostly it works

 

on some lines excel splits the data into two cells on initial import - before you get to use something like "text-to-columns"

 

so column A has

 

Opera, Arts and Donuts^"5fc81c140c6946c7b79f4f95f0594dc5"^"202868"^"E:\ServerFolders\Multimedia\Recorded TV\The Grand Tour (2016)\Season 1\The Grand Tour (2016) - S01E03 - Opera

 

and column B has

 

 Arts and Donuts.mkv"^"37489319936"^"3"^"1"^"False"^"Episode"^"10198"^"16022"^"System.Object[]"^"@{PlaybackPositionTicks=0; PlayCount=1; IsFavorite=False; LastPlayedDate=2018-04-09T17:31:42.0000000+00:00; Played=True}"^"The Grand Tour (2016)"^"10198"^"16022"^"fac20bc8ff140f091545b07eefb05123"^"Season 1"^"@{Primary=596c963496c1f7d02321c8d140bdadc3}"^"System.Object[]"^"0c850e4f59633cb611d96fec7ffac304"^"10198"^"d59ade7efa0fe0e717672a2a785109cd"^"Video"

 

It appears from a quick look that the initial import to excel is seeing the "," in the file path and splitting on that

 

This is consistent across multiple episode with a comma in the name

Although odd it does not do it for the name field

 

 

So in summary - i think its either the converto-csv or excel thats chopping off data rather than emby not providing it

 

As simple test is to save the output as json and see if the data is then missing or not - my guess is it will be all there

 

I might be wrong with the above - but have little info to go on. :)

 

Good Luck



#39 muzicman0 OFFLINE  

muzicman0

    Advanced Member

  • Members
  • 134 posts
  • Local time: 01:00 AM

Posted 22 February 2020 - 06:48 PM

just tried you code from post #34

 

what i see is that mostly it works

 

on some lines excel splits the data into two cells on initial import - before you get to use something like "text-to-columns"

 

so column A has

 

Opera, Arts and Donuts^"5fc81c140c6946c7b79f4f95f0594dc5"^"202868"^"E:\ServerFolders\Multimedia\Recorded TV\The Grand Tour (2016)\Season 1\The Grand Tour (2016) - S01E03 - Opera

 

and column B has

 

 Arts and Donuts.mkv"^"37489319936"^"3"^"1"^"False"^"Episode"^"10198"^"16022"^"System.Object[]"^"@{PlaybackPositionTicks=0; PlayCount=1; IsFavorite=False; LastPlayedDate=2018-04-09T17:31:42.0000000+00:00; Played=True}"^"The Grand Tour (2016)"^"10198"^"16022"^"fac20bc8ff140f091545b07eefb05123"^"Season 1"^"@{Primary=596c963496c1f7d02321c8d140bdadc3}"^"System.Object[]"^"0c850e4f59633cb611d96fec7ffac304"^"10198"^"d59ade7efa0fe0e717672a2a785109cd"^"Video"

 

It appears from a quick look that the initial import to excel is seeing the "," in the file path and splitting on that

 

This is consistent across multiple episode with a comma in the name

Although odd it does not do it for the name field

 

 

So in summary - i think its either the converto-csv or excel thats chopping off data rather than emby not providing it

 

As simple test is to save the output as json and see if the data is then missing or not - my guess is it will be all there

 

I might be wrong with the above - but have little info to go on. :)

 

Good Luck

Perhaps, I will test some more, but that is the exact reason that I chose to use caret as a separator in the file.  That is the line:

 

ConvertTo-Csv -NoTypeInformation -Delimiter '^' 

 

in the code.  

 

Either way, I will test it out since you may be right, but if you are, it must be some bug in powershell.



#40 PenkethBoy ONLINE  

PenkethBoy

    Advanced Member

  • Members
  • 3925 posts
  • Local time: 09:00 AM
  • LocationWarrington,UK

Posted 22 February 2020 - 07:01 PM

i think its more likely - excel

 

i was using excel 2007 - if that matters






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users