|
@@ -12,6 +12,7 @@ import os
|
|
|
import posixpath
|
|
|
import random
|
|
|
import re
|
|
|
+import sys
|
|
|
import urllib.request
|
|
|
from argparse import ArgumentParser, ArgumentTypeError
|
|
|
from bisect import bisect
|
|
@@ -24,6 +25,11 @@ from lxml import html
|
|
|
from pytimeparse.timeparse import timeparse
|
|
|
|
|
|
|
|
|
+def debug(*args, **kwargs):
|
|
|
+ kwargs.setdefault("file", sys.stderr)
|
|
|
+ print(*args, **kwargs)
|
|
|
+
|
|
|
+
|
|
|
def get_int(s):
|
|
|
return int(re.findall(r"\d+", s)[0])
|
|
|
|
|
@@ -57,6 +63,10 @@ def weighted_choice(values, weights):
|
|
|
Their meaning is "relative", that is [1,2,3] is the same as [2,4,6]
|
|
|
"""
|
|
|
assert len(values) == len(weights)
|
|
|
+ if not values:
|
|
|
+ raise IndexError("Cannot do weighted choice from an empty sequence")
|
|
|
+ if sum(weights) == 0:
|
|
|
+ raise IndexError("Cannot do weighted choice where weight=0")
|
|
|
total = 0
|
|
|
cum_weights = []
|
|
|
for w in weights:
|
|
@@ -488,17 +498,28 @@ def main():
|
|
|
sources = args.urls
|
|
|
|
|
|
if args.source_weights:
|
|
|
- weights = tuple(map(int, args.source_weights.split(":")))
|
|
|
+ weights = list(map(int, args.source_weights.split(":")))
|
|
|
if len(weights) != len(sources):
|
|
|
parser.exit(
|
|
|
status=2, message="Weight must be in the" " same number as sources\n"
|
|
|
)
|
|
|
- sources = [weighted_choice(sources, weights)]
|
|
|
+ else:
|
|
|
+ weights = [1] * len(sources)
|
|
|
|
|
|
- audios = []
|
|
|
- for url in sources:
|
|
|
+ audio_by_source = OrderedDict()
|
|
|
+ for i, url in enumerate(sources):
|
|
|
url_audios = retrieve(url, args)
|
|
|
- audios += [au for au in url_audios if audio_passes_filters(au, args)]
|
|
|
+ url_audios = [au for au in url_audios if audio_passes_filters(au, args)]
|
|
|
+ audio_by_source[url] = url_audios
|
|
|
+ if not url_audios:
|
|
|
+ weights[i] = 0
|
|
|
+ if sum(weights) == 0:
|
|
|
+ return
|
|
|
+ sources = [weighted_choice(sources, weights)]
|
|
|
+
|
|
|
+ audios = []
|
|
|
+ for source_url in sources:
|
|
|
+ audios += audio_by_source[source_url]
|
|
|
|
|
|
# sort
|
|
|
if args.sort_by == "random":
|
|
@@ -548,16 +569,17 @@ def main():
|
|
|
return
|
|
|
for audio in audios[:-1]:
|
|
|
if args.debug:
|
|
|
- print(repr(audio))
|
|
|
+ debug(repr(audio))
|
|
|
else:
|
|
|
put(audio, args.copy)
|
|
|
if args.slotsize is not None:
|
|
|
duration = audio.duration
|
|
|
if duration < args.slotsize:
|
|
|
+ # TODO: prendi musica da un'altra cartella
|
|
|
print("## musica per {} secondi".format(args.slotsize - duration))
|
|
|
# finally, the last one
|
|
|
if args.debug:
|
|
|
- print(repr(audios[-1]))
|
|
|
+ debug(repr(audios[-1]))
|
|
|
else:
|
|
|
put(audios[-1], args.copy)
|
|
|
|