diff options
author | Tristan Williams <tgwil@tgwil.net> | 2025-03-30 19:02:05 -0400 |
---|---|---|
committer | Tristan Williams <tgwil@tgwil.net> | 2025-03-30 19:02:05 -0400 |
commit | 88445f508f9d5a7f4357c7e2d549b4ab35eadf4f (patch) | |
tree | a4b140b3433ac4df84c9eda80f9748b4d003fb50 | |
parent | Okay I actually tested it this time and it works (diff) | |
download | sortashuffle-88445f508f9d5a7f4357c7e2d549b4ab35eadf4f.tar.gz sortashuffle-88445f508f9d5a7f4357c7e2d549b4ab35eadf4f.tar.bz2 sortashuffle-88445f508f9d5a7f4357c7e2d549b4ab35eadf4f.zip |
Conform to PEP 8 conventions
-rw-r--r-- | sortashuffle.py | 108 |
1 files changed, 97 insertions, 11 deletions
diff --git a/sortashuffle.py b/sortashuffle.py index ea03ff7..1bb7047 100644 --- a/sortashuffle.py +++ b/sortashuffle.py | |||
@@ -1,7 +1,9 @@ | |||
1 | #!/usr/bin/env python3 | 1 | #!/usr/bin/env python3 |
2 | |||
3 | # sortashuffle.py | 2 | # sortashuffle.py |
4 | # Shuffles shows but keeps episodes in order. | 3 | |
4 | # REQUIREMENTS | ||
5 | # - Must be run as administrator on Windows | ||
6 | # - All episodes of each show must be in one folder; no season folders! | ||
5 | 7 | ||
6 | # USAGE | 8 | # USAGE |
7 | # 0. Copy this script to the playlist folder | 9 | # 0. Copy this script to the playlist folder |
@@ -16,28 +18,87 @@ | |||
16 | import os | 18 | import os |
17 | import random | 19 | import random |
18 | 20 | ||
21 | |||
19 | # Must be in format "C://Dir1//Dir2" | 22 | # Must be in format "C://Dir1//Dir2" |
20 | TARGET = "<TARGET DIR>" | 23 | TARGET = "<TARGET DIR>" |
21 | SOURCES = ["<SOURCE DIR>", | 24 | SOURCES = ["<SOURCE DIR>", |
22 | "<SOURCE DIR>"] | 25 | "<SOURCE DIR>"] |
23 | 26 | ||
27 | |||
24 | def collect_showlist(sources): | 28 | def collect_showlist(sources): |
25 | """Returns a list of lists. | 29 | """Collect show dirs and associated episodes in a nested list. |
26 | Each sub-list represents a show, and elements represent episodes.""" | 30 | |
31 | Parameters: | ||
32 | sources (list of str): List of paths to scan for episodes. | ||
33 | Each path should represent a distinct show, with all | ||
34 | episodes in one flat directory. | ||
35 | |||
36 | Returns: | ||
37 | list of list of str: Nested list structure where: | ||
38 | - each sub-list represents a show. | ||
39 | - elements of sub-lists represent episodes. | ||
40 | |||
41 | Notes: | ||
42 | - Episode filenames are returned in arbitrary filesystem order. | ||
43 | - No filtering is performed. All files are included. | ||
44 | - Does not perform recursive directory scanning | ||
45 | """ | ||
27 | return [[f for f in os.listdir(source)] for source in sources] | 46 | return [[f for f in os.listdir(source)] for source in sources] |
28 | 47 | ||
48 | |||
29 | def calculate_weights(showlist): | 49 | def calculate_weights(showlist): |
30 | """Calculate the weights for each show; # of remaining episodes""" | 50 | """Calculate the weights to be used for selecting a show at random. |
51 | |||
52 | The number of episodes is used as the weight because shows with more | ||
53 | episodes need to be selected more often in order to maintain an even | ||
54 | spread of each show across the whole playlist. | ||
55 | |||
56 | Parameters: | ||
57 | showlist (list of list of str): Nested list structure where: | ||
58 | - each sub-list represents a show. | ||
59 | - elements of sub-lists represent episodes. | ||
60 | |||
61 | Returns: | ||
62 | list of int: List where: | ||
63 | - each element corresponds to a show in `showlist' | ||
64 | - each element represents the remaining number of episodes. | ||
65 | """ | ||
31 | return [len(show) for show in showlist] | 66 | return [len(show) for show in showlist] |
32 | 67 | ||
68 | |||
33 | def select_show(showlist, weightlist): | 69 | def select_show(showlist, weightlist): |
34 | """Select a show, accounting weight.""" | 70 | """Select a show at random, accounting for weight. |
71 | |||
72 | Parameters: | ||
73 | showlist (list of list of str): Nested list structure where: | ||
74 | - each sub-list represents a show. | ||
75 | - elements of sub-lists represent episodes. | ||
76 | weightlist (list of int): List where: | ||
77 | - each element corresponds to a show in `showlist' | ||
78 | - each element represents the remaining number of episodes. | ||
79 | |||
80 | Returns: | ||
81 | int: The list index of a show in the showlist. | ||
82 | """ | ||
35 | return random.choices(range(len(showlist)), | 83 | return random.choices(range(len(showlist)), |
36 | weights=weightlist, | 84 | weights=weightlist, |
37 | k=1)[0] | 85 | k=1)[0] |
38 | 86 | ||
87 | |||
39 | def shuffle(showlist, weightlist): | 88 | def shuffle(showlist, weightlist): |
40 | """Shuffle the playlist.""" | 89 | """Shuffle the playlist. |
90 | |||
91 | Parameters: | ||
92 | showlist (list of list of str): Nested list structure where: | ||
93 | - each sub-list represents a show. | ||
94 | - elements of sub-lists represent episodes. | ||
95 | weightlist (list of int): List where: | ||
96 | - each element corresponds to a show in `showlist' | ||
97 | - each element represents the remaining number of episodes. | ||
98 | |||
99 | Returns: | ||
100 | shuffled (list of str): Flat list of shuffled episodes. | ||
101 | """ | ||
41 | shuffled = [] | 102 | shuffled = [] |
42 | while any(showlist): | 103 | while any(showlist): |
43 | selection = select_show(showlist, weightlist) | 104 | selection = select_show(showlist, weightlist) |
@@ -45,22 +106,46 @@ def shuffle(showlist, weightlist): | |||
45 | weightlist[selection] = len(showlist[selection]) | 106 | weightlist[selection] = len(showlist[selection]) |
46 | return shuffled | 107 | return shuffled |
47 | 108 | ||
109 | |||
48 | def deploy_symlinks(shuffled): | 110 | def deploy_symlinks(shuffled): |
49 | """Deploy episode symlinks.""" | 111 | """Deploy episode symlinks. |
112 | |||
113 | Creates a symlink for each episode in `shuffled'. | ||
114 | Uses the `count' in the for loop as filename to keep shuffled order. | ||
115 | |||
116 | Parameters: | ||
117 | shuffled (list of str): Flat list of shuffled episodes. | ||
118 | |||
119 | Returns: | ||
120 | count (int): The number of symlinks deployed. | ||
121 | """ | ||
50 | count = 0 | 122 | count = 0 |
51 | for episode in shuffled: | 123 | for episode in shuffled: |
52 | os.symlink(episode, TARGET + "//" + str(count)) | 124 | os.symlink(episode, TARGET + "//" + str(count)) |
53 | count += 1 | 125 | count += 1 |
54 | return count | 126 | return count |
55 | 127 | ||
128 | |||
56 | def deploy_index(shuffled): | 129 | def deploy_index(shuffled): |
57 | """Deploy playlist index.""" | 130 | """Deploy playlist index. |
131 | |||
132 | Create a file namned _PLAYLIST_INDEX.txt containing the shuffled | ||
133 | list of episodes. | ||
134 | |||
135 | Parameters: | ||
136 | shuffled (list): Flat list of shuffled episodes. | ||
137 | |||
138 | Returns: | ||
139 | index (str): Full path and filename of _PLAYLIST_INDEX.txt. | ||
140 | """ | ||
58 | index = open(TARGET + "//" + "_PLAYLIST_INDEX.txt", 'w', encoding='utf-8') | 141 | index = open(TARGET + "//" + "_PLAYLIST_INDEX.txt", 'w', encoding='utf-8') |
59 | index.write("\n".join(shuffled)) | 142 | index.write("\n".join(shuffled)) |
60 | index.close() | 143 | index.close() |
61 | return index | 144 | return index |
62 | 145 | ||
146 | |||
63 | def main(): | 147 | def main(): |
148 | """Shuffle shows but keeps episodes in order.""" | ||
64 | SHOWLIST = collect_showlist(SOURCES) | 149 | SHOWLIST = collect_showlist(SOURCES) |
65 | WEIGHTLIST = calculate_weights(SHOWLIST) | 150 | WEIGHTLIST = calculate_weights(SHOWLIST) |
66 | SHUFFLED = shuffle(SHOWLIST, WEIGHTLIST) | 151 | SHUFFLED = shuffle(SHOWLIST, WEIGHTLIST) |
@@ -68,5 +153,6 @@ def main(): | |||
68 | deploy_index(SHUFFLED) | 153 | deploy_index(SHUFFLED) |
69 | return 0 | 154 | return 0 |
70 | 155 | ||
71 | if __name__=="__main__": | 156 | |
157 | if __name__ == "__main__": | ||
72 | main() | 158 | main() |