diff --git a/README.md b/README.md index ba709cf..1ab5c66 100644 --- a/README.md +++ b/README.md @@ -156,7 +156,6 @@ POST /v1/compositions ``` Request Headers -X-OMI-Version: 1.0 Content-Type: application/json Body @@ -195,6 +194,46 @@ The composition was successfully registered. ``` +### Register a Recording + +POST /v1/recordings + +``` +Request +Headers +Content-Type: application/json + +Body +{ + "title": "Crystallize", + "isrc": "US-TEY-09-00057", + "labels": [ + { + "id": "string", + "name": "string" + } + ], + "artists": [ + { + "name": "Lindsey Stirling", + "isni": "0000 0004 0314 2012" + } + ], + "released": "01/01/1970", + "duration": "00:02:16", + "versionTitle": "Hello, world!", + "albumTitle": "Hello, world!" +} + + +Response + +201 Created + +The recording was successfully registered. +``` + + ### Register a Manifestation In order to register the manifestation on BigchainDB as transactions on a diff --git a/omi_api/cli.py b/omi_api/cli.py index f214cbf..a5002de 100644 --- a/omi_api/cli.py +++ b/omi_api/cli.py @@ -1,9 +1,11 @@ import os import click +import pymongo from bigchaindb_driver.crypto import generate_keypair from omi_api.server import create_server +from omi_api.queries import bdb_coll @click.group() @@ -11,6 +13,16 @@ def cli(): pass +@cli.command() +def indexes(): + base = 'block.transactions.asset.data' + indexes = ['iswc', 'isrc', 'title', 'artists.name', 'composers.name', + 'songwriters.name', 'publishers.name', 'name'] + for i in indexes: + bdb_coll().create_index([('{}.{}'.format(base, i), + pymongo.ASCENDING)]) + + @cli.command() def keypair(): keypair = generate_keypair() diff --git a/omi_api/queries.py b/omi_api/queries.py new file mode 100644 index 0000000..311bfa8 --- /dev/null +++ b/omi_api/queries.py @@ -0,0 +1,36 @@ +import os + +from pymongo import MongoClient + +def bdb(): + return MongoClient(os.environ.get('MONGODB_HOST', None), + int(os.environ.get('MONGODB_PORT', None)), + ssl=False) + +def bdb_coll(): + return bdb()['bigchain']['bigchain'] + +def bdb_find(query, _type): + # TODO: make this a global? + base = 'block.transactions.asset.data' + + match = [] + for key, value in query.items(): + # Filter out None values + if value: + match.append({'{}.{}'.format(base, key): value}) + match.append({'{}.@type'.format(base): _type}) + match = {'$and': match} + + pipeline = [ + {'$match': match}, + {'$unwind': '$block.transactions'}, + {'$match': match}, + {'$project': { + '_id': False, + 'block.transactions.asset.data': True + }} + ] + + cur = bdb_coll().aggregate(pipeline) + return cur diff --git a/omi_api/views/compositions.py b/omi_api/views/compositions.py index 7a1cf9d..d0972e0 100644 --- a/omi_api/views/compositions.py +++ b/omi_api/views/compositions.py @@ -6,6 +6,7 @@ from coalaip import CoalaIp, entities from coalaip_bigchaindb.plugin import Plugin from omi_api.utils import get_bigchaindb_api_url +from omi_api.queries import bdb_find coalaip = CoalaIp(Plugin(get_bigchaindb_api_url())) @@ -15,6 +16,26 @@ class CompositionListApi(Resource): + def get(self): + parser = reqparse.RequestParser() + parser.add_argument('title', type=str) + parser.add_argument('name', type=str) + #TODO add all other parameters + args = dict(parser.parse_args()) + + res = bdb_find(query=args, _type='AbstractWork') + resp = [] + for doc in res: + doc = doc['block']['transactions']['asset']['data'] + doc = { + 'title': doc['name'], + 'composers': doc['composers'], + 'songwriters': doc['songwriters'], + 'publishers': doc['publishers'], + } + resp.append(doc) + return resp + def post(self): parser = reqparse.RequestParser() diff --git a/omi_api/views/recordings.py b/omi_api/views/recordings.py index 677d14a..4d0bd0b 100644 --- a/omi_api/views/recordings.py +++ b/omi_api/views/recordings.py @@ -7,6 +7,7 @@ from coalaip_bigchaindb.plugin import Plugin from omi_api.models import recording_model from omi_api.utils import get_bigchaindb_api_url +from omi_api.queries import bdb_find coalaip = CoalaIp(Plugin(get_bigchaindb_api_url())) @@ -16,25 +17,46 @@ class RecordingListApi(Resource): + def get(self): + # TODO method can be generalized to utility probably + parser = reqparse.RequestParser() + parser.add_argument('title', type=str) + parser.add_argument('name', type=str) + #TODO add all other parameters + args = dict(parser.parse_args()) + + res = bdb_find(query=args, _type='CreativeWork') + resp = [] + for doc in res: + #todo this is super ugly + doc = doc['block']['transactions']['asset']['data'] + print(doc) + doc = { + 'title': doc['name'], + 'labels': doc['labels'], + 'artists': doc['artists'], + } + resp.append(doc) + return resp + def post(self): parser = reqparse.RequestParser() # These are the required parameters parser.add_argument('title', type=str, required=True, location='json') - parser.add_argument('composers', type=list, required=True, + parser.add_argument('labels', type=list, required=True, location='json') - parser.add_argument('songwriters', type=list, required=True, + parser.add_argument('artists', type=list, required=True, location='json') - parser.add_argument('publishers', type=list, required=True, + parser.add_argument('isrc', type=str, required=False, location='json') args = parser.parse_args() # Here we're transforming from OMI to COALA - work = { + manifestation = { 'name': args['title'], - 'composers': args['composers'], - 'songwriters': args['songwriters'], - 'publishers': args['publishers'], + 'labels': args['labels'], + 'artists': args['artists'], } copyright_holder = { @@ -43,13 +65,14 @@ def post(self): } # TODO: Do a mongodb query to extract the id of the work + # OR: Maybe we just register the manifestation without the work for now + # ? - copyright_, manifestation, work = coalaip.register_manifestation( - manifestation_data=manifestation, - copyright_holder=copyright_holder, - work_data=work - ) - + #copyright_, manifestation, work = coalaip.register_manifestation( + # manifestation_data=manifestation, + # copyright_holder=copyright_holder, + # work_data=work + #) return 'The recording was successfully registered.', 200 diff --git a/setup.py b/setup.py index a324fb7..8bd35f5 100644 --- a/setup.py +++ b/setup.py @@ -10,6 +10,7 @@ 'flask-restful>=0.3.5', 'gunicorn>=19.6.0', 'flask-cors==3.0.2', + 'pymongo~=3.4', ] tests_require = [