mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-05 04:53:01 +00:00
use artist tracks in period to generate mix
This commit is contained in:
+17
-8
@@ -217,7 +217,13 @@ class MixesPlugin(Plugin):
|
|||||||
if artist["artisthash"] in indexed:
|
if artist["artisthash"] in indexed:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
mix = self.create_artist_mix(artist)
|
# INFO: track['tracks'] is a dict of trackhashes and their counts
|
||||||
|
# get the trackhashes sorted by count
|
||||||
|
trackhashes = sorted(
|
||||||
|
artist["tracks"], key=lambda x: artist["tracks"][x], reverse=True
|
||||||
|
)
|
||||||
|
|
||||||
|
mix = self.create_artist_mix(artist, trackhashes[:self.MAX_TRACKS_TO_FETCH])
|
||||||
|
|
||||||
if mix:
|
if mix:
|
||||||
mixes.append(mix)
|
mixes.append(mix)
|
||||||
@@ -253,7 +259,7 @@ class MixesPlugin(Plugin):
|
|||||||
|
|
||||||
return f"Featuring {tracks[0].artists[0]['name']}"
|
return f"Featuring {tracks[0].artists[0]['name']}"
|
||||||
|
|
||||||
def create_artist_mix(self, artist: dict[str, str]):
|
def create_artist_mix(self, artist: dict[str, str], trackhashes: list[str]):
|
||||||
"""
|
"""
|
||||||
Given an artist dict, creates an artist mix.
|
Given an artist dict, creates an artist mix.
|
||||||
"""
|
"""
|
||||||
@@ -262,10 +268,12 @@ class MixesPlugin(Plugin):
|
|||||||
if not _artist:
|
if not _artist:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
tracks = TrackStore.get_tracks_by_trackhashes(_artist.trackhashes)
|
tracks = TrackStore.get_tracks_by_trackhashes(trackhashes)
|
||||||
tracks = sorted(tracks, key=lambda x: x.playduration, reverse=True)
|
# tracks = sorted(tracks, key=lambda x: x.playduration, reverse=True)
|
||||||
sourcetracks = tracks[: self.MAX_TRACKS_TO_FETCH]
|
# sourcetracks = tracks[: self.MAX_TRACKS_TO_FETCH]
|
||||||
sourcehash = create_hash(*[t.trackhash for t in sourcetracks])
|
|
||||||
|
# INFO: Sort the trackhashes when creating the sourcehash
|
||||||
|
sourcehash = create_hash(*sorted(trackhashes, key=lambda x: trackhashes.index(x)))
|
||||||
|
|
||||||
db_mix = MixTable.get_by_sourcehash(sourcehash)
|
db_mix = MixTable.get_by_sourcehash(sourcehash)
|
||||||
if db_mix:
|
if db_mix:
|
||||||
@@ -273,7 +281,7 @@ class MixesPlugin(Plugin):
|
|||||||
print(db_mix.title)
|
print(db_mix.title)
|
||||||
return db_mix
|
return db_mix
|
||||||
|
|
||||||
mix_tracks, albums, artists = self.get_track_mix(sourcetracks)
|
mix_tracks, albums, artists = self.get_track_mix(tracks)
|
||||||
|
|
||||||
if len(mix_tracks) < self.MIN_TRACK_MIX_LENGTH:
|
if len(mix_tracks) < self.MIN_TRACK_MIX_LENGTH:
|
||||||
return None
|
return None
|
||||||
@@ -296,6 +304,7 @@ class MixesPlugin(Plugin):
|
|||||||
extra={
|
extra={
|
||||||
"type": "artist",
|
"type": "artist",
|
||||||
"artisthash": artist["artisthash"],
|
"artisthash": artist["artisthash"],
|
||||||
|
"sourcetracks": trackhashes,
|
||||||
"image": mix_image,
|
"image": mix_image,
|
||||||
# NOTE: Save the similar albums and artists
|
# NOTE: Save the similar albums and artists
|
||||||
# Related to the source tracks that were used to create the mix
|
# Related to the source tracks that were used to create the mix
|
||||||
@@ -430,7 +439,7 @@ class MixesPlugin(Plugin):
|
|||||||
|
|
||||||
return Mix(
|
return Mix(
|
||||||
id=f"t{mix.extra['artisthash']}",
|
id=f"t{mix.extra['artisthash']}",
|
||||||
title="", # INFO: Will be filled after all mixes are created.
|
title="", # INFO: Will be filled after all mixes are created.
|
||||||
description=self.get_mix_description(tracks, mix.extra["artisthash"]),
|
description=self.get_mix_description(tracks, mix.extra["artisthash"]),
|
||||||
tracks=[t.trackhash for t in tracks],
|
tracks=[t.trackhash for t in tracks],
|
||||||
sourcehash=create_hash(*[t.trackhash for t in tracks]),
|
sourcehash=create_hash(*[t.trackhash for t in tracks]),
|
||||||
|
|||||||
+9
-1
@@ -15,12 +15,15 @@ def get_artists_in_period(
|
|||||||
start_time: int | float, end_time: int | float, userid: int | None = None
|
start_time: int | float, end_time: int | float, userid: int | None = None
|
||||||
):
|
):
|
||||||
scrobbles = ScrobbleTable.get_all_in_period(start_time, end_time, userid)
|
scrobbles = ScrobbleTable.get_all_in_period(start_time, end_time, userid)
|
||||||
artists: Any = defaultdict(lambda: {"playcount": 0, "playduration": 0})
|
artists: Any = defaultdict(
|
||||||
|
lambda: {"playcount": 0, "playduration": 0, "tracks": {}}
|
||||||
|
)
|
||||||
|
|
||||||
for scrobble in scrobbles:
|
for scrobble in scrobbles:
|
||||||
track = TrackStore.get_tracks_by_trackhashes([scrobble.trackhash])
|
track = TrackStore.get_tracks_by_trackhashes([scrobble.trackhash])
|
||||||
if not track:
|
if not track:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
track = track[0]
|
track = track[0]
|
||||||
|
|
||||||
for artist in track.artists:
|
for artist in track.artists:
|
||||||
@@ -31,6 +34,11 @@ def get_artists_in_period(
|
|||||||
artists[artisthash]["playcount"] += 1
|
artists[artisthash]["playcount"] += 1
|
||||||
artists[artisthash]["playduration"] += scrobble.duration
|
artists[artisthash]["playduration"] += scrobble.duration
|
||||||
|
|
||||||
|
# index the track counts too
|
||||||
|
artists[artisthash]["tracks"][track.trackhash] = (
|
||||||
|
artists[artisthash]["tracks"].get(track.trackhash, 0) + 1
|
||||||
|
)
|
||||||
|
|
||||||
artists = list(artists.values())
|
artists = list(artists.values())
|
||||||
return sorted(artists, key=lambda x: x["playduration"], reverse=True)
|
return sorted(artists, key=lambda x: x["playduration"], reverse=True)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user