diff options
| author | Tristan Williams <tgwil@tgwil.net> | 2025-03-19 14:38:17 -0400 |
|---|---|---|
| committer | Tristan Williams <tgwil@tgwil.net> | 2025-03-19 14:38:17 -0400 |
| commit | 38af77ef4dd82ac5ee2ad3473fb77335e1273308 (patch) | |
| tree | d789e88eb585cc2fa2e3b51aa03e84acae2aa17b | |
| parent | Fix typo (diff) | |
| download | sortashuffle-38af77ef4dd82ac5ee2ad3473fb77335e1273308.tar.gz sortashuffle-38af77ef4dd82ac5ee2ad3473fb77335e1273308.tar.bz2 sortashuffle-38af77ef4dd82ac5ee2ad3473fb77335e1273308.zip | |
I should test things before I commit
| -rw-r--r-- | sortashuffle.py | 56 |
1 files changed, 18 insertions, 38 deletions
diff --git a/sortashuffle.py b/sortashuffle.py index 14cd082..aac1e51 100644 --- a/sortashuffle.py +++ b/sortashuffle.py | |||
| @@ -19,41 +19,21 @@ TARGET = "<ENTER TARGET DIR>" | |||
| 19 | SOURCES = ["<SOURCE DIR>", | 19 | SOURCES = ["<SOURCE DIR>", |
| 20 | "<SOURCE DIR>"] | 20 | "<SOURCE DIR>"] |
| 21 | 21 | ||
| 22 | def get_episode_list(): | 22 | SHOWLIST = [[f for f in os.listdir(source)] for source in SOURCES] |
| 23 | """Get list of episodes for each show in SOURCES.""" | 23 | |
| 24 | SHOWLIST = [[f for f in os.listdir(source)] for source in SOURCES] | 24 | WEIGHTLIST = [len(show) for show in SHOWLIST] |
| 25 | 25 | ||
| 26 | def get_weight_list(): | 26 | SHUFFLED = [] |
| 27 | """Determine weights (# of episodes) so shows get spread evenly.""" | 27 | while any(SHOWLIST): |
| 28 | WEIGHTLIST = [len(show) for show in SHOWLIST] | 28 | selection = random.choices(range(len(SHOWLIST)), weights=WEIGHTLIST, k=1)[0] |
| 29 | 29 | SHUFFLED.append(SOURCES[selection] + "//" + SHOWLIST[selection].pop(0)) | |
| 30 | def shuffle(): | 30 | WEIGHTLIST[selection] = len(SHOWLIST[selection]) |
| 31 | """Shuffle shows, randomly selected according to weight.""" | 31 | |
| 32 | SHUFFLED = [] | 32 | count = 0 |
| 33 | while any(SHOWLIST): | 33 | for episode in SHUFFLED: |
| 34 | selection = random.choices(range(len(SHOWLIST)), weights=WEIGHTLIST, k=1)[0] | 34 | os.symlink(episode, TARGET + "//" + str(count)) |
| 35 | SHUFFLED.append(SOURCES[selection] + "//" + SHOWLIST[selection].pop(0)) | 35 | count += 1 |
| 36 | WEIGHTLIST[selection] = len(SHOWLIST[selection]) | 36 | |
| 37 | 37 | INDEX = open(TARGET + "//" + "_PLAYLIST_INDEX.txt", 'w') | |
| 38 | def deploy_symlinks(): | 38 | INDEX.write("\n".join(SHUFFLED)) |
| 39 | """Deploy a symlink for each episode in the playlist.""" | 39 | INDEX.close() |
| 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() | ||