Sqlite3 query to get recent TV episodes

I have a script that I run that returns the 5 most recent movies in my Plex server, and I have tried to do the same thing to give me a list of the most recent TV episodes, but am unable to get ti to work.

Basically, I want an sqlite3 command that I can executer at the shell that returns something like “Modern Family, The magicians, You’re the worst, The Daily Show, The Goldbergs” when I ask for the 5 newest episodes. I don’t want the episode titles just the series name, but I cannot figure out how to get it.

I thought something like this

SELECT * FROM metadata_items WHERE library_section_id=1 and title like "%You Owe Me a Unicorn%";

would at least get me started, but that doesn’t show a series name at all (It’s The Passage).

If I do this instead

SELECT * FROM metadata_items join media_items WHERE metadata_items.library_section_id=1 and title like "%You Owe Me a Unicorn%";

I do get results that list "The%20Passage, but I get results that include every single episode of every single series with the info for that episode prepended to each line.

1 Like

Try this (might need to change section ID):

select episodes.id,shows.title,episodes.title,seasons.`index`,episodes.`index` from metadata_items as episodes join metadata_items as seasons on seasons.id=episodes.parent_id join metadata_items as shows on shows.id=seasons.parent_id where episodes.library_section_id=2 and episodes.metadata_type=4 order by episodes.added_at desc limit 10;
1 Like

Wow. Well yes, that works (mostly, it lists the Daily show twice because there were two new episodes) but how would I have gotten there? Can you explain what the index does and what metadata_type=4 does?

1 Like

Can you explain what the index does

Index is the field which holds season and episode numbers.

and what metadata_type=4 does?

4 = episode
3 = season
2 = show
1 = movie

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.