summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Williams <tgwil@tgwil.net>2025-03-30 14:26:23 -0400
committerTristan Williams <tgwil@tgwil.net>2025-03-30 14:26:23 -0400
commitf59ed6e3e030c5dd23b318f44796a6a009c392b9 (patch)
treec174f92e43171f31b8fd0df930915cb63ae00fdb
parentStyle consistency (diff)
downloadsortashuffle-f59ed6e3e030c5dd23b318f44796a6a009c392b9.tar.gz
sortashuffle-f59ed6e3e030c5dd23b318f44796a6a009c392b9.tar.bz2
sortashuffle-f59ed6e3e030c5dd23b318f44796a6a009c392b9.zip
Refactor for future work
-rw-r--r--sortashuffle.py59
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>"
21SOURCES = ["<SOURCE DIR>", 21SOURCES = ["<SOURCE DIR>",
22 "<SOURCE DIR>"] 22 "<SOURCE DIR>"]
23 23
24SHOWLIST = [[f for f in os.listdir(source)] for source in SOURCES] 24def 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
26WEIGHTLIST = [len(show) for show in SHOWLIST] 29def calculate_weights(showlist):
30 """Calculate the weights for each show; # of remaining episodes"""
31 return [len(show) for show in showlist]
27 32
28SHUFFLED = [] 33def select_show(showlist, weightlist):
29while 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
34count = 0 39def shuffle(showlist, weightlist):
35for 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
39INDEX = open(TARGET + "//" + "_PLAYLIST_INDEX.txt", 'w', encoding='utf-8') 48def deploy_symlinks(shuffled):
40INDEX.write("\n".join(SHUFFLED)) 49 """Deploy episode symlinks."""
41INDEX.close() 50 count = 0
51 for episode in shuffled:
52 os.symlink(episode, TARGET + "//" + str(count))
53 count += 1
54 return count
55
56def 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
63def 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
71if __name__=="__main__":
72 main()