25
25
)
26
26
27
27
28
- async def combine (keyphrase : str ) -> str :
29
- """
30
- Create JSON array containing YouTube IDs of the top tracks by the given artist according to Last.fm.
31
- :param keyphrase: Name of an artist or a band.
32
- :return: str containing JSON array.
33
- """
34
- top = await fetching .create_top (keyphrase )
35
- ids = json .dumps (top )
36
- return ids
37
-
38
-
39
28
def process (keyphrase : str ) -> list :
40
29
"""
41
30
Check if an entry for the given artist exists in the database, update it if it is outdated,
@@ -44,36 +33,38 @@ def process(keyphrase: str) -> list:
44
33
:return: List of YouTube IDs.
45
34
"""
46
35
try :
47
- name = await fetching .get_name (keyphrase )
48
- name = name .lower ()
36
+ artist = await fetching .get_name (keyphrase )
37
+ artist = artist .lower ()
49
38
except Exception as e :
50
39
logging .debug (
51
40
f"An error occurred while fetching artist name from Last.fm: { e } ."
52
41
)
53
- name = keyphrase .lower ()
42
+ artist = keyphrase .lower ()
54
43
today = datetime .now ()
55
44
conn = await asyncpg .connect (dsn = DATABASE_URL )
56
- record = await conn .fetch (f"SELECT * FROM top WHERE artist = '{ name } '" )
45
+ record = await conn .fetch (f"SELECT * FROM top WHERE artist = '{ artist } '" )
57
46
if (
58
47
record
59
- and (today - datetime .strptime (record [0 ]["date" ], "%Y-%m-%d" )).days < VALID_FOR_DAYS
48
+ and (today - datetime .strptime (record [0 ]["date" ], "%Y-%m-%d" )).days
49
+ < VALID_FOR_DAYS
60
50
):
61
- logging .info (f'There is an artist with the name "{ name } " in the database' )
51
+ logging .info (f'There is an artist with the name "{ artist } " in the database' )
62
52
await conn .execute (
63
- f"UPDATE top SET requests = requests + 1 WHERE artist = '{ name } '"
53
+ f"UPDATE top SET requests = requests + 1 WHERE artist = '{ artist } '"
64
54
)
65
- tracks = record [0 ]["tracks" ]
55
+ tracks = json . loads ( record [0 ]["tracks" ])
66
56
else :
67
57
logging .info (
68
- f'There is no artist named "{ name } " in the database or the entry is older than { VALID_FOR_DAYS } days'
58
+ f'There is no artist named "{ artist } " in the database or the entry is older than { VALID_FOR_DAYS } days'
69
59
)
70
- tracks = await combine (name )
60
+ tracks = await fetching .create_top (artist )
61
+ tracks_json = json .dumps (tracks )
71
62
date = datetime .strftime (today , "%Y-%m-%d" )
72
63
query = f"""INSERT INTO top (artist, tracks, date, requests)
73
- VALUES('{ name } ', '{ tracks } ', '{ date } ', 1)
64
+ VALUES('{ artist } ', '{ tracks_json } ', '{ date } ', 1)
74
65
ON CONFLICT (artist)
75
- DO UPDATE SET tracks = '{ tracks } ', date = '{ date } ', requests = top.requests + 1"""
66
+ DO UPDATE SET tracks = '{ tracks_json } ', date = '{ date } ', requests = top.requests + 1"""
76
67
await conn .execute (query )
77
- logging .info (f'Entry for "{ name } " created/updated in the database' )
68
+ logging .info (f'Entry for "{ artist } " created/updated in the database' )
78
69
await conn .close ()
79
70
return json .loads (tracks )
0 commit comments