commit 00fc65ced2587f8ec6c397fd048ef86605ef55cd Author: Alexander Popov Date: Tue Oct 18 05:27:59 2016 +0300 first version diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a6c57f5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.json diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..cf1ab25 --- /dev/null +++ b/LICENSE @@ -0,0 +1,24 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to diff --git a/README.md b/README.md new file mode 100644 index 0000000..a40b7b8 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +**How to use** +1. Rename `config.json.example` to `config.json` and edit. +2. Run this `python lastfm-import.py` or `~/lastfm-import.py`. +3. PROFIT!! + +**TODO** +`-` web service +`-` more output types (sqlite, csv) +`-` confirugurabled output diff --git a/config.json.example b/config.json.example new file mode 100644 index 0000000..c5c7d86 --- /dev/null +++ b/config.json.example @@ -0,0 +1,4 @@ +{ + "username" : "", + "api_key" : "" +} diff --git a/lastfm-import.py b/lastfm-import.py new file mode 100644 index 0000000..fbeb3b6 --- /dev/null +++ b/lastfm-import.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 + +import json +import urllib.request + +__author__ = 'Alexander Popov' +__version__ = (0, 1, 0,) +__license__ = 'Unlicense' + +with open('./config.json') as f: + CONFIG = json.loads(f.read()) + + +def get_pages(username, api_key): + response = urllib.request.urlopen( + 'http://ws.audioscrobbler.com/2.0/' + '?method=user.getrecenttracks&user=%s&api_key=%s&format=json' % + (username, api_key,)).read().decode("utf8") + pages = int(json.loads(response)['recenttracks']['@attr']['totalPages']) + + return(pages) + +if __name__ == '__main__': + PAGES = get_pages(CONFIG['username'], CONFIG['api_key']) + COUNT = 1 + TRACKS = [] + while COUNT <= PAGES: + print('\r%d%%' % (COUNT * 100 / PAGES), end='') + response = json.loads( + urllib.request.urlopen( + 'http://ws.audioscrobbler.com/2.0/' + '?method=user.getrecenttracks&user=%s' + '&api_key=%s&page=%d&format=json' % + (CONFIG['username'], CONFIG['api_key'], COUNT,)) + .read().decode("utf8"))['recenttracks']['track'] + + for track in response: + TRACKS.append({'artist': track['artist']['#text'], + 'name': track['name'], + 'album': track['album']['#text'], + 'date': track['date']['uts']}) + + COUNT += 1 + + with open('%s.json' % (CONFIG['username']), 'w+', encoding='utf-8') as f: + f.write( + json.dumps(TRACKS, indent=4, sort_keys=True, ensure_ascii=False)) + print('\r%d tracks saved in %s.json!' % (len(TRACKS), CONFIG['username'],))