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)
![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

View File

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

View File

@ -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)

View File

@ -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'
)