This commit is contained in:
Alexander Popov 2017-04-23 20:35:19 +03:00
parent 016cd671ee
commit ac05aad045
3 changed files with 54 additions and 47 deletions

View File

@ -1,19 +1,22 @@
![Version](https://img.shields.io/pypi/v/lastfm_backup.svg?style=flat-square) ![Version](https://img.shields.io/pypi/v/lastfm-backup.svg?style=flat-square)
![License](https://img.shields.io/pypi/l/lastfm_backup.svg.svg?style=flat-square) ![License](https://img.shields.io/pypi/l/lastfm-backup.svg.svg?style=flat-square)
![PyVersion](https://img.shields.io/pypi/pyversions/lastfm_backup.svg.svg?style=flat-square) ![PyVersion](https://img.shields.io/pypi/pyversions/lastfm-backup.svg.svg?style=flat-square)
**How to use as standartalone app** **How to use:**
1. Rename `config.json.example` to `config.json` and edit. --------------
2. Run this `python lastfm_backup.py` or `~/lastfm_backup.py`. * [Get](http://www.last.fm/api/account/create) API Key.
3. PROFIT!! * Stop scrobbling!
* Rename `config.json.example` to `config.json` and edit.
* Run script `lastfm_backup.py`.
* WAIT!!
**How to use as library** My `>72,2k` tracks scrobbles sized `~9.9M`.
see [getTracks.py](https://github.com/iiiypuk/lastfm-backup/blob/master/samples/getTracks.py). Tar.xz sized `5.4K`.
My `>57,5k` tracks scrobbles sized `~8.2Mb`. **TODO:**
--------
**TODO** - [ ] web service
`-` web service - [ ] more output types (sqlite, csv)
`-` more output types (sqlite, csv) - [ ] confirugurabled output
`-` confirugurabled output - [ ] continue backup
`-` continue backup - [ ] multithread

53
lastfm_backup.py Normal file → Executable file
View File

@ -2,53 +2,60 @@
import json import json
import urllib.request import urllib.request
import os.path
__author__ = 'Alexander Popov' __author__ = 'Alexander Popov'
__version__ = '0.1.1' __version__ = '1.0.0'
__license__ = 'Unlicense' __license__ = 'Unlicense'
def get_pages(username, api_key): def get_pages(username, api_key):
response = urllib.request.urlopen( response = urllib.request.urlopen(
'http://ws.audioscrobbler.com/2.0/' 'http://ws.audioscrobbler.com/2.0/'
'?method=user.getrecenttracks&user=%s&api_key=%s&format=json' % '?method=user.getrecenttracks&user={0}&api_key={1}&format=json'
(username, api_key,)).read().decode("utf8") '&limit=200'.format(username, api_key)).read().decode("utf8")
pages = int(json.loads(response)['recenttracks']['@attr']['totalPages']) pages = int(json.loads(response)['recenttracks']['@attr']['totalPages'])
return(pages) return(pages)
def get_scrobbles(username, api_key, page): def get_scrobbles(username, api_key, page):
response = json.loads( response = json.loads(urllib.request.urlopen(
urllib.request.urlopen( 'http://ws.audioscrobbler.com/2.0/'
'http://ws.audioscrobbler.com/2.0/' '?method=user.getrecenttracks&user={0}&api_key={1}&format=json'
'?method=user.getrecenttracks&user=%s' '&limit=200'.format(username, api_key)
'&api_key=%s&page=%d&format=json' % ).read().decode("utf8"))['recenttracks']['track']
(username, api_key, page,))
.read().decode("utf8"))['recenttracks']['track']
return(response) return(response)
if __name__ == '__main__': if __name__ == '__main__':
with open('./config.json') as f: CFG = dict()
CONFIG = json.loads(f.read())
PAGES = get_pages(CONFIG['username'], CONFIG['api_key']) if os.path.exists('./config.json'):
COUNT = 1 with open('./config.json') as f:
TRACKS = [] CFG = json.loads(f.read())
while COUNT <= PAGES: else:
print('\r%d%%' % (COUNT * 100 / PAGES), end='') CFG['api_key'] = input('API Key: ')
response = get_scrobbles(CONFIG['username'], CONFIG['api_key'], COUNT) CFG['username'] = input('Username: ')
PAGES = get_pages(CFG['username'], CFG['api_key'])
curPage = 1
tracks = []
while curPage <= PAGES:
print('\r%d%%' % (curPage * 100 / PAGES), end='')
response = get_scrobbles(CFG['username'], CFG['api_key'], curPage)
for track in response: for track in response:
TRACKS.append({'artist': track['artist']['#text'], tracks.append({'artist': track['artist']['#text'],
'name': track['name'], 'name': track['name'],
'album': track['album']['#text'], 'album': track['album']['#text'],
'date': track['date']['uts']}) 'date': track['date']['uts']})
COUNT += 1 curPage += 1
with open('%s.json' % (CONFIG['username']), 'w+', encoding='utf-8') as f: with open('%s.json' % (CFG['username']), 'w+', encoding='utf-8') as f:
f.write( f.write(
json.dumps(TRACKS, indent=4, sort_keys=True, ensure_ascii=False)) json.dumps(tracks, indent=4, sort_keys=True, ensure_ascii=False))
print('\r%d tracks saved in %s.json!' % (len(TRACKS), CONFIG['username'],))
print('\r{0} tracks saved in {1}.json!'.format(
len(tracks), CFG['username'],))

View File

@ -3,24 +3,21 @@ from distutils.core import setup
import lastfm_backup import lastfm_backup
setup( setup(
name='lastfm_backup', name='lastfm-backup',
version=lastfm_backup.__version__, version=lastfm_backup.__version__,
description='Last.fm backup scrobbles library' description='Last.fm scrobbles backup',
' and standartalone program.',
author=lastfm_backup.__author__, author=lastfm_backup.__author__,
author_email='iiiypuk@fastmail.fm', author_email='iiiypuk@fastmail.fm',
url='https://github.com/iiiypuk/lastfm-import', url='https://github.com/iiiypuk/lastfm-backup',
py_modules=['lastfm_backup'], py_modules=['lastfm_backup'],
scripts=['lastfm_backup.py'], scripts=['lastfm_backup.py'],
license=lastfm_backup.__license__, license=lastfm_backup.__license__,
platforms='any', platforms='any',
keywords=['last.fm', 'lastfm', 'import'], keywords=['last.fm', 'lastfm', 'backup'],
classifiers=['License :: Public Domain', classifiers=['License :: Public Domain',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.2', 'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3', 'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.5'],
],
) )