Philippe Khin
Autoplay the latest episode of your favourite TV series with Python.
23 November 2016
When you're too lazy to remember when was the last episode you've watched
python
script

Does it also happen to you, when you watched an episode of your favourite TV series or animes  (downloaded on your PC), and the day after, you don't remember which episode you have watched anymore ?

Well, it happens often to me, especially when you skip watching for days. So I've written a small Python script in order to automate this, and it saves me a lot a time. I don't have to bother myself opening the TV series folder, double click it, and delete it each time.

Here's the code:

import subprocess
import send2trash
import os
import sys

while  True:	# loop to play next episode if ENTER is the input
  animeDirectory = "D:\Anime\\"
  animeTitle     = "Gintama"
  listEpisode    = os.listdir(animeDirectory + animeTitle)
  episodeToWatch = animeDirectory + animeTitle + "\\" + listEpisode[0]
  print("Now playing: "+ episodeToWatch)
  vlc_path       = r"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe"
  process        = subprocess.Popen([vlc_path, episodeToWatch])
  process.wait()


  isEpisodeViewed = input('''Episode finished ? :
  y - YES, delete episode.
  n - NO, don't delete episode.
  ENTER - Delete episode and play next episode.
  s - delete episode and shutdown the computer.
  ''')
  if isEpisodeViewed=="y":   # watch only one episode
    send2trash.send2trash(episodeToWatch)
    print ("Deleted : %s" % episodeToWatch)
    break
  elif isEpisodeViewed=="n":
    break		   # don't delete the unfinished episode
  elif isEpisodeViewed=="":  # watch the next episode
    send2trash.send2trash(episodeToWatch)
    print ("Deleted : %s" % episodeToWatch)
    continue
  elif isEpisodeViewed=="s": # delete episode and shutdown the computer
    send2trash.send2trash(episodeToWatch)
    print ("Deleted : %s" % episodeToWatch)
    os.system('shutdown -s')
    break

Replace the animeDirectory by the path to the folder of your TV series, and animeTitle by the name of the folder containing your TV series episodes.

This script will get all the name of the episodes containing in your specified TV series folder, and uses subprocess (which enables Python to execute other programs installed on your PC) to play the episode with VLC Player. So you have to specify the path to your VLC executable (the vlc.exe)

When you launch this script, it's gonna play the first episode, e.g., for me, I have Gintama[1080p] 01.mp4, Gintama[1080p] 02.mp4, etc...

After the end of an episode, as you close the VLC Player, the terminal will ask you (text in isEpisodeViewed) whether you want to watch the next episode or not, or shutdown after deleting the watched episode, by typing the corresponding letter. By pressing on the Enter key, it will play the next episode, after sending the watched episode to your bin.

Alternatively, you can modify the code to suit your own way of managing your episode file. For example, instead of deleting it after watching, you can rename it by for instance, appending a "Watched" string to the beginning of the file name, and check with an if statement by calling startswith('Watched'). And if you watch several TV series, you can use an input and write the name of the series folder, or choose it from a list printed on the terminal etc...

Hope this little script will save you time !

For the complete code, please check my GitHub repository.

© 2020, Philippe Khin