This repository has been archived by the owner on Aug 7, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msid_mapping.py
executable file
·66 lines (54 loc) · 2.75 KB
/
msid_mapping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
import psycopg2
import psycopg2.extras
from datasethoster import Query
from datasethoster.main import app, register_query
import config
class MSIDMappingQuery(Query):
def names(self):
return ("msid-mapping", "MessyBrainz <=> MusicBrainz Mapping")
def inputs(self):
return ['msb_artist_credit_name', 'msb_recording_name']
def introduction(self):
return """This page allows you to enter the name of an artist and the name of a recording (track)
and the query will attempt to find a match in the MessyBrainz mapping."""
def outputs(self):
return ['index', 'artist_arg', 'recording_arg',
'mb_artist_name', 'mb_release_name', 'mb_recording_name',
'mb_release_mbid', 'mb_recording_mbid', 'mb_artist_credit_id']
def fetch(self, params, offset=-1, limit=-1):
args = []
for i, param in enumerate(params):
args.append(tuple((i,
"".join(param['msb_artist_credit_name'].lower().split()),
"".join(param['msb_recording_name'].lower().split())
)
)
)
args = tuple(args)
with psycopg2.connect(config.DB_CONNECT_MB) as conn:
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as curs:
subst = ",".join(['%s'] * len(args))
curs.execute("""SELECT DISTINCT index, arg.name AS artist_arg, arg.rec AS recording_arg,
ac.name AS mb_artist_name,
rl.name AS mb_release_name,
r.name AS mb_recording_name,
rl.gid AS mb_release_mbid,
r.gid AS mb_recording_mbid,
mb_artist_credit_id
FROM mapping.msid_mbid_mapping
JOIN recording r
ON r.id = mb_recording_id
JOIN release rl
ON rl.id = mb_release_id
JOIN artist_credit ac
ON r.artist_credit = ac.id
RIGHT JOIN (values """ + subst + """) AS arg(index, name, rec)
ON msb_artist_name = arg.name AND msb_recording_name = arg.rec""", args)
results = []
while True:
data = curs.fetchone()
if not data:
break
results.append(dict(data))
return results