new version
This commit is contained in:
parent
f2d7ad058c
commit
4c1480be4c
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
env
|
||||
*.json
|
||||
*.pyc
|
||||
build/
|
||||
|
@ -12,7 +12,7 @@
|
||||
|
||||
**TODO:**
|
||||
--------
|
||||
- [+] web service [lfmbak](https://github.com/iiiypuk/lfmbak)
|
||||
- [ ] web service [see lfmbak](https://github.com/iiiypuk/lfmbak)
|
||||
- [ ] more output types (sqlite, csv)
|
||||
- [ ] confirugurabled output
|
||||
- [ ] continue backup
|
||||
|
@ -1,61 +1,89 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import json
|
||||
import urllib.request
|
||||
import requests
|
||||
import os.path
|
||||
|
||||
__author__ = 'Alexander Popov'
|
||||
__version__ = '1.0.1'
|
||||
__version__ = '2.0.0'
|
||||
__license__ = 'Unlicense'
|
||||
|
||||
|
||||
def get_pages(username, api_key):
|
||||
response = 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")
|
||||
pages = int(json.loads(response)['recenttracks']['@attr']['totalPages'])
|
||||
def get_pages(username, api_key, limit=200):
|
||||
""" Getting the number of pages with scrobbling data """
|
||||
|
||||
return(pages)
|
||||
response = requests.get(
|
||||
'https://ws.audioscrobbler.com/2.0/'
|
||||
'?method=user.getrecenttracks&user='
|
||||
'{0}&api_key={1}&format=json&limit={2}'
|
||||
.format(username, api_key, limit))
|
||||
|
||||
data = json.loads(response.content.decode("utf8"))
|
||||
|
||||
return(int(data['recenttracks']['@attr']['totalPages']))
|
||||
|
||||
|
||||
def get_scrobbles(username, api_key, page):
|
||||
response = json.loads(urllib.request.urlopen(
|
||||
'http://ws.audioscrobbler.com/2.0/'
|
||||
def get_page(username, api_key, page, limit=200):
|
||||
""" Getting scrobbling data from a page """
|
||||
|
||||
response = requests.get(
|
||||
'https://ws.audioscrobbler.com/2.0/'
|
||||
'?method=user.getrecenttracks&user={0}&api_key={1}&format=json'
|
||||
'&limit=200&page={2}'.format(username, api_key, page)
|
||||
).read().decode("utf8"))['recenttracks']['track']
|
||||
'&limit={2}&page={3}'.format(username, api_key, limit, page))
|
||||
|
||||
data = json.loads(response.content.decode("utf8"))
|
||||
|
||||
return(data['recenttracks']['track'])
|
||||
|
||||
|
||||
def get_now_scrobbling(username, api_key):
|
||||
""" Getting now scrobbling track """
|
||||
|
||||
response = requests.get(
|
||||
'https://ws.audioscrobbler.com/2.0/'
|
||||
'?method=user.getrecenttracks&user={0}'
|
||||
'&api_key={1}&format=json&limit=1'
|
||||
.format(username, api_key))
|
||||
|
||||
data = json.loads(response.content.decode('utf-8'))
|
||||
|
||||
if '@attr' in data['recenttracks']['track'][0]:
|
||||
return(True)
|
||||
else:
|
||||
return(False)
|
||||
|
||||
return(response)
|
||||
|
||||
if __name__ == '__main__':
|
||||
CFG = dict()
|
||||
_ = dict()
|
||||
|
||||
if os.path.exists('./config.json'):
|
||||
with open('./config.json') as f:
|
||||
CFG = json.loads(f.read())
|
||||
_ = json.loads(f.read())
|
||||
else:
|
||||
CFG['api_key'] = input('API Key: ')
|
||||
CFG['username'] = input('Username: ')
|
||||
_['api_key'] = input('API Key: ')
|
||||
_['username'] = input('Username: ')
|
||||
api_key, username = _['api_key'], _['username']
|
||||
|
||||
PAGES = get_pages(CFG['username'], CFG['api_key'])
|
||||
curPage = 1
|
||||
tracks = []
|
||||
while curPage <= PAGES:
|
||||
print('\r{0}% [{1} of {2}]'.format((curPage * 100 / PAGES), curPage, PAGES), end='')
|
||||
response = get_scrobbles(CFG['username'], CFG['api_key'], curPage)
|
||||
total_pages = get_pages(username, api_key) - 603
|
||||
current_page = 1
|
||||
scrobbled = []
|
||||
|
||||
while current_page <= total_pages:
|
||||
print('\r{0:.2f}% [{1} of {2}]'
|
||||
.format((current_page * 100 / total_pages),
|
||||
current_page, total_pages), end='')
|
||||
|
||||
response = get_page(username, api_key, current_page)
|
||||
|
||||
for track in response:
|
||||
tracks.append({'artist': track['artist']['#text'],
|
||||
'name': track['name'],
|
||||
'album': track['album']['#text'],
|
||||
'date': track['date']['uts']})
|
||||
scrobbled.append(track)
|
||||
|
||||
curPage += 1
|
||||
current_page += 1
|
||||
|
||||
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))
|
||||
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)
|
||||
|
||||
print('\n{0} tracks saved in {1}.json!'.format(
|
||||
len(tracks), CFG['username'],))
|
||||
len(scrobbled), username))
|
||||
|
Loading…
Reference in New Issue
Block a user