diff options
| author | Tristan Williams <tgwil@tgwil.net> | 2025-03-30 14:26:23 -0400 |
|---|---|---|
| committer | Tristan Williams <tgwil@tgwil.net> | 2025-03-30 14:26:23 -0400 |
| commit | f59ed6e3e030c5dd23b318f44796a6a009c392b9 (patch) | |
| tree | c174f92e43171f31b8fd0df930915cb63ae00fdb | |
| parent | Style consistency (diff) | |
| download | sortashuffle-f59ed6e3e030c5dd23b318f44796a6a009c392b9.tar.gz sortashuffle-f59ed6e3e030c5dd23b318f44796a6a009c392b9.tar.bz2 sortashuffle-f59ed6e3e030c5dd23b318f44796a6a009c392b9.zip | |
Refactor for future work
| -rw-r--r-- | sortashuffle.py | 59 |
1 files changed, 45 insertions, 14 deletions
diff --git a/sortashuffle.py b/sortashuffle.py index 13a033e..31c57ef 100644 --- a/sortashuffle.py +++ b/sortashuffle.py | |||
| @@ -21,21 +21,52 @@ TARGET = "<TARGET DIR>" | |||
| 21 | SOURCES = ["<SOURCE DIR>", | 21 | SOURCES = ["<SOURCE DIR>", |
| 22 | "<SOURCE DIR>"] | 22 | "<SOURCE DIR>"] |
| 23 | 23 | ||
| 24 | SHOWLIST = [[f for f in os.listdir(source)] for source in SOURCES] | 24 | def collect_showlist(sources): |
| 25 | """Returns a list of lists. | ||
| 26 | Each sub-list represents a show, and elements represent episodes.""" | ||
| 27 | return [[f for f in os.listdir(source)] for source in sources] | ||
| 25 | 28 | ||
| 26 | WEIGHTLIST = [len(show) for show in SHOWLIST] | 29 | def calculate_weights(showlist): |
| 30 | """Calculate the weights for each show; # of remaining episodes""" | ||
| 31 | return [len(show) for show in showlist] | ||
| 27 | 32 | ||
| 28 | SHUFFLED = [] | 33 | def select_show(showlist, weightlist): |
| 29 | while any(SHOWLIST): | 34 | """Select a show, accounting weight.""" |
| 30 | selection = random.choices(range(len(SHOWLIST)), weights=WEIGHTLIST, k=1)[0] | 35 | return random.choices(range(len(showlist)), |
| 31 | SHUFFLED.append(SOURCES[selection] + "//" + SHOWLIST[selection].pop(0)) | 36 | weights=weightlist, |
| 32 | WEIGHTLIST[selection] = len(SHOWLIST[selection]) | 37 | k=1)[0] |
| 33 | 38 | ||
| 34 | count = 0 | 39 | def shuffle(showlist, weightlist): |
| 35 | for episode in SHUFFLED: | 40 | """Shuffle the playlist.""" |
| 36 | os.symlink(episode, TARGET + "//" + str(count)) | 41 | shuffled = [] |
| 37 | count += 1 | 42 | while any(showlist): |
| 43 | selection = select_show(showlist, weightlist, previous) | ||
| 44 | shuffled.append(SOURCES[selection] + "//" + showlist[selection].pop(0)) | ||
| 45 | weightlist[selection] = len(showlist[selection]) | ||
| 46 | return shuffled | ||
| 38 | 47 | ||
| 39 | INDEX = open(TARGET + "//" + "_PLAYLIST_INDEX.txt", 'w', encoding='utf-8') | 48 | def deploy_symlinks(shuffled): |
| 40 | INDEX.write("\n".join(SHUFFLED)) | 49 | """Deploy episode symlinks.""" |
| 41 | INDEX.close() | 50 | count = 0 |
| 51 | for episode in shuffled: | ||
| 52 | os.symlink(episode, TARGET + "//" + str(count)) | ||
| 53 | count += 1 | ||
| 54 | return count | ||
| 55 | |||
| 56 | def deploy_index(shuffled): | ||
| 57 | """Deploy playlist index.""" | ||
| 58 | index = open(TARGET + "//" + "_PLAYLIST_INDEX.txt", 'w', encoding='utf-8') | ||
| 59 | index.write("\n".join(shuffled)) | ||
| 60 | index.close() | ||
| 61 | return INDEX | ||
| 62 | |||
| 63 | def main(): | ||
| 64 | SHOWLIST = collect_showlist(SOURCES) | ||
| 65 | WEIGHTLIST = calculate_weights(SHOWLIST) | ||
| 66 | SHUFFLED = shuffle(SHOWLIST, WEIGHTLIST) | ||
| 67 | deploy_symlinks(SHUFFLED) | ||
| 68 | deploy_index(SHUFFLED) | ||
| 69 | return 0 | ||
| 70 | |||
| 71 | if __name__=="__main__": | ||
| 72 | main() | ||