diff options
| author | Tristan Williams <tgwil@tgwil.net> | 2025-03-19 12:14:25 -0400 |
|---|---|---|
| committer | Tristan Williams <tgwil@tgwil.net> | 2025-03-19 12:14:25 -0400 |
| commit | 01c44dead21298b22367c8817ce660e75c23dd04 (patch) | |
| tree | afde14286c73e6a4fcfe9956388b9aeaa87b1af4 | |
| download | sortashuffle-01c44dead21298b22367c8817ce660e75c23dd04.tar.gz sortashuffle-01c44dead21298b22367c8817ce660e75c23dd04.tar.bz2 sortashuffle-01c44dead21298b22367c8817ce660e75c23dd04.zip | |
Create sortashuffle.py
| -rw-r--r-- | sortashuffle.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/sortashuffle.py b/sortashuffle.py new file mode 100644 index 0000000..e15b357 --- /dev/null +++ b/sortashuffle.py | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | #!/usr/bin/env python3 | ||
| 2 | |||
| 3 | # sortashuffle.py | ||
| 4 | # Shuffles shows but keeps episodes in order. | ||
| 5 | |||
| 6 | # USAGE | ||
| 7 | # 1. Modify the TARGET variable below | ||
| 8 | # 2. Modify the SOURCES variable below | ||
| 9 | # 3. Run the script as administrator | ||
| 10 | |||
| 11 | import os | ||
| 12 | import random | ||
| 13 | |||
| 14 | # Playlist destination folder | ||
| 15 | TARGET = "<ENTER TARGET DIR>" | ||
| 16 | |||
| 17 | # Shows to include | ||
| 18 | # MUST be in format "C://Dir1//Dir2" | ||
| 19 | SOURCES = ["<SOURCE DIR>", | ||
| 20 | "<SOURCE DIR>"] | ||
| 21 | |||
| 22 | def get_episode_list(): | ||
| 23 | """Get list of episodes for each show in SOURCES.""" | ||
| 24 | SHOWLIST = [[f for f in os.listdir(source)] for source in SOURCES] | ||
| 25 | |||
| 26 | def get_weight_list(): | ||
| 27 | """Determine weights (# of episodes) so shows get spread evenly.""" | ||
| 28 | WEIGHTLIST = [len(show) for show in showlist] | ||
| 29 | |||
| 30 | def shuffle(): | ||
| 31 | """Shuffle shows, randomly selected according to weight.""" | ||
| 32 | SHUFFLED = [] | ||
| 33 | while any(SHOWLIST): | ||
| 34 | selection = random.choices(range(len(SHOWLIST)), weights=WEIGHTLIST, k=1)[0] | ||
| 35 | SHUFFLED.append(SOURCES[selection] + "//" + SHOWLIST[selection].pop(0)) | ||
| 36 | WEIGHTLIST[selection] = len(SHOWLIST[selection]) | ||
| 37 | |||
| 38 | def deploy_symlinks(): | ||
| 39 | """Deploy a symlink for each episode in the playlist.""" | ||
| 40 | count = 0 | ||
| 41 | for episode in SHUFFLED: | ||
| 42 | os.symlink(episode, TARGET + str(count)) | ||
| 43 | count += 1 | ||
| 44 | |||
| 45 | def deploy_index(): | ||
| 46 | """Deploy playlist index file.""" | ||
| 47 | INDEX = open(TARGET + "//" + "_PLAYLIST_INDEX.txt", 'w') | ||
| 48 | INDEX.write("\n".join(SHUFFLED)) | ||
| 49 | INDEX.close() | ||
| 50 | |||
| 51 | def main(): | ||
| 52 | get_episode_list() | ||
| 53 | get_weight_list() | ||
| 54 | shuffle() | ||
| 55 | deploy_symlinks() | ||
| 56 | deploy_index() | ||
| 57 | |||
| 58 | if __name__=="__main__": | ||
| 59 | main() | ||