Jump to content

Find 'IsWatched' episodes in a specific directory?


muzicman0

Recommended Posts

  • 4 months later...
muzicman0

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.  

Link to comment
Share on other sites

muzicman0

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!

 

 

Link to comment
Share on other sites

muzicman0

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
Link to comment
Share on other sites

PenkethBoy

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"

Link to comment
Share on other sites

muzicman0

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
Link to comment
Share on other sites

  • 2 weeks later...
muzicman0

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?

Link to comment
Share on other sites

muzicman0

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"
Link to comment
Share on other sites

PenkethBoy

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

Link to comment
Share on other sites

muzicman0

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.

Link to comment
Share on other sites

muzicman0

I went back to your original code just to avoid any changes I might have made.  The only thing I updated was the URL to:

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

The results were interesting.  The first episode returned didn't have the extra info, but that is somewhat to be expected since it didn't have season/episode info in Emby either (it was a special).  The next 2 episodes had all the info I was looking for (they did have season/episode info in Emby).

 

So, I guess what is happening is that the convert from json is only looking at the first 'item' and creating columns based on that.  I have no idea how to get around that limitation, assuming I am even corrcect.  The results are posted below:

 

{
    "Items":  [
                  {
                      "Name":  "Our Little Secret: Everybody Wants a Porch",
                      "ServerId":  "340d61099851442d9615694cc77f1e26",
                      "Id":  "831511",
                      "Path":  "\\\\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",
                      "RunTimeTicks":  37067291220,
                      "IsFolder":  false,
                      "Type":  "Episode",
                      "UserData":  {
                                       "PlaybackPositionTicks":  0,
                                       "PlayCount":  2,
                                       "IsFavorite":  false,
                                       "LastPlayedDate":  "2020-02-20T22:04:50.0000000+00:00",
                                       "Played":  true
                                   },
                      "SeriesName":  "Home Town",
                      "SeriesId":  "827373",
                      "SeriesPrimaryImageTag":  "e200366702ef5c8f5688af7f1bb6cc20",
                      "SeasonName":  "Season Unknown",
                      "ImageTags":  {
                                        "Primary":  "d38e03a8a9772171723437e43154a3e7"
                                    },
                      "BackdropImageTags":  [


                                            ],
                      "MediaType":  "Video"
                  },
                  {
                      "Name":  "Kid + Plane + Cable + Truck",
                      "ServerId":  "340d61099851442d9615694cc77f1e26",
                      "Id":  "860404",
                      "Path":  "\\\\10.1.0.130\\Local Cloud\\OneDrive\\Recorded TV\\MacGyver (2016)\\Season 4\\MacGyver S04E03 Kid + Plane + Cable + Truck.ts",
                      "RunTimeTicks":  37157498440,
                      "IndexNumber":  3,
                      "ParentIndexNumber":  4,
                      "IsFolder":  false,
                      "Type":  "Episode",
                      "ParentLogoItemId":  "803815",
                      "ParentBackdropItemId":  "803815",
                      "ParentBackdropImageTags":  [
                                                      "e8262cd5610841775fd69a4d373f6c6e"
                                                  ],
                      "UserData":  {
                                       "PlaybackPositionTicks":  0,
                                       "PlayCount":  3,
                                       "IsFavorite":  false,
                                       "LastPlayedDate":  "2020-02-22T19:12:15.0000000+00:00",
                                       "Played":  true
                                   },
                      "SeriesName":  "MacGyver",
                      "SeriesId":  "803815",
                      "SeasonId":  "803886",
                      "SeriesPrimaryImageTag":  "a25f80dc4cf5eea4a2706a6270f82bce",
                      "SeasonName":  "Season 4",
                      "ImageTags":  {
                                        "Primary":  "c4f78f9d89c25e51dc819ed59dbb6dfb"
                                    },
                      "BackdropImageTags":  [


                                            ],
                      "ParentLogoImageTag":  "f2f84153070fe3a7e7f09317e17b4622",
                      "ParentThumbItemId":  "803815",
                      "ParentThumbImageTag":  "5a8444ee937b8a2b3882063bc118acb4",
                      "MediaType":  "Video"
                  },
                  {
                      "Name":  "Restoring a Craftsman",
                      "ServerId":  "340d61099851442d9615694cc77f1e26",
                      "Id":  "831542",
                      "Path":  "\\\\10.1.0.130\\Local Cloud\\OneDrive\\Recorded TV\\Home Town (2017)\\Season 4\\Home Town S04E07 Restoring a Craftsman.ts",
                      "RunTimeTicks":  37085414010,
                      "IndexNumber":  7,
                      "ParentIndexNumber":  4,
                      "IsFolder":  false,
                      "Type":  "Episode",
                      "ParentBackdropItemId":  "827373",
                      "ParentBackdropImageTags":  [
                                                      "faad9f62968f813d220e7167887a1f63"
                                                  ],
                      "UserData":  {
                                       "PlaybackPositionTicks":  0,
                                       "PlayCount":  1,
                                       "IsFavorite":  false,
                                       "LastPlayedDate":  "2020-02-22T17:45:23.0000000+00:00",
                                       "Played":  true
                                   },
                      "SeriesName":  "Home Town",
                      "SeriesId":  "827373",
                      "SeasonId":  "827375",
                      "SeriesPrimaryImageTag":  "e200366702ef5c8f5688af7f1bb6cc20",
                      "SeasonName":  "Season 4",
                      "ImageTags":  {
                                        "Primary":  "859e5bf41663f794619c2c6ec433c72c"
                                    },
                      "BackdropImageTags":  [


                                            ],
                      "MediaType":  "Video"
                  }
              ],
    "TotalRecordCount":  3
}

Link to comment
Share on other sites

muzicman0

i think its more likely - excel

 

i was using excel 2007 - if that matters

I'm not using excel at all.  I am loading the csv into notepad++ since I have excel using traditional commas as a separator.

 

But, the scripts are not using either.  

Link to comment
Share on other sites

PenkethBoy

Ok - the way around that issue of using the first item as the basis for the csv - think you are correct in your assumption - without looking at the convertto-csv code directly

 

write the file out directly i.e. you control the columns from the json input and if a property is missing from an item - you can put in whatever "null" value you want to the csv file

 

and it does not need to be a csv file anymore etc...

Link to comment
Share on other sites

PenkethBoy

had another thought

 

if you sort the json by "SeasonName" the "unknown Season" episodes would be at the bottom 99% of the time

 so would prevent the convertto-csv "failing" unless they are the only episode(s) returned

 

you could do this in the api call - by adding &SortBy=SeasonName&SortOrder=Ascending or a variation on that

  • Like 1
Link to comment
Share on other sites

muzicman0

had another thought

 

if you sort the json by "SeasonName" the "unknown Season" episodes would be at the bottom 99% of the time

 so would prevent the convertto-csv "failing" unless they are the only episode(s) returned

 

you could do this in the api call - by adding &SortBy=SeasonName&SortOrder=Ascending or a variation on that

I added &SortBy=IndexNumber&SortOrder=Descending, and that appears to work.  IndexNumber seems to relate to episode number, so it will start with the highest episode number, and go down from there.

Link to comment
Share on other sites

  • 3 weeks later...
muzicman0

Is there a way to get the resolution of each episode returned?  If so, then I could edit my script so that the bit rate on 720 videos is less than 1080 videos.

Link to comment
Share on other sites

PenkethBoy

Yes - but its expensive in terms of json size and server resources - if thats not a problem then...

 

add Fields=Mediasources which will give you all the info emby has on each episode

 

if its just size you are after then a simple Fields=width,height will be a light way of doing it

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...