Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Commit

Permalink
Progress on queries
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDaub committed May 5, 2017
1 parent e78418a commit 25b1e57
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 14 deletions.
41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ POST /v1/compositions
```
Request
Headers
X-OMI-Version: 1.0
Content-Type: application/json
Body
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions omi_api/cli.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
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()
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()
Expand Down
36 changes: 36 additions & 0 deletions omi_api/queries.py
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions omi_api/views/compositions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand All @@ -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()

Expand Down
49 changes: 36 additions & 13 deletions omi_api/views/recordings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand All @@ -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 = {
Expand All @@ -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


Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'flask-restful>=0.3.5',
'gunicorn>=19.6.0',
'flask-cors==3.0.2',
'pymongo~=3.4',
]

tests_require = [
Expand Down

0 comments on commit 25b1e57

Please sign in to comment.