Files
media-episode-viewer/ADJUSTMENT.md
T
2026-02-22 15:15:07 +08:00

5.7 KiB

This is complicated than I thought, so we'll adjust our approach.

Jellyfin available APIs for this

curl --location 'http://saturday.local/System/ActivityLog/Entries?api_key=<apikey></apikey>&hasUserId=true&minDate=2026-02-20T16%3A00%3A00.000Z&limit=100' \
--header 'accept: application/json'
  • It doesn't even accept specific dates UP to a certain point. we could theoretically take the activities from a given mindate and reverse the list, but we can't be sure if the earliest item is the earliest for that date.

So we'll limit our scope to only run this app on that sunday or within few days after, at least not over 7 days; ideally I'd run this within 3 days after the last Saturday.

Also I may have to persistently keep some record of the last episode watched, so we'll need an SQLite database to store the details.

And let's keep it extremely simple, let's ditch Sonarr API for this.

New method:

  1. Before all these of course, we have to take the latest Sunday date e.g. today is February 24, 2026, so the latest Sunday is February 22, 2026 12:00:00 PM PHT or February 21, 2026 16:00:00 UTC. We can calculate this date by using the current date and adjusting it to the nearest Sunday.
  2. Take activity logs from the last 7 days, and get the last One Piece episode watched before the Sunday.
  • Although the sunday must be in UTC converted from PHT, so e.g. February 22, 2026 12:00:00 PM PHT is February 21, 2026 16:00:00 UTC, so we have to make sure we get the accurate activity log e.g.
[
  {
      "Id": 15658,
      "Name": "pat has finished playing One Piece - The Straw Hats Run! Save the Captive Tonoyasu! on Apple TV",
      "Type": "VideoPlaybackStopped",
      "ItemId": "9c543413ef481048f33c5bd0f4a6bd8c",
      "Date": "2026-02-21T14:10:23.4402504Z",
      "UserId": "d0e714841b1f4588bf7d90153460180b",
      "Severity": "Information"
  },
  // other activity logs
]
  • Based on the sample log above, we can see that the user "pat" has finished playing an episode of One Piece on Apple TV. The ItemId is "9c543413ef481048f33c5bd0f4a6bd8c", and the date of the activity is "2026-02-21T14:10:23.4402504Z". We can use this information to determine which episode was watched and when it was watched.
  • If for example I ran this script on Feberuary 24, and I alraedy watched another episode of One Piece, we have to take the last episode watched before the Sunday, which is February 22, 2026 12:00:00 PM PHT or February 21, 2026 16:00:00 UTC. So we have to filter the activity logs to only include those that occurred before that date and time.
  • The data doesn't contain the episode number, so we have to extract the playback info from the given ItemId e.g.
curl --location 'http://saturday.local/Items/<itemid>/PlaybackInfo?api_key=<apikey>' \
--header 'accept: application/json'

# response:
{
    "MediaSources": [
        {
            "Protocol": "File",
            "Id": "9c543413ef481048f33c5bd0f4a6bd8c",
            "Path": "/media/ANIME/[ONGOING]/One Piece/Season 21/One Piece - S21E48 (939) - The Straw Hats Run! Save the Captive Tonoyasu!.mkv",
            "Type": "Default",
            "Container": "mkv",
            "Size": 465009687,
            "Name": "One Piece - S21E48 (939) - The Straw Hats Run! Save the Captive Tonoyasu!",
// other media source details
        }
    ]
}
  • using the data from the MediaSources, we can extract the episode number (e.g. 939) from the "Name" field, which contains the episode title and number. We can then use this information to determine which episode was watched and when it was watched.
  • We can then store this information in an SQLite database, along with the date and time of the activity, so that we can keep track of which episodes have been watched and when they were watched. This will allow us to easily determine which episode to watch next on the following Saturday.

as for the initial "previous saturday" episode I watched, it was 928 so let's just seed it.

All in all, I'm just expecting the output for the report to be:

ONE PIECE PROGRESS!!! (Feb 8-14) Episode 928/1155 Around 10 episodes watched this week!

Literally the template is:

ONE PIECE PROGRESS!!! (MMM D1-D2) Episode X/Y Around Z episodes watched this week!

  • Note: The date range depends if the date overlaps with the previous month e.g. Mar 29 - Apr 4; if not, then just within the same month e.g. Feb 8-14. The first date is the previous Sunday, and the second date is the following Saturday.
  • X is of course the last episode we extracted based on the method above, and Y is the latest total episode; let's make this manually configurable until we figure out how we can extract this from Sonarr. the current latest episode is 1155, we'll seed this as well.

Conclusion and Summary

  • New approach:
  1. Calculate the latest Sunday date based on the current date.
  2. Fetch activity logs from the last 7 days and filter them to find the last One Piece episode watched before the calculated Sunday date.
  3. Extract the episode number from the media source information using the ItemId from the activity log.
  4. Store the episode number and the date of the activity in an SQLite database for future reference.
  5. Generate a report based on the stored data, showing the progress of One Piece episodes watched within the week, including the date range, the last episode watched, the total number of episodes, and the number of episodes watched during the week.
  • This approach allows us to accurately track the progress of One Piece episodes watched each week, while also providing a clear and concise report for easy reference. By using an SQLite database, we can persistently store the episode information and easily retrieve it for future reports, ensuring that we have a comprehensive record of our One Piece viewing history.