diff --git a/README.md b/README.md index f7df047..471ce8a 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,26 @@ +# Last.fm Backup + ![Version](https://img.shields.io/pypi/v/lastfm-backup.svg?style=for-the-badge) ![License](https://img.shields.io/pypi/l/lastfm-backup.svg?style=for-the-badge) ![PyVersion](https://img.shields.io/pypi/pyversions/lastfm-backup.svg?style=for-the-badge) ### Features: -* simple -* three formats for export data +* Simple +* Three formats for export data: + 1. `dump` - JSON, direct from API + 2. `simple` - Artist, track name, album & date as JSON + 3. `csv` - Artist, track name, album & date as comma separated values ### How to use: * Stop scrobbling! -* [Get](http://www.last.fm/api/account/create) API Key. * Modify `config.json`. +* [Get API Key](http://www.last.fm/api/account/create). * Run `lastfm_backup.py`. * WAIT =) ### TODO: - [ ] web service [see lfmbak](https://github.com/iiiypuk/lfmbak) -- [ ] more output types (sqlite, csv) -- [ ] confirugurabled output +- [x] more output types (sqlite, csv) +- [ ] configurable output - [ ] continue backup -- [ ] multithreading +- [ ] multi-threading diff --git a/config.json.example b/config.json.example index 3a25b32..c00263e 100644 --- a/config.json.example +++ b/config.json.example @@ -1,5 +1,5 @@ { "username" : "", "api_key" : "", - "export_format": "is as, simple, csv" + "export_format": "dump, simple, csv" } diff --git a/lastfm_backup.py b/lastfm_backup.py index 2d229e0..8da87f5 100755 --- a/lastfm_backup.py +++ b/lastfm_backup.py @@ -6,7 +6,7 @@ import os.path import requests __author__ = 'Alexander Popov' -__version__ = '2.0.0' +__version__ = '2.1.0' __license__ = 'Unlicense' @@ -54,42 +54,46 @@ def get_now_scrobbling(username, api_key): 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 """ - if export_format == 'is as': + if export_format == 'dump': with open('%s.json' % (username), 'w', encoding='utf-8') as f: data = json.dumps(tracks, indent=4, sort_keys=True, ensure_ascii=False) f.write(data) elif export_format == 'simple': - _ = {} + _ = [] for track in tracks: - _.append([ - track['artist']['#text'], track['name'], track['date']['uts'] - ]) + _.append({ + 'artist': track['artist']['#text'], + 'name': track['name'], + 'album': track['album']['#text'], + 'date': int(track['date']['uts']) + }) with open('%s.json' % (username), 'w', encoding='utf-8') as f: - data = json.dumps(_, indent=4, - sort_keys=True, ensure_ascii=False) + data = json.dumps(_, indent=4, sort_keys=True, ensure_ascii=False) f.write(data) - elif export_format == 'cvs': + elif export_format == 'csv': _ = [] for track in tracks: _.append([ track['artist']['#text'], track['name'], + track['album']['#text'], int(track['date']['uts']) ]) - with open('%s.csv' % (username), 'w', encoding='utf-8') as f: + with open('%s.csv' % (username), 'w', encoding='utf-8', + newline='') as f: data = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC, delimiter=',') - for row in _: - data.writerow(row) + data.writerow(['artist', 'track', 'album', 'date']) + data.writerows(_) return(1) diff --git a/setup.py b/setup.py index a85c27a..3ccf0c4 100644 --- a/setup.py +++ b/setup.py @@ -17,5 +17,5 @@ setuptools.setup( classifiers=['License :: Public Domain', 'Programming Language :: Python :: 3', 'Operating System :: OS Independent'], - python_requires='>=3.0' + python_requires='>=3.2' )