diff --git a/feed b/feed index fe14aee..a0039e7 100755 --- a/feed +++ b/feed @@ -1,31 +1,31 @@ #!/usr/bin/env python3 -''' +""" Feed parser with many features from a feed, it supports filtering, subslicing, random picking Beside feeds, it supports picking files from directories -''' -import os +""" +import datetime import logging -from argparse import ArgumentParser, ArgumentTypeError -from subprocess import check_output, CalledProcessError -from collections import OrderedDict -import re -import urllib.request -from urllib.parse import urlparse, unquote +import os import posixpath import random +import re +import urllib.request +from argparse import ArgumentParser, ArgumentTypeError from bisect import bisect -import datetime +from collections import OrderedDict +from subprocess import CalledProcessError, check_output +from urllib.parse import unquote, urlparse -from lxml import html import requests +from lxml import html from pytimeparse.timeparse import timeparse def get_int(s): - return int(re.findall(r'\d+', s)[0]) + return int(re.findall(r"\d+", s)[0]) def DurationType(arg): @@ -34,27 +34,28 @@ def DurationType(arg): else: secs = timeparse(arg) if secs is None: - raise ArgumentTypeError('%r is not a valid duration' % arg) + raise ArgumentTypeError("%r is not a valid duration" % arg) return secs + def TimeDeltaType(arg): if arg.isdecimal(): secs = int(arg) else: secs = timeparse(arg) if secs is None: - raise ArgumentTypeError('%r is not a valid time range' % arg) + raise ArgumentTypeError("%r is not a valid time range" % arg) return datetime.timedelta(seconds=secs) def weighted_choice(values, weights): - ''' + """ random.choice with weights weights must be integers greater than 0. Their meaning is "relative", that is [1,2,3] is the same as [2,4,6] - ''' + """ assert len(values) == len(weights) total = 0 cum_weights = [] @@ -68,19 +69,19 @@ def weighted_choice(values, weights): def delta_humanreadable(tdelta): if tdelta is None: - return '' + return "" days = tdelta.days hours = (tdelta - datetime.timedelta(days=days)).seconds // 3600 if days: - return '{}d{}h'.format(days, hours) - return '{}h'.format(hours) + return "{}d{}h".format(days, hours) + return "{}h".format(hours) class Audio(object): def __init__(self, url, duration=None, date=None): self.url = url if duration is None: - duration = get_duration(url.encode('utf-8')) + duration = get_duration(url.encode("utf-8")) self.duration = duration self.date = date self.end_date = datetime.datetime(9999, 12, 31, tzinfo=datetime.timezone.utc) @@ -89,8 +90,9 @@ class Audio(object): return self.url def __repr__(self): - return '