Jump to content

Buidling auto clean-up plugin. Need some hints help to get started


Powdor

Recommended Posts

Powdor

Hello,

The idea sounds simple. I would like to have the system automatically marks items for deletion (and possibly delete them in a second stage)  if they are not used and have been present for a long time. This is of course a little more complex because "not used" means any activity on a TV serie should treat the entire serie as active.

Line of thinking so far:

  • Rules that would make the files eligible for deletion using cutoff time in number of days:
    • Based on the timestamp the item was added to the library (DateCreated)
    • Exclude those items, collections and related items where user activity was found in the same time period 
  • Was it marked as favorite (exclude from deletion)

There is no filter available for MaxDateCreated on libraryManager.GetItemList. But filtering later is okay (don't know yet how it will behave with big collections).

I could not find / understand how to get the user activity rolled up so I can process it. This is where I hope someone can give me some hints.

I have been looking at IActivityManager and UserDataManager. But both use some linking that is not obvious to me.

ps. I have started the work over here: Powdor/EmbyMediaCleaner: Plugin to automatically clean your local media library of watched, old files (github.com)

Link to comment
Share on other sites

Hi, I would probably suggest a scheduled task with filters to figure out what to delete. I wouldn't really use IActivityManager as you can't really sort and filter on that data.

If you need new filters added then let me know. Thanks.

Link to comment
Share on other sites

Powdor

@luke yes I think a filter MaxDateCreated would be welcome to be able to filter the initial list down significanlty. But how do I get to the data about the last played date for a specific item or even beter for a TV show or Season. Which API should I be looking into?

Link to comment
Share on other sites

bakes82

If youre doing the sched task way its the libraryManager.

 

 var mediaItems = libraryManager.GetItemList(new InternalItemsQuery
                                                        {
                                                            IncludeItemTypes = new[] { nameof(Movie) },
                                                            IsVirtualItem    = false,
                                                            OrderBy = new[]
                                                                      {
                                                                          new ValueTuple<string,
                                                                              SortOrder>(ItemSortBy.PremiereDate,
                                                                           SortOrder.Descending),
                                                                          new ValueTuple<string,
                                                                              SortOrder>(ItemSortBy.SeriesSortNameOrSortName,
                                                                           SortOrder.Ascending)
                                                                      }
                                                        })
                                           .ToList();

Then loop/filter the movies again, here a quick loop

image.png.76fbf13ffa1207c5335664150dc04899.png

Link to comment
Share on other sites

  • 2 weeks later...
Powdor

I was playing around a bit to understand the data structure.. I figured it out from db perspective but I am not sure how things ar mapped into the librarymanager items exactly.. But more importantly I cannot get back LastPlayedDate or PlayCount to be anything but null? I think I must be missing something ..

image.png.e1430db9caab333906342b02d3e774b9.png

 

I do see the fact that  it was played by a user tracked here:
image.png.5d18abed53540a26745c4e521542ae70.png

Edited by Powdor
Link to comment
Share on other sites

Powdor

I am looking to implement this kind of filtering.. Any thoughts on this?
 

select count(m.Id) Count, sum(m.size)/1024/1024/1024 SizeInGb
from MediaItems m
where 
m.Size > 0
and m.DateModified < StrfTime('%s', Date('now', '-365 day'))

AND m.Id not in
(SELECT m.Id
FROM UserDatas u
LEFT OUTER JOIN MediaItems m on u.UserDataKeyId = m.UserDataKeyId
WHERE m.MediaType = 1
and LastPlayedDateInt > StrfTime('%s', Date('now', '-365 day'))) 

and 
(m.SeriesId is null or
m.SeriesId not in
(SELECT 
distinct m.SeriesId SeriesId
FROM UserDatas u
LEFT OUTER JOIN MediaItems m on u.UserDataKeyId = m.UserDataKeyId
WHERE 
 not (m.SeriesId is null) and
 LastPlayedDateInt > StrfTime('%s', Date('now', '-365 day')))
 )

 

Link to comment
Share on other sites

On 5/20/2022 at 5:44 AM, Powdor said:

I am looking to implement this kind of filtering.. Any thoughts on this?
 

select count(m.Id) Count, sum(m.size)/1024/1024/1024 SizeInGb
from MediaItems m
where 
m.Size > 0
and m.DateModified < StrfTime('%s', Date('now', '-365 day'))

AND m.Id not in
(SELECT m.Id
FROM UserDatas u
LEFT OUTER JOIN MediaItems m on u.UserDataKeyId = m.UserDataKeyId
WHERE m.MediaType = 1
and LastPlayedDateInt > StrfTime('%s', Date('now', '-365 day'))) 

and 
(m.SeriesId is null or
m.SeriesId not in
(SELECT 
distinct m.SeriesId SeriesId
FROM UserDatas u
LEFT OUTER JOIN MediaItems m on u.UserDataKeyId = m.UserDataKeyId
WHERE 
 not (m.SeriesId is null) and
 LastPlayedDateInt > StrfTime('%s', Date('now', '-365 day')))
 )

 

what is the result?

Link to comment
Share on other sites

  • 3 weeks later...
Powdor

The first part is just counting the number of items. But I would like to take a list of items that I then try to delete.

The main filter is just selecting all that is older than 365 days

 m.DateModified < StrfTime('%s', Date('now', '-365 day'))

This sub select removes items from the result list that have been watched/played in the past 365 days (ie. items of interest)

AND m.Id not in
(SELECT m.Id
FROM UserDatas u
LEFT OUTER JOIN MediaItems m on u.UserDataKeyId = m.UserDataKeyId
WHERE m.MediaType = 1
and LastPlayedDateInt > StrfTime('%s', Date('now', '-365 day'))) 

The next sub select removes items from the result list that are part of a tv series / show of which had items that have been watched/played in the past 365 days

 

and 
(m.SeriesId is null or
m.SeriesId not in
(SELECT 
distinct m.SeriesId SeriesId
FROM UserDatas u
LEFT OUTER JOIN MediaItems m on u.UserDataKeyId = m.UserDataKeyId
WHERE 
 not (m.SeriesId is null) and
 LastPlayedDateInt > StrfTime('%s', Date('now', '-365 day')))
 )

 

Is there a way to use these queries directly or do you think this can be accomplished with existing filters / query facilities?

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