Last active
December 17, 2015 11:59
-
-
Save shpikat/5606954 to your computer and use it in GitHub Desktop.
Radio playlist bitrate checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
import getopt | |
from pprint import pprint | |
import sys | |
import urllib.request | |
import urllib.error | |
def usage(filename): | |
print("Usage:", filename, "<m3u playlist file> [m3u playlist files...]") | |
print("\t-h, --help\tprints this help") | |
def main(argv=None): | |
if argv is None: | |
argv = sys.argv | |
try: | |
opts, args = getopt.getopt(argv[1:], "h", ["help"]) | |
except getopt.error as message: | |
print(message) | |
usage(argv[0]) | |
return 2 | |
for o, a in opts: | |
if o in ("-h", "--help"): | |
usage(argv[0]) | |
return 0 | |
if not args: | |
usage(argv[0]) | |
return 2 | |
radios = defaultdict(lambda: defaultdict(list)) | |
for filename in args: | |
try: | |
for line in open(filename, 'r', encoding='iso-8859-1'): | |
if not line.isspace() and not line.startswith('#'): | |
try: | |
with urllib.request.urlopen(line) as stream: | |
info = stream.info() | |
radios[info["icy-genre"]][info["icy-br"]].append(line.strip()) | |
except urllib.error.HTTPError as error: | |
print("Error opening", line.strip(), ":", error.reason) | |
except FileNotFoundError: | |
print("File", filename, "not found") | |
pprint(radios) | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment