|
| 1 | +from datetime import datetime |
| 2 | + |
| 3 | +import troi.filters |
| 4 | +import troi.listenbrainz.recs |
| 5 | +import troi.musicbrainz.recording_lookup |
| 6 | +import troi.musicbrainz.year_lookup |
| 7 | +import troi.sorts |
| 8 | +from troi import Recording |
| 9 | +from troi.listenbrainz.dataset_fetcher import DataSetFetcherElement |
| 10 | +from troi.playlist import PlaylistShuffleElement, PlaylistRedundancyReducerElement |
| 11 | + |
| 12 | + |
| 13 | +class TopDiscoveries(troi.patch.Patch): |
| 14 | + """ |
| 15 | + See below for description |
| 16 | + """ |
| 17 | + |
| 18 | + NAME = "Top Discoveries of %d for %s" |
| 19 | + DESC = """<p> |
| 20 | + This playlist contains the top tracks for %s that were first listened to in %d. |
| 21 | + </p> |
| 22 | + <p> |
| 23 | + For more information on how this playlist is generated, please see our |
| 24 | + <a href="https://musicbrainz.org/doc/YIM2022Playlists">Year in Music 2022 Playlists</a> page. |
| 25 | + </p> |
| 26 | + """ |
| 27 | + |
| 28 | + def __init__(self, debug=False): |
| 29 | + troi.patch.Patch.__init__(self, debug) |
| 30 | + |
| 31 | + @staticmethod |
| 32 | + def inputs(): |
| 33 | + """ |
| 34 | + Generate a top discoveries playlist for a user. |
| 35 | +
|
| 36 | + \b |
| 37 | + USER_ID: is a MusicBrainz userid that has an account on ListenBrainz. |
| 38 | + USER_NAME: is a MusicBrainz username that has an account on ListenBrainz. |
| 39 | + """ |
| 40 | + return [{"type": "argument", "args": ["user_id"]}, |
| 41 | + {"type": "argument", "args": ["user_name"]}] |
| 42 | + |
| 43 | + @staticmethod |
| 44 | + def outputs(): |
| 45 | + return [Recording] |
| 46 | + |
| 47 | + @staticmethod |
| 48 | + def slug(): |
| 49 | + return "top-discoveries-for-year" |
| 50 | + |
| 51 | + @staticmethod |
| 52 | + def description(): |
| 53 | + return "Generate a top discoveries playlist for a user." |
| 54 | + |
| 55 | + def create(self, inputs): |
| 56 | + recs = DataSetFetcherElement(server_url="https://datasets.listenbrainz.org/top-discoveries/json", |
| 57 | + json_post_data=[{ 'user_id': inputs['user_id'] }]) |
| 58 | + |
| 59 | + year = datetime.now().year |
| 60 | + pl_maker = troi.playlist.PlaylistMakerElement(self.NAME % (year, inputs['user_name']), |
| 61 | + self.DESC % (inputs['user_name'], year), |
| 62 | + patch_slug=self.slug(), |
| 63 | + user_name=inputs['user_name']) |
| 64 | + pl_maker.set_sources(recs) |
| 65 | + |
| 66 | + shaper = PlaylistRedundancyReducerElement() |
| 67 | + shaper.set_sources(pl_maker) |
| 68 | + |
| 69 | + shuffle = PlaylistShuffleElement() |
| 70 | + shuffle.set_sources(shaper) |
| 71 | + |
| 72 | + return shuffle |
0 commit comments