new version

This commit is contained in:
Alexander Popov 2020-07-02 02:24:51 +03:00
parent f2d7ad058c
commit 4c1480be4c
3 changed files with 64 additions and 35 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
env
*.json *.json
*.pyc *.pyc
build/ build/

View File

@ -12,7 +12,7 @@
**TODO:** **TODO:**
-------- --------
- [+] web service [lfmbak](https://github.com/iiiypuk/lfmbak) - [ ] web service [see lfmbak](https://github.com/iiiypuk/lfmbak)
- [ ] more output types (sqlite, csv) - [ ] more output types (sqlite, csv)
- [ ] confirugurabled output - [ ] confirugurabled output
- [ ] continue backup - [ ] continue backup

View File

@ -1,61 +1,89 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import json import json
import urllib.request import requests
import os.path import os.path
__author__ = 'Alexander Popov' __author__ = 'Alexander Popov'
__version__ = '1.0.1' __version__ = '2.0.0'
__license__ = 'Unlicense' __license__ = 'Unlicense'
def get_pages(username, api_key): def get_pages(username, api_key, limit=200):
response = urllib.request.urlopen( """ Getting the number of pages with scrobbling data """
'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'])
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): def get_page(username, api_key, page, limit=200):
response = json.loads(urllib.request.urlopen( """ Getting scrobbling data from a page """
'http://ws.audioscrobbler.com/2.0/'
response = requests.get(
'https://ws.audioscrobbler.com/2.0/'
'?method=user.getrecenttracks&user={0}&api_key={1}&format=json' '?method=user.getrecenttracks&user={0}&api_key={1}&format=json'
'&limit=200&page={2}'.format(username, api_key, page) '&limit={2}&page={3}'.format(username, api_key, limit, page))
).read().decode("utf8"))['recenttracks']['track']
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__': if __name__ == '__main__':
CFG = dict() _ = dict()
if os.path.exists('./config.json'): if os.path.exists('./config.json'):
with open('./config.json') as f: with open('./config.json') as f:
CFG = json.loads(f.read()) _ = json.loads(f.read())
else: else:
CFG['api_key'] = input('API Key: ') _['api_key'] = input('API Key: ')
CFG['username'] = input('Username: ') _['username'] = input('Username: ')
api_key, username = _['api_key'], _['username']
PAGES = get_pages(CFG['username'], CFG['api_key']) total_pages = get_pages(username, api_key) - 603
curPage = 1 current_page = 1
tracks = [] scrobbled = []
while curPage <= PAGES:
print('\r{0}% [{1} of {2}]'.format((curPage * 100 / PAGES), curPage, PAGES), end='') while current_page <= total_pages:
response = get_scrobbles(CFG['username'], CFG['api_key'], curPage) 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: for track in response:
tracks.append({'artist': track['artist']['#text'], scrobbled.append(track)
'name': track['name'],
'album': track['album']['#text'],
'date': track['date']['uts']})
curPage += 1 current_page += 1
with open('%s.json' % (CFG['username']), 'w+', encoding='utf-8') as f: with open('%s.json' % (username), 'w+', encoding='utf-8') as f:
f.write( data = json.dumps(scrobbled, indent=4,
json.dumps(tracks, indent=4, sort_keys=True, ensure_ascii=False)) sort_keys=True, ensure_ascii=False)
f.write(data)
print('\n{0} tracks saved in {1}.json!'.format( print('\n{0} tracks saved in {1}.json!'.format(
len(tracks), CFG['username'],)) len(scrobbled), username))