-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrhythmbox.py
More file actions
125 lines (101 loc) · 5.24 KB
/
rhythmbox.py
File metadata and controls
125 lines (101 loc) · 5.24 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import argparse
import sys
import xmltodict
def list_entries(entries: object, TYPE_TO_MATCH: str = 'ignore', yaml_format=False, verbose=0):
""" List Internet Radio entries
:type entries: object - list of entries
:param TYPE_TO_MATCH: entry type to match ('iradio', 'song', 'ignore')
:param yaml_format - YAML rather than JSON
:param verbose: messages
"""
line_count: int = 0
try:
for entry in entries:
if entry['@type'] == TYPE_TO_MATCH:
line_count += 1
if verbose > 0:
if TYPE_TO_MATCH == 'ignore':
location_entry = {'@type': entry['@type'], 'location': entry['location']}
print("{:03d}: {}".format(line_count, location_entry))
else:
print("{:03d}: {}".format(line_count, entry))
if verbose == 0:
print("{1:6s}: {0:03d}".format(line_count, TYPE_TO_MATCH))
except KeyError as entry_key_error:
print("Missing key {0}, in, '{1}'".format(entry_key_error, args.infd.name))
return None
def list_all_entries(entries: object, KNOWN_ENTRIES=('iradio', 'song', 'ignore'), known_type=False, verbose=0):
""" List Internet Radio entries
:type entries: object - list of entries
:param KNOWN_ENTRIES: known entry types ('iradio', 'song', 'ignore')
:param known_type: entry type in TYPE_TO_MATCH
:param verbose: messages
"""
line_count: int = 0
try:
for entry in entries:
if known_type and entry['@type'] in KNOWN_ENTRIES:
line_count += 1
if verbose > 0:
print("{:03d}: {}".format(line_count, entry))
elif entry['@type'] not in KNOWN_ENTRIES:
line_count += 1
if verbose > 0:
print("{:03d}: {}".format(line_count, entry))
if verbose == 0:
if known_type and entry['@type'] in KNOWN_ENTRIES:
print("{1:6s}: {0:03d}".format(line_count, 'known'))
else:
print("{1:6s}: {0:03d}".format(line_count, 'unknown'))
except KeyError as entry_key_error:
print("Missing key {0}, in, '{1}'".format(entry_key_error, args.infd.name))
return None
# ==============================================================================
# Python Library: https://docs.python.org/dev/library/argparse.html
# Nargs usage: https://docs.python.org/dev/library/argparse.html#nargs
# Tutorial: https://docs.python.org/dev/howto/argparse.html
# PEP 8 – Style Guide for Python Code: https://peps.python.org/pep-0008/
# PyFormat Using % and .format() for great good!: https://pyformat.info/
# Search Python Standard Library: https://docs.python.org/3/library/index.html
# Python Basics: https://docs.python-guide.org/scenarios/xml/
# The ElementTree XML API: https://docs.python.org/3/library/xml.etree.elementtree.html
# Dictionary into a valid XML string: https://pypi.org/project/dicttoxml/
# Logging Best Practices: https://www.loggly.com/use-cases/6-python-logging-best-practices-you-should-be-aware-of/
# ==============================================================================
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
arguments = None
parser = argparse.ArgumentParser(description='Simple RhythmBox XML File Parser')
parser.add_argument('-r', '--radio', action='store_true', help='extract radio entries')
parser.add_argument('-s', '--song', action='store_true', help='extract song entries')
parser.add_argument('-i', '--ignore', action='store_true', help='extract ignore entries')
parser.add_argument('-u', '--unknown', action='store_true', help='extract unknown entries')
parser.add_argument('-v', '--verbose', action='count', default=0)
parser.add_argument('infd', nargs='?', type=argparse.FileType('r'), default=sys.stdin)
# parser.add_argument('outfd', nargs='?', type=argparse.FileType('w'), default=sys.stdout)
args = parser.parse_args()
if args.verbose >= 1:
print("args: {0}".format(args.__str__()))
# Note equivalent of: infd = open('filename.txt', 'r')
# Done by: add_argument('infd', nargs='?', type=argparse.FileType('r'), default=sys.stdin)
try:
data = xmltodict.parse(args.infd.read())
entries = data['rhythmdb']['entry']
known_entries = ('iradio', 'song', 'ignore')
if args.radio:
list_entries(entries=entries, TYPE_TO_MATCH='iradio', yaml_format=False, verbose=args.verbose)
elif args.song:
list_entries(entries=entries, TYPE_TO_MATCH='song', yaml_format=False, verbose=args.verbose)
elif args.ignore:
list_entries(entries=entries, TYPE_TO_MATCH='ignore', yaml_format=True, verbose=args.verbose)
elif args.unknown:
list_all_entries(entries=entries, KNOWN_ENTRIES=known_entries, known_type=False, verbose=args.verbose)
else:
list_all_entries(entries=entries, KNOWN_ENTRIES=known_entries, known_type=True, verbose=args.verbose)
sys.exit(0)
except FileNotFoundError as file_not_found_error:
print("{0}".format(file_not_found_error))
sys.exit(1)
except Exception as error:
print('{0}'.format(error))
sys.exit(1)