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
*.json
*.csv
*.pyc
build/
dist/

View File

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

View File

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

View File

@ -1,8 +1,9 @@
#!/usr/bin/env python3
import json
import requests
import csv
import os.path
import requests
__author__ = 'Alexander Popov'
__version__ = '2.0.0'
@ -53,6 +54,46 @@ def get_now_scrobbling(username, api_key):
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__':
_ = dict()
@ -64,7 +105,7 @@ if __name__ == '__main__':
_['username'] = input('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
scrobbled = []
@ -80,10 +121,9 @@ if __name__ == '__main__':
current_page += 1
with open('%s.json' % (username), 'w+', encoding='utf-8') as f:
data = json.dumps(scrobbled, indent=4,
sort_keys=True, ensure_ascii=False)
f.write(data)
# if get_now_scrobbling(username, api_key):
# scrobbled.pop(0)
print('\n{0} tracks saved in {1}.json!'.format(
len(scrobbled), username))
if scrobbling_export(scrobbled, username, _['export_format']):
print('\n{0} tracks saved!'.format(
len(scrobbled), username))

View File

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