Merge pull request #3 from Nitemice/fix/export-formats

Fix/export formats
This commit is contained in:
Alexander Popov 2020-10-04 13:25:09 +03:00 committed by GitHub
commit b63e1f1536
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 21 deletions

View File

@ -1,21 +1,26 @@
# Last.fm Backup
![Version](https://img.shields.io/pypi/v/lastfm-backup.svg?style=for-the-badge) ![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) ![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) ![PyVersion](https://img.shields.io/pypi/pyversions/lastfm-backup.svg?style=for-the-badge)
### Features: ### Features:
* simple * Simple
* three formats for export data * 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: ### How to use:
* Stop scrobbling! * Stop scrobbling!
* [Get](http://www.last.fm/api/account/create) API Key.
* Modify `config.json`. * Modify `config.json`.
* [Get API Key](http://www.last.fm/api/account/create).
* Run `lastfm_backup.py`. * Run `lastfm_backup.py`.
* WAIT =) * WAIT =)
### TODO: ### TODO:
- [ ] web service [see lfmbak](https://github.com/iiiypuk/lfmbak) - [ ] web service [see lfmbak](https://github.com/iiiypuk/lfmbak)
- [ ] more output types (sqlite, csv) - [x] more output types (sqlite, csv)
- [ ] confirugurabled output - [ ] configurable output
- [ ] continue backup - [ ] continue backup
- [ ] multithreading - [ ] multi-threading

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

@ -6,7 +6,7 @@ import os.path
import requests import requests
__author__ = 'Alexander Popov' __author__ = 'Alexander Popov'
__version__ = '2.0.0' __version__ = '2.1.0'
__license__ = 'Unlicense' __license__ = 'Unlicense'
@ -54,42 +54,46 @@ 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)
f.write(data) f.write(data)
elif export_format == 'simple': elif export_format == 'simple':
_ = {} _ = []
for track in tracks: for track in tracks:
_.append([ _.append({
track['artist']['#text'], track['name'], track['date']['uts'] '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: 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 == 'cvs': elif export_format == 'csv':
_ = [] _ = []
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') 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'
) )