This commit is contained in:
Alexander Popov 2016-10-19 14:04:20 +03:00
parent 00fc65ced2
commit 9685d8aa8a
7 changed files with 75 additions and 15 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
*.json *.json
*.pyc

View File

@ -1,9 +1,19 @@
**How to use** ![Version](https://img.shields.io/pypi/v/lastfm_import.svg?style=flat-square)
![License](https://img.shields.io/pypi/l/lastfm_import.svg.svg?style=flat-square)
![PyVersion](https://img.shields.io/pypi/pyversions/lastfm_import.svg.svg?style=flat-square)
**How to use as standartalone app**
1. Rename `config.json.example` to `config.json` and edit. 1. Rename `config.json.example` to `config.json` and edit.
2. Run this `python lastfm-import.py` or `~/lastfm-import.py`. 2. Run this `python lastfm_import.py` or `~/lastfm_import.py`.
3. PROFIT!! 3. PROFIT!!
***How to use as library**
see [getTracks.py](https://github.com/iiiypuk/lastfm-import/blob/master/samples/getTracks.py).
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

View File

@ -4,12 +4,9 @@ import json
import urllib.request import urllib.request
__author__ = 'Alexander Popov' __author__ = 'Alexander Popov'
__version__ = (0, 1, 0,) __version__ = '0.1.1'
__license__ = 'Unlicense' __license__ = 'Unlicense'
with open('./config.json') as f:
CONFIG = json.loads(f.read())
def get_pages(username, api_key): def get_pages(username, api_key):
response = urllib.request.urlopen( response = urllib.request.urlopen(
@ -20,19 +17,28 @@ def get_pages(username, api_key):
return(pages) 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']
return(response)
if __name__ == '__main__': if __name__ == '__main__':
with open('./config.json') as f:
CONFIG = json.loads(f.read())
PAGES = get_pages(CONFIG['username'], CONFIG['api_key']) PAGES = get_pages(CONFIG['username'], CONFIG['api_key'])
COUNT = 1 COUNT = 1
TRACKS = [] TRACKS = []
while COUNT <= PAGES: while COUNT <= PAGES:
print('\r%d%%' % (COUNT * 100 / PAGES), end='') print('\r%d%%' % (COUNT * 100 / PAGES), end='')
response = json.loads( response = get_scrobbles(CONFIG['username'], CONFIG['api_key'], COUNT)
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: for track in response:
TRACKS.append({'artist': track['artist']['#text'], TRACKS.append({'artist': track['artist']['#text'],

15
samples/getTracks.py Normal file
View File

@ -0,0 +1,15 @@
#!/usr/bin/env python3
import lastfm_import as lfm
API_KEY = '0e5070361556658180f9b1518b341eda'
USERNAME = 'goodgame'
if __name__ == '__main__':
pages = lfm.get_pages(USERNAME, API_KEY)
for page in range(1, pages + 1):
tracks = lfm.get_scrobbles(USERNAME, API_KEY, page)
for track in tracks:
print('%s - %s' % (track['artist']['#text'], track['name'],))

2
setup.cfg Normal file
View File

@ -0,0 +1,2 @@
[metadata]
description-file=README.md

26
setup.py Normal file
View File

@ -0,0 +1,26 @@
from distutils.core import setup
import lastfm_import
setup(
name='lastfm_import',
version=lastfm_import.__version__,
description='Last.fm backup scrobbles library'
' and standartalone program.',
author=lastfm_import.__author__,
author_email='iiiypuk@fastmail.fm',
url='https://github.com/iiiypuk/lastfm-import',
py_modules=['lastfm_import'],
scripts=['lastfm_import.py'],
license=lastfm_import.__license__,
platforms='any',
keywords=['last.fm', 'lastfm', 'import'],
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',
],
)