Auto order episodes of Buffy the Vampire Slayer and Angel

Order the episodes of Buffy the Vampire Slayer and Angel automatically to the "best viewing order"

Some of my favorite series from my childhood are the Buffy the Vampire Slayer and Angel. Recently we started to re-watch it with my wife and found the best viewing order to do it to keep the continuity and enjoy the many crossover stories.

Anyway, I was lazy to rename 254 episodes by hand, so I created a python script to do it automatically. Enjoy!

import os

# BUFFY AND ANGEL EPISODE ORDER SCRIPT
# created by @ianchak
#
# Order all your Buffy and Angel episodes in the best viewing order automatically
# http://www.myths.com/pub/tv/buffy/viewing-order.html
#
# 1. Drop all episodes of Buffy and Angel to a folder
# 2. Drop this file in the same folder
# 3. Run the script
#       - The script adds 001. - 254. prefixes to your filenames in the right order
#       - The script detects already prefixed files and skips them


class Episode:
    def __init__(self, type, episode):
        self.type = type
        self.episode = episode


matches = [
    Episode("Buffy", "S01E01"),
    Episode("Buffy", "S01E02"),
    Episode("Buffy", "S01E03"),
    Episode("Buffy", "S01E04"),
    Episode("Buffy", "S01E05"),
    Episode("Buffy", "S01E06"),
    Episode("Buffy", "S01E07"),
    Episode("Buffy", "S01E08"),
    Episode("Buffy", "S01E09"),
    Episode("Buffy", "S01E10"),
    Episode("Buffy", "S01E11"),
    Episode("Buffy", "S01E12"),
    Episode("Buffy", "S02E01"),
    Episode("Buffy", "S02E02"),
    Episode("Buffy", "S02E03"),
    Episode("Buffy", "S02E04"),
    Episode("Buffy", "S02E05"),
    Episode("Buffy", "S02E06"),
    Episode("Buffy", "S02E07"),
    Episode("Buffy", "S02E08"),
    Episode("Buffy", "S02E09"),
    Episode("Buffy", "S02E10"),
    Episode("Buffy", "S02E11"),
    Episode("Buffy", "S02E12"),
    Episode("Buffy", "S02E13"),
    Episode("Buffy", "S02E14"),
    Episode("Buffy", "S02E15"),
    Episode("Buffy", "S02E16"),
    Episode("Buffy", "S02E17"),
    Episode("Buffy", "S02E18"),
    Episode("Buffy", "S02E19"),
    Episode("Buffy", "S02E20"),
    Episode("Buffy", "S02E21"),
    Episode("Buffy", "S02E22"),
    Episode("Buffy", "S03E01"),
    Episode("Buffy", "S03E02"),
    Episode("Buffy", "S03E03"),
    Episode("Buffy", "S03E04"),
    Episode("Buffy", "S03E05"),
    Episode("Buffy", "S03E06"),
    Episode("Buffy", "S03E07"),
    Episode("Buffy", "S03E08"),
    Episode("Buffy", "S03E09"),
    Episode("Buffy", "S03E10"),
    Episode("Buffy", "S03E11"),
    Episode("Buffy", "S03E12"),
    Episode("Buffy", "S03E13"),
    Episode("Buffy", "S03E14"),
    Episode("Buffy", "S03E15"),
    Episode("Buffy", "S03E16"),
    Episode("Buffy", "S03E17"),
    Episode("Buffy", "S03E18"),
    Episode("Buffy", "S03E19"),
    Episode("Buffy", "S03E20"),
    Episode("Buffy", "S03E21"),
    Episode("Buffy", "S03E22"),
    Episode("Buffy", "S04E01"),
    Episode("Angel", "S01E01"),
    Episode("Buffy", "S04E02"),
    Episode("Angel", "S01E02"),
    Episode("Buffy", "S04E03"),
    Episode("Angel", "S01E03"),
    Episode("Buffy", "S04E04"),
    Episode("Angel", "S01E04"),
    Episode("Buffy", "S04E05"),
    Episode("Angel", "S01E05"),
    Episode("Buffy", "S04E06"),
    Episode("Angel", "S01E06"),
    Episode("Buffy", "S04E07"),
    Episode("Angel", "S01E07"),
    Episode("Buffy", "S04E08"),
    Episode("Angel", "S01E08"),
    Episode("Buffy", "S04E09"),
    Episode("Angel", "S01E09"),
    Episode("Buffy", "S04E10"),
    Episode("Buffy", "S04E11"),
    Episode("Angel", "S01E10"),
    Episode("Angel", "S01E11"),
    Episode("Buffy", "S04E12"),
    Episode("Angel", "S01E12"),
    Episode("Angel", "S01E13"),
    Episode("Buffy", "S04E13"),
    Episode("Buffy", "S04E14"),
    Episode("Angel", "S01E14"),
    Episode("Angel", "S01E15"),
    Episode("Buffy", "S04E15"),
    Episode("Buffy", "S04E16"),
    Episode("Angel", "S01E16"),
    Episode("Buffy", "S04E17"),
    Episode("Angel", "S01E17"),
    Episode("Buffy", "S04E18"),
    Episode("Buffy", "S04E19"),
    Episode("Angel", "S01E18"),
    Episode("Angel", "S01E19"),
    Episode("Buffy", "S04E20"),
    Episode("Buffy", "S04E21"),
    Episode("Buffy", "S04E22"),
    Episode("Angel", "S01E20"),
    Episode("Angel", "S01E21"),
    Episode("Angel", "S01E22"),
    Episode("Angel", "S02E01"),
    Episode("Buffy", "S05E01"),
    Episode("Angel", "S02E02"),
    Episode("Buffy", "S05E02"),
    Episode("Angel", "S02E03"),
    Episode("Buffy", "S05E03"),
    Episode("Angel", "S02E04"),
    Episode("Buffy", "S05E04"),
    Episode("Angel", "S02E05"),
    Episode("Buffy", "S05E05"),
    Episode("Angel", "S02E06"),
    Episode("Buffy", "S05E06"),
    Episode("Angel", "S02E07"),
    Episode("Buffy", "S05E07"),
    Episode("Angel", "S02E08"),
    Episode("Buffy", "S05E08"),
    Episode("Angel", "S02E09"),
    Episode("Buffy", "S05E09"),
    Episode("Angel", "S02E10"),
    Episode("Buffy", "S05E10"),
    Episode("Angel", "S02E11"),
    Episode("Buffy", "S05E11"),
    Episode("Angel", "S02E12"),
    Episode("Buffy", "S05E12"),
    Episode("Angel", "S02E13"),
    Episode("Buffy", "S05E13"),
    Episode("Angel", "S02E14"),
    Episode("Buffy", "S05E14"),
    Episode("Angel", "S02E15"),
    Episode("Buffy", "S05E15"),
    Episode("Angel", "S02E16"),
    Episode("Buffy", "S05E16"),
    Episode("Angel", "S02E17"),
    Episode("Buffy", "S05E17"),
    Episode("Angel", "S02E18"),
    Episode("Buffy", "S05E18"),
    Episode("Angel", "S02E19"),
    Episode("Angel", "S02E20"),
    Episode("Buffy", "S05E19"),
    Episode("Buffy", "S05E20"),
    Episode("Angel", "S02E21"),
    Episode("Buffy", "S05E21"),
    Episode("Buffy", "S05E22"),
    Episode("Angel", "S02E22"),
    Episode("Angel", "S03E01"),
    Episode("Angel", "S03E02"),
    Episode("Angel", "S03E03"),
    Episode("Buffy", "S06E01"),
    Episode("Buffy", "S06E02"),
    Episode("Buffy", "S06E03"),
    Episode("Angel", "S03E04"),
    Episode("Buffy", "S06E04"),
    Episode("Angel", "S03E05"),
    Episode("Buffy", "S06E05"),
    Episode("Angel", "S03E06"),
    Episode("Buffy", "S06E06"),
    Episode("Angel", "S03E07"),
    Episode("Buffy", "S06E07"),
    Episode("Angel", "S03E08"),
    Episode("Buffy", "S06E08"),
    Episode("Angel", "S03E09"),
    Episode("Buffy", "S06E09"),
    Episode("Buffy", "S06E10"),
    Episode("Angel", "S03E10"),
    Episode("Buffy", "S06E11"),
    Episode("Angel", "S03E11"),
    Episode("Angel", "S03E12"),
    Episode("Buffy", "S06E12"),
    Episode("Angel", "S03E13"),
    Episode("Buffy", "S06E13"),
    Episode("Buffy", "S06E14"),
    Episode("Angel", "S03E14"),
    Episode("Angel", "S03E15"),
    Episode("Buffy", "S06E15"),
    Episode("Angel", "S03E16"),
    Episode("Buffy", "S06E16"),
    Episode("Buffy", "S06E17"),
    Episode("Angel", "S03E17"),
    Episode("Angel", "S03E18"),
    Episode("Angel", "S03E19"),
    Episode("Buffy", "S06E18"),
    Episode("Angel", "S03E20"),
    Episode("Buffy", "S06E19"),
    Episode("Angel", "S03E21"),
    Episode("Buffy", "S06E20"),
    Episode("Angel", "S03E22"),
    Episode("Buffy", "S06E21"),
    Episode("Buffy", "S06E22"),
    Episode("Buffy", "S07E01"),
    Episode("Buffy", "S07E02"),
    Episode("Angel", "S04E01"),
    Episode("Buffy", "S07E03"),
    Episode("Angel", "S04E02"),
    Episode("Buffy", "S07E04"),
    Episode("Angel", "S04E03"),
    Episode("Buffy", "S07E05"),
    Episode("Angel", "S04E04"),
    Episode("Angel", "S04E05"),
    Episode("Buffy", "S07E06"),
    Episode("Angel", "S04E06"),
    Episode("Buffy", "S07E07"),
    Episode("Angel", "S04E07"),
    Episode("Buffy", "S07E08"),
    Episode("Buffy", "S07E09"),
    Episode("Buffy", "S07E10"),
    Episode("Buffy", "S07E11"),
    Episode("Angel", "S04E08"),
    Episode("Buffy", "S07E12"),
    Episode("Angel", "S04E09"),
    Episode("Angel", "S04E10"),
    Episode("Buffy", "S07E13"),
    Episode("Angel", "S04E11"),
    Episode("Buffy", "S07E14"),
    Episode("Angel", "S04E12"),
    Episode("Buffy", "S07E15"),
    Episode("Buffy", "S07E16"),
    Episode("Angel", "S04E13"),
    Episode("Angel", "S04E14"),
    Episode("Buffy", "S07E17"),
    Episode("Angel", "S04E15"),
    Episode("Angel", "S04E16"),
    Episode("Angel", "S04E17"),
    Episode("Angel", "S04E18"),
    Episode("Buffy", "S07E18"),
    Episode("Angel", "S04E19"),
    Episode("Angel", "S04E20"),
    Episode("Buffy", "S07E19"),
    Episode("Angel", "S04E21"),
    Episode("Buffy", "S07E20"),
    Episode("Angel", "S04E22"),
    Episode("Buffy", "S07E21"),
    Episode("Buffy", "S07E22"),
    Episode("Angel", "S05E01"),
    Episode("Angel", "S05E02"),
    Episode("Angel", "S05E03"),
    Episode("Angel", "S05E04"),
    Episode("Angel", "S05E05"),
    Episode("Angel", "S05E06"),
    Episode("Angel", "S05E07"),
    Episode("Angel", "S05E08"),
    Episode("Angel", "S05E09"),
    Episode("Angel", "S05E10"),
    Episode("Angel", "S05E11"),
    Episode("Angel", "S05E12"),
    Episode("Angel", "S05E13"),
    Episode("Angel", "S05E14"),
    Episode("Angel", "S05E15"),
    Episode("Angel", "S05E16"),
    Episode("Angel", "S05E17"),
    Episode("Angel", "S05E18"),
    Episode("Angel", "S05E19"),
    Episode("Angel", "S05E20"),
    Episode("Angel", "S05E21"),
    Episode("Angel", "S05E22"),
]


def main():

    folder = "."
    for filename in os.listdir(folder):
        for match_idx, match in enumerate(matches, start=1):
            if (
                match.type.lower() in filename.lower()
                and match.episode.lower() in filename.lower()
                and filename[3] != "."
            ):
                epnum = str(match_idx).zfill(3)
                dst = f"{epnum}. {filename}"
                src = f"{folder}/{filename}"
                dst = f"{folder}/{dst}"
                print(f"Renaming {match.type} {match.episode} ==> {epnum}")

                os.rename(src, dst)


if __name__ == "__main__":
    main()