Minor export improvements

Added album field to CSV.
Write all records to CSV at once.
Renamed "is as" export mode to "dump". It's more accurate and nicer with
a single word.
This commit is contained in:
Nitemice 2020-10-04 17:59:57 +11:00
parent a0a84a212f
commit abbf9df872
3 changed files with 16 additions and 18 deletions

View File

@ -1,5 +1,5 @@
{ {
"username" : "", "username" : "",
"api_key" : "", "api_key" : "",
"export_format": "is as, simple, csv" "export_format": "dump, simple, csv"
} }

View File

@ -54,10 +54,10 @@ def get_now_scrobbling(username, api_key):
return(False) return(False)
def scrobbling_export(tracks, username, export_format='is as'): def scrobbling_export(tracks, username, export_format='dump'):
""" Save scrobbled track via various format """ """ Save scrobbled track via various format """
if export_format == 'is as': if export_format == 'dump':
with open('%s.json' % (username), 'w', encoding='utf-8') as f: with open('%s.json' % (username), 'w', encoding='utf-8') as f:
data = json.dumps(tracks, indent=4, data = json.dumps(tracks, indent=4,
sort_keys=True, ensure_ascii=False) sort_keys=True, ensure_ascii=False)
@ -67,35 +67,33 @@ def scrobbling_export(tracks, username, export_format='is as'):
_ = [] _ = []
for track in tracks: for track in tracks:
_.append({'artist': track['artist']['#text'], _.append({
'name': track['name'], 'artist': track['artist']['#text'],
'album': track['album']['#text'], 'name': track['name'],
'date': track['date']['uts']}) 'album': track['album']['#text'],
'date': int(track['date']['uts'])
})
with open('%s.json' % (username), 'w', encoding='utf-8') as f: with open('%s.json' % (username), 'w', encoding='utf-8') as f:
data = json.dumps(_, indent=4, data = json.dumps(_, indent=4, sort_keys=True, ensure_ascii=False)
sort_keys=True, ensure_ascii=False)
f.write(data) f.write(data)
elif export_format == 'csv': elif export_format == 'csv':
_ = [] _ = []
_.append([
'artist',
'track',
'date'
])
for track in tracks: for track in tracks:
_.append([ _.append([
track['artist']['#text'], track['artist']['#text'],
track['name'], track['name'],
track['album']['#text'],
int(track['date']['uts']) int(track['date']['uts'])
]) ])
with open('%s.csv' % (username), 'w', encoding='utf-8', newline='') as f: with open('%s.csv' % (username), 'w', encoding='utf-8',
newline='') as f:
data = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC, delimiter=',') data = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC, delimiter=',')
for row in _: data.writerow(['artist', 'track', 'album', 'date'])
data.writerow(row) data.writerows(_)
return(1) return(1)

View File

@ -17,5 +17,5 @@ setuptools.setup(
classifiers=['License :: Public Domain', classifiers=['License :: Public Domain',
'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3',
'Operating System :: OS Independent'], 'Operating System :: OS Independent'],
python_requires='>=3.0' python_requires='>=3.2'
) )