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

53
lastfm_backup.py Normal file → Executable file
View File

@ -2,53 +2,60 @@
import json
import urllib.request
import os.path
__author__ = 'Alexander Popov'
__version__ = '0.1.1'
__version__ = '1.0.0'
__license__ = 'Unlicense'
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")
'?method=user.getrecenttracks&user={0}&api_key={1}&format=json'
'&limit=200'.format(username, api_key)).read().decode("utf8")
pages = int(json.loads(response)['recenttracks']['@attr']['totalPages'])
return(pages)
def get_scrobbles(username, api_key, page):
response = json.loads(
urllib.request.urlopen(
'http://ws.audioscrobbler.com/2.0/'
'?method=user.getrecenttracks&user=%s'
'&api_key=%s&page=%d&format=json' %
(username, api_key, page,))
.read().decode("utf8"))['recenttracks']['track']
response = json.loads(urllib.request.urlopen(
'http://ws.audioscrobbler.com/2.0/'
'?method=user.getrecenttracks&user={0}&api_key={1}&format=json'
'&limit=200'.format(username, api_key)
).read().decode("utf8"))['recenttracks']['track']
return(response)
if __name__ == '__main__':
with open('./config.json') as f:
CONFIG = json.loads(f.read())
CFG = dict()
PAGES = get_pages(CONFIG['username'], CONFIG['api_key'])
COUNT = 1
TRACKS = []
while COUNT <= PAGES:
print('\r%d%%' % (COUNT * 100 / PAGES), end='')
response = get_scrobbles(CONFIG['username'], CONFIG['api_key'], COUNT)
if os.path.exists('./config.json'):
with open('./config.json') as f:
CFG = json.loads(f.read())
else:
CFG['api_key'] = input('API Key: ')
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:
TRACKS.append({'artist': track['artist']['#text'],
tracks.append({'artist': track['artist']['#text'],
'name': track['name'],
'album': track['album']['#text'],
'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(
json.dumps(TRACKS, indent=4, sort_keys=True, ensure_ascii=False))
print('\r%d tracks saved in %s.json!' % (len(TRACKS), CONFIG['username'],))
json.dumps(tracks, indent=4, sort_keys=True, ensure_ascii=False))
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
setup(
name='lastfm_backup',
name='lastfm-backup',
version=lastfm_backup.__version__,
description='Last.fm backup scrobbles library'
' and standartalone program.',
description='Last.fm scrobbles backup',
author=lastfm_backup.__author__,
author_email='iiiypuk@fastmail.fm',
url='https://github.com/iiiypuk/lastfm-import',
url='https://github.com/iiiypuk/lastfm-backup',
py_modules=['lastfm_backup'],
scripts=['lastfm_backup.py'],
license=lastfm_backup.__license__,
platforms='any',
keywords=['last.fm', 'lastfm', 'import'],
keywords=['last.fm', 'lastfm', 'backup'],
classifiers=['License :: Public Domain',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
],
'Programming Language :: Python :: 3.5'],
)