This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
143 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import os | ||
from collections import defaultdict | ||
import datetime | ||
import sys | ||
from uuid import UUID | ||
|
||
import peewee | ||
import requests | ||
|
||
from lb_content_resolver.model.database import db | ||
from lb_content_resolver.model.recording import Recording, RecordingMetadata | ||
|
||
|
||
class MetadataLookup: | ||
''' | ||
Given the local database, lookup metadata from MusicBrainz to allow local playlist resolution. | ||
''' | ||
|
||
def __init__(self, db): | ||
self.db = db | ||
|
||
def lookup(self): | ||
""" | ||
""" | ||
|
||
self.db.open_db() | ||
args = [] | ||
mbid_to_id_index = {} | ||
for recording in Recording.select() \ | ||
.join(RecordingMetadata, peewee.JOIN.LEFT_OUTER) \ | ||
.order_by(RecordingMetadata.last_updated): | ||
args.append({ "[recording_mbid]": str(recording.recording_mbid) }) | ||
mbid_to_id_index[str(recording.recording_mbid)] = recording | ||
if len(args) == 1000: | ||
break | ||
|
||
r = requests.post("https://labs.api.listenbrainz.org/bulk-tag-lookup/json", json=args) | ||
if r.status_code != 200: | ||
print("Fail: %d %s" % (r.status_code, r.text)) | ||
return | ||
|
||
recording_pop = {} | ||
recording_tags = {} | ||
for row in r.json(): | ||
print("%s, %s, %s" % (row["recording_mbid"], row["tag"], row["source"])) | ||
|
||
mbid = str(row["recording_mbid"]) | ||
recording_pop[mbid] = row["percent"] | ||
if mbid not in recording_tags: | ||
recording_tags[mbid] = { "artist": [], "release-group": [], "recording": [] } | ||
|
||
recording_tags[mbid][row["source"]].append(row["tag"]) | ||
|
||
print(f"{len(args)} db rows, {len(r.json())} api rows") | ||
|
||
with db.atomic(): | ||
mbids = recording_pop.keys() | ||
for mbid in list(set(mbids)): | ||
recording = mbid_to_id_index[mbid] | ||
print(f"update {mbid}") | ||
try: | ||
_ = recording.metadata.last_updated | ||
print("update existing") | ||
recording.metadata.popularity = row["percent"] | ||
recording.metadata.last_updated = datetime.datetime.now() | ||
recording.save() | ||
except AttributeError: | ||
print("create new") | ||
recording.metadata = RecordingMetadata.create(recording=recording.id, | ||
popularity=recording_pop[mbid], | ||
last_updated=datetime.datetime.now()) | ||
recording.save() | ||
|
||
# for row in r.json(): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import datetime | ||
from peewee import * | ||
from lb_content_resolver.model.database import db | ||
|
||
|
||
class Tag(Model): | ||
""" | ||
Represents a tag that could be joined to an entity | ||
""" | ||
|
||
class Meta: | ||
database = db | ||
|
||
id = AutoField() | ||
name = TextField(null=False, unique=True) | ||
|
||
def __repr__(self): | ||
return "<Tag('%s')>" % (self.name or "") | ||
|
||
|
||
class RecordingTag(Model): | ||
""" | ||
A tag connected to a recording | ||
""" | ||
|
||
class Meta: | ||
database = db | ||
|
||
id = AutoField() | ||
recording = ForeignKeyField(Recording) | ||
tag = ForeignKeyField(Tag) | ||
last_updated = DateTimeField(null=False, default=datetime.datetime.now) | ||
entity = TextField(null=False) | ||
|
||
def __repr__(self): | ||
return "<RecordingTag('%s','%d')>" % (self.tag.name or "", self.recording) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters