Skip to content

Instantly share code, notes, and snippets.

@ferbncode
Created March 14, 2017 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ferbncode/b1dc5277d311ea84eca1ccd2a6771620 to your computer and use it in GitHub Desktop.
Save ferbncode/b1dc5277d311ea84eca1ccd2a6771620 to your computer and use it in GitHub Desktop.
from critiquebrainz import mb_db
import sys
import sqlalchemy
from brainzutils import cache
from critiquebrainz.mb_db import db_helpers
# example function to get tags, release_group, artists, url_rels
def get_release_group_by_id(id, relations=[]):
"""(Example function)Get release group with the MusicBrainz ID."""
key = cache.gen_key(id)
release_group = cache.get(key)
if not release_group:
with mb_db.mb_engine.connect() as connection:
result = connection.execute(sqlalchemy.text("""
SELECT rg.id,
rg.name,
rg.artist_credit,
ty.name AS type,
artist_credit.artist_count,
artist_credit.name,
array_agg(acn.name) as artist_names,
array_agg(artist.gid) as artist_gids
FROM release_group AS rg
LEFT JOIN release_group_primary_type AS ty
ON rg.type = ty.id
JOIN artist_credit
ON rg.artist_credit = artist_credit.id
JOIN artist_credit_name acn
ON acn.artist_credit = artist_credit.id
JOIN artist
ON acn.artist = artist.id
WHERE rg.gid = :release_group_id
GROUP BY rg.id, ty.id, artist_credit.id
"""),{
"release_group_id": id,
})
row = result.fetchone()
release_group = dict(row)
# The data fetched needs to be properly arranged
# in dictionaries.
if not rows:
raise NoDataFoundError("No data Found")
if "url-rels" in relations:
with mb_db.db_engine.connect() as connection:
result = connection.execute(sqlalchemy.text("""
SELECT url.id,
url.gid,
url.url,
link_type.name,
release_group.gid,
release_group.name
FROM l_release_group_url
INNER JOIN url
ON url.id = l_release_group_url.entity1
INNER JOIN release_group
ON l_release_group_url.entity1 = release_group.id
INNER JOIN link
ON l_release_group_url.link = link.id
INNER JOIN link_type
ON link.link_type=link_type.id
WHERE release_group.gid = :id
"""), {
"id": id,
})
url_relations = result.fetchall()
# Urls can be arranged then in a dictionary as critiquebrainz.frontend.external.relationships does
if url_relations:
release_group["url-relations"] = url_relations
## Adding new features can be easier in this case.
## Eg. Adding tags,
if "tags" in relations:
## We can use helpers for things common between entities.
## This is similar to what Search Server actually does.
tags = db_helpers.get_tags(id, 'release_group', 'release_group_tag', 'release_group')
if tags:
release_group["tags"] = tags
## Cache results as it is done presently
cache.set(key=key, val=release_group, time=DEFAULT_CACHE_EXPIRATION)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment