add export more format

This commit is contained in:
Alexander Popov 2020-07-02 04:03:07 +03:00
parent 4c1480be4c
commit 029af27ea2
5 changed files with 64 additions and 20 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
env env
*.json *.json
*.csv
*.pyc *.pyc
build/ build/
dist/ dist/

View File

@ -1,17 +1,19 @@
![Version](https://img.shields.io/pypi/v/lastfm-backup.svg?style=flat-square) ![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=flat-square) ![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=flat-square) ![PyVersion](https://img.shields.io/pypi/pyversions/lastfm-backup.svg?style=for-the-badge)
**How to use:** ### Features:
-------------- * simple
* [Get](http://www.last.fm/api/account/create) API Key. * three formats for export data
### How to use:
* Stop scrobbling! * Stop scrobbling!
* Rename `config.json.example` to `config.json` and edit. * [Get](http://www.last.fm/api/account/create) API Key.
* Run script `lastfm_backup.py`. * Modify `config.json`.
* 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) - [ ] more output types (sqlite, csv)
- [ ] confirugurabled output - [ ] confirugurabled output

View File

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

View File

@ -1,8 +1,9 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import json import json
import requests import csv
import os.path import os.path
import requests
__author__ = 'Alexander Popov' __author__ = 'Alexander Popov'
__version__ = '2.0.0' __version__ = '2.0.0'
@ -53,6 +54,46 @@ def get_now_scrobbling(username, api_key):
return(False) return(False)
def scrobbling_export(tracks, username, export_format='is as'):
""" Save scrobbled track via various format """
if export_format == 'is as':
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']
])
with open('%s.json' % (username), 'w', encoding='utf-8') as f:
data = json.dumps(_, indent=4,
sort_keys=True, ensure_ascii=False)
f.write(data)
elif export_format == 'cvs':
_ = []
for track in tracks:
_.append([
track['artist']['#text'],
track['name'],
int(track['date']['uts'])
])
with open('%s.csv' % (username), 'w', encoding='utf-8') as f:
data = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC, delimiter=',')
for row in _:
data.writerow(row)
return(1)
if __name__ == '__main__': if __name__ == '__main__':
_ = dict() _ = dict()
@ -64,7 +105,7 @@ if __name__ == '__main__':
_['username'] = input('Username: ') _['username'] = input('Username: ')
api_key, username = _['api_key'], _['username'] api_key, username = _['api_key'], _['username']
total_pages = get_pages(username, api_key) - 603 total_pages = get_pages(username, api_key)
current_page = 1 current_page = 1
scrobbled = [] scrobbled = []
@ -80,10 +121,9 @@ if __name__ == '__main__':
current_page += 1 current_page += 1
with open('%s.json' % (username), 'w+', encoding='utf-8') as f: # if get_now_scrobbling(username, api_key):
data = json.dumps(scrobbled, indent=4, # scrobbled.pop(0)
sort_keys=True, ensure_ascii=False)
f.write(data)
print('\n{0} tracks saved in {1}.json!'.format( if scrobbling_export(scrobbled, username, _['export_format']):
len(scrobbled), username)) print('\n{0} tracks saved!'.format(
len(scrobbled), username))

View File

@ -5,7 +5,7 @@ import lastfm_backup
setuptools.setup( setuptools.setup(
name='lastfm-backup', name='lastfm-backup',
version=lastfm_backup.__version__, version=lastfm_backup.__version__,
description='Last.fm scrobbles backup', description='Last.fm scrobbling backup',
author=lastfm_backup.__author__, author=lastfm_backup.__author__,
author_email='iiiypuk@fastmail.fm', author_email='iiiypuk@fastmail.fm',
url='https://github.com/iiiypuk/lastfm-backup', url='https://github.com/iiiypuk/lastfm-backup',