lastfm-backup/lastfm_backup.py

139 lines
3.8 KiB
Python
Raw Permalink Normal View History

2016-10-18 05:27:59 +03:00
#!/usr/bin/env python3
import json
2020-07-02 04:03:07 +03:00
import csv
2017-04-23 20:35:19 +03:00
import os.path
2020-07-02 04:03:07 +03:00
import requests
2016-10-18 05:27:59 +03:00
2020-10-05 02:32:15 +03:00
__author__ = "Alexander Popov"
__version__ = "2.1.0"
__license__ = "Unlicense"
2016-10-18 05:27:59 +03:00
2020-07-02 02:24:51 +03:00
def get_pages(username, api_key, limit=200):
""" Getting the number of pages with scrobbling data """
2016-10-18 05:27:59 +03:00
2020-07-02 02:24:51 +03:00
response = requests.get(
2020-10-05 02:32:15 +03:00
"https://ws.audioscrobbler.com/2.0/"
"?method=user.getrecenttracks&user="
"{0}&api_key={1}&format=json&limit={2}".format(username, api_key, limit)
)
2016-10-18 05:27:59 +03:00
2020-07-02 02:24:51 +03:00
data = json.loads(response.content.decode("utf8"))
2016-10-19 14:04:20 +03:00
2020-10-05 02:32:15 +03:00
return int(data["recenttracks"]["@attr"]["totalPages"])
2020-07-02 02:24:51 +03:00
def get_page(username, api_key, page, limit=200):
""" Getting scrobbling data from a page """
response = requests.get(
2020-10-05 02:32:15 +03:00
"https://ws.audioscrobbler.com/2.0/"
"?method=user.getrecenttracks&user={0}&api_key={1}&format=json"
"&limit={2}&page={3}".format(username, api_key, limit, page)
)
2020-07-02 02:24:51 +03:00
data = json.loads(response.content.decode("utf8"))
2020-10-05 02:32:15 +03:00
return data["recenttracks"]["track"]
2020-07-02 02:24:51 +03:00
def get_now_scrobbling(username, api_key):
""" Getting now scrobbling track """
response = requests.get(
2020-10-05 02:32:15 +03:00
"https://ws.audioscrobbler.com/2.0/"
"?method=user.getrecenttracks&user={0}"
"&api_key={1}&format=json&limit=1".format(username, api_key)
)
2020-07-02 02:24:51 +03:00
2020-10-05 02:32:15 +03:00
data = json.loads(response.content.decode("utf-8"))
2020-07-02 02:24:51 +03:00
2020-10-05 02:32:15 +03:00
if "@attr" in data["recenttracks"]["track"][0]:
return True
2020-07-02 02:24:51 +03:00
else:
2020-10-05 02:32:15 +03:00
return False
2016-10-19 14:04:20 +03:00
2020-10-05 02:32:15 +03:00
def scrobbling_export(tracks, username, export_format="dump"):
2020-07-02 04:03:07 +03:00
""" Save scrobbled track via various format """
2020-10-05 02:32:15 +03:00
if export_format == "dump":
with open("%s.json" % (username), "w", encoding="utf-8") as f:
data = json.dumps(tracks, indent=4, sort_keys=True, ensure_ascii=False)
2020-07-02 04:03:07 +03:00
f.write(data)
2020-10-05 02:32:15 +03:00
elif export_format == "simple":
_ = []
2020-07-02 04:03:07 +03:00
for track in tracks:
2020-10-05 02:32:15 +03:00
_.append(
{
"artist": track["artist"]["#text"],
"name": track["name"],
"album": track["album"]["#text"],
"date": int(track["date"]["uts"]),
}
)
with open("%s.json" % (username), "w", encoding="utf-8") as f:
data = json.dumps(_, indent=4, sort_keys=True, ensure_ascii=False)
2020-07-02 04:03:07 +03:00
f.write(data)
2020-10-05 02:32:15 +03:00
elif export_format == "csv":
2020-07-02 04:03:07 +03:00
_ = []
for track in tracks:
2020-10-05 02:32:15 +03:00
_.append(
[
track["artist"]["#text"],
track["name"],
track["album"]["#text"],
int(track["date"]["uts"]),
]
)
with open("%s.csv" % (username), "w", encoding="utf-8", newline="") as f:
data = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC, delimiter=",")
data.writerow(["artist", "track", "album", "date"])
data.writerows(_)
2020-07-02 04:03:07 +03:00
2020-10-05 02:32:15 +03:00
return 1
2020-07-02 04:03:07 +03:00
2020-10-05 02:32:15 +03:00
if __name__ == "__main__":
2020-07-02 02:24:51 +03:00
_ = dict()
2017-04-23 20:35:19 +03:00
2020-10-05 02:32:15 +03:00
if os.path.exists("./config.json"):
with open("./config.json") as f:
2020-07-02 02:24:51 +03:00
_ = json.loads(f.read())
2017-04-23 20:35:19 +03:00
else:
2020-10-05 02:32:15 +03:00
_["api_key"] = input("API Key: ")
_["username"] = input("Username: ")
api_key, username = _["api_key"], _["username"]
2020-07-02 02:24:51 +03:00
2020-07-02 04:03:07 +03:00
total_pages = get_pages(username, api_key)
2020-07-02 02:24:51 +03:00
current_page = 1
scrobbled = []
while current_page <= total_pages:
2020-10-05 02:32:15 +03:00
print(
"\r{0:.2f}% [{1} of {2}]".format(
(current_page * 100 / total_pages), current_page, total_pages
),
end="",
)
2017-04-23 20:35:19 +03:00
2020-07-02 02:24:51 +03:00
response = get_page(username, api_key, current_page)
2016-10-18 05:27:59 +03:00
for track in response:
2020-07-02 02:24:51 +03:00
scrobbled.append(track)
2016-10-18 05:27:59 +03:00
2020-07-02 02:24:51 +03:00
current_page += 1
2016-10-18 05:27:59 +03:00
2020-07-02 04:03:07 +03:00
# if get_now_scrobbling(username, api_key):
# scrobbled.pop(0)
2017-04-23 20:35:19 +03:00
2020-10-05 02:32:15 +03:00
if scrobbling_export(scrobbled, username, _["export_format"]):
print("\n{0} tracks saved!".format(len(scrobbled), username))