From 029af27ea21a0d7dad184c48dfa02db3b3ca2540 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Thu, 2 Jul 2020 04:03:07 +0300 Subject: [PATCH] add export more format --- .gitignore | 1 + README.md | 22 ++++++++++-------- config.json.example | 3 ++- lastfm_backup.py | 56 ++++++++++++++++++++++++++++++++++++++------- setup.py | 2 +- 5 files changed, 64 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index eceadef..b29eec4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ env *.json +*.csv *.pyc build/ dist/ diff --git a/README.md b/README.md index 44f50d8..f7df047 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/config.json.example b/config.json.example index c5c7d86..3a25b32 100644 --- a/config.json.example +++ b/config.json.example @@ -1,4 +1,5 @@ { "username" : "", - "api_key" : "" + "api_key" : "", + "export_format": "is as, simple, csv" } diff --git a/lastfm_backup.py b/lastfm_backup.py index f80c348..2d229e0 100755 --- a/lastfm_backup.py +++ b/lastfm_backup.py @@ -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)) diff --git a/setup.py b/setup.py index ee605a4..a85c27a 100644 --- a/setup.py +++ b/setup.py @@ -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',