-
Notifications
You must be signed in to change notification settings - Fork 4
/
lastfm-all_favorite_tracks.py
78 lines (62 loc) · 2.08 KB
/
lastfm-all_favorite_tracks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import pylast
import configparser
import logging
import time
import argparse
from pathlib import Path
logger = logging.getLogger()
temps_debut = time.time()
def lastfmconnect():
config = configparser.ConfigParser()
config.read("config.ini")
api_key = config["lastfm"]["api_key"]
api_secret = config["lastfm"]["api_secret"]
api_username = config["lastfm"]["username"]
api_password = pylast.md5(config["lastfm"]["password"])
network = pylast.LastFMNetwork(
api_key=api_key,
api_secret=api_secret,
username=api_username,
password_hash=api_password,
)
return network
def main():
args = parse_args()
network = lastfmconnect()
if not args.username:
raise Exception("Use the -u/--username flag to set an username.")
users = [x.strip() for x in args.username.split(",")]
Path("Exports").mkdir(parents=True, exist_ok=True)
for user in users:
logger.info("Extracting favorite tracks for %s.", user)
user = network.get_user(user)
loved_tracks = user.get_loved_tracks(limit=None)
loved_tracks = [x.track for x in loved_tracks]
logger.info("%s tracks extracted for %s.", len(loved_tracks), user)
with open(f"Exports/{int(time.time())}_{user}_favorite_tracks.csv", "w") as f:
for track in loved_tracks:
f.write(f"{track.artist} - {track.title}\n")
logger.info("Runtime : %.2f seconds" % (time.time() - temps_debut))
def parse_args():
parser = argparse.ArgumentParser(
description="Extract all favorite tracks from one or several lastfm users."
)
parser.add_argument(
"--debug",
help="Display debugging information.",
action="store_const",
dest="loglevel",
const=logging.DEBUG,
default=logging.INFO,
)
parser.add_argument(
"--username",
"-u",
help="Names of the users (separated by comma).",
type=str,
)
args = parser.parse_args()
logging.basicConfig(level=args.loglevel)
return args
if __name__ == "__main__":
main()