1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
#!/usr/bin/env python3
# sortashuffle.py
# Shuffles shows but keeps episodes in order.
# USAGE
# 0. Copy this script to the playlist folder
# 1. Modify the TARGET variable below
# 2. Modify the SOURCES variable below
# 3. Run the script as administrator
#
# To add/remove shows or regenerate playlist,
# delete everything in TARGET (except this script)
# and re-run the script.
import os
import random
# Must be in format "C://Dir1//Dir2"
TARGET = "<TARGET DIR>"
SOURCES = ["<SOURCE DIR>",
"<SOURCE DIR>"]
def collect_showlist(sources):
"""Returns a list of lists.
Each sub-list represents a show, and elements represent episodes."""
return [[f for f in os.listdir(source)] for source in sources]
def calculate_weights(showlist):
"""Calculate the weights for each show; # of remaining episodes"""
return [len(show) for show in showlist]
def select_show(showlist, weightlist):
"""Select a show, accounting weight."""
return random.choices(range(len(showlist)),
weights=weightlist,
k=1)[0]
def shuffle(showlist, weightlist):
"""Shuffle the playlist."""
shuffled = []
while any(showlist):
selection = select_show(showlist, weightlist)
shuffled.append(SOURCES[selection] + "//" + showlist[selection].pop(0))
weightlist[selection] = len(showlist[selection])
return shuffled
def deploy_symlinks(shuffled):
"""Deploy episode symlinks."""
count = 0
for episode in shuffled:
os.symlink(episode, TARGET + "//" + str(count))
count += 1
return count
def deploy_index(shuffled):
"""Deploy playlist index."""
index = open(TARGET + "//" + "_PLAYLIST_INDEX.txt", 'w', encoding='utf-8')
index.write("\n".join(shuffled))
index.close()
return INDEX
def main():
SHOWLIST = collect_showlist(SOURCES)
WEIGHTLIST = calculate_weights(SHOWLIST)
SHUFFLED = shuffle(SHOWLIST, WEIGHTLIST)
deploy_symlinks(SHUFFLED)
deploy_index(SHUFFLED)
return 0
if __name__=="__main__":
main()
|