Skip to content

Commit 890bb88

Browse files
committed
Remove combine coroutine
There is no need in separate coroutine for this functionality, now it is handled inside 'process' coroutine.
1 parent 5a0f5e5 commit 890bb88

File tree

1 file changed

+15
-24
lines changed

1 file changed

+15
-24
lines changed

storing.py

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,6 @@
2525
)
2626

2727

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-
3928
def process(keyphrase: str) -> list:
4029
"""
4130
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:
4433
:return: List of YouTube IDs.
4534
"""
4635
try:
47-
name = await fetching.get_name(keyphrase)
48-
name = name.lower()
36+
artist = await fetching.get_name(keyphrase)
37+
artist = artist.lower()
4938
except Exception as e:
5039
logging.debug(
5140
f"An error occurred while fetching artist name from Last.fm: {e}."
5241
)
53-
name = keyphrase.lower()
42+
artist = keyphrase.lower()
5443
today = datetime.now()
5544
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}'")
5746
if (
5847
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
6050
):
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')
6252
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}'"
6454
)
65-
tracks = record[0]["tracks"]
55+
tracks = json.loads(record[0]["tracks"])
6656
else:
6757
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'
6959
)
70-
tracks = await combine(name)
60+
tracks = await fetching.create_top(artist)
61+
tracks_json = json.dumps(tracks)
7162
date = datetime.strftime(today, "%Y-%m-%d")
7263
query = f"""INSERT INTO top (artist, tracks, date, requests)
73-
VALUES('{name}', '{tracks}', '{date}', 1)
64+
VALUES('{artist}', '{tracks_json}', '{date}', 1)
7465
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"""
7667
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')
7869
await conn.close()
7970
return json.loads(tracks)

0 commit comments

Comments
 (0)