Skip to content

Commit

Permalink
feat: Add endpoints for MB entities
Browse files Browse the repository at this point in the history
  • Loading branch information
anshg1214 committed Sep 1, 2022
1 parent ff2ba4c commit f7f436b
Show file tree
Hide file tree
Showing 14 changed files with 1,638 additions and 194 deletions.
5 changes: 4 additions & 1 deletion critiquebrainz/db/rating_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ def get_stats(entity_id, entity_type):
for rating in ratings:
ratings_stats[rating] += 1

average_rating = sum(ratings)/len(ratings)
if ratings:
average_rating = sum(ratings)/len(ratings)
else:
average_rating = 0

return ratings_stats, average_rating
11 changes: 11 additions & 0 deletions critiquebrainz/ws/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

deploy_env = os.environ.get('DEPLOY_ENV', '')
CONSUL_CONFIG_FILE_RETRY_COUNT = 10
REVIEWS_LIMIT = 5


def create_app(debug=None, config_path=None):
Expand Down Expand Up @@ -109,11 +110,21 @@ def _register_blueprints(app):
from critiquebrainz.ws.user.views import user_bp
from critiquebrainz.ws.review.bulk import bulk_review_bp
from critiquebrainz.ws.artist.views import artist_bp
from critiquebrainz.ws.label.views import label_bp
from critiquebrainz.ws.event.views import event_bp
from critiquebrainz.ws.place.views import place_bp
from critiquebrainz.ws.recording.views import recording_bp
from critiquebrainz.ws.release_group.views import release_group_bp
from critiquebrainz.ws.work.views import work_bp
app.register_blueprint(oauth_bp, url_prefix="/oauth")
app.register_blueprint(review_bp, url_prefix="/review")
app.register_blueprint(user_bp, url_prefix="/user")
app.register_blueprint(bulk_review_bp, url_prefix="/reviews")
app.register_blueprint(artist_bp, url_prefix="/artist")
app.register_blueprint(label_bp, url_prefix="/label")
app.register_blueprint(event_bp, url_prefix="/event")
app.register_blueprint(place_bp, url_prefix="/place")
app.register_blueprint(recording_bp, url_prefix="/recording")
app.register_blueprint(release_group_bp, url_prefix="/release-group")
app.register_blueprint(work_bp, url_prefix="/work")

390 changes: 219 additions & 171 deletions critiquebrainz/ws/artist/views.py

Large diffs are not rendered by default.

Empty file.
302 changes: 302 additions & 0 deletions critiquebrainz/ws/event/views.py

Large diffs are not rendered by default.

Empty file.
217 changes: 217 additions & 0 deletions critiquebrainz/ws/label/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
from flask import Blueprint, jsonify
import critiquebrainz.db.review as db_review
import critiquebrainz.db.rating_stats as db_rating_stats
from critiquebrainz.frontend.external.musicbrainz_db import label as db_label
from critiquebrainz.decorators import crossdomain
from critiquebrainz.ws.exceptions import NotFound
from critiquebrainz.ws.parser import Parser
from critiquebrainz.ws import REVIEWS_LIMIT

label_bp = Blueprint('ws_label', __name__)


@label_bp.route('/<uuid:label_mbid>', methods=['GET', 'OPTIONS'])
@crossdomain(headers="Authorization, Content-Type")
def label_entity_handler(label_mbid):
"""Get list of reviews.
**Request Example:**
.. code-block:: bash
$ curl "https://critiquebrainz.org/ws/1/label/e268deeb-31bc-4428-9caf-c7e2726cd496" \\
-X GET
**Response Example:**
.. code-block:: json
{
"average_rating": 5.0,
"label": {
"area": "Dakar",
"artist-rels": [
{
"artist": {
"comment": "Songwriter / Producer / CEO",
"life-span": {
"begin": "1992-06-07"
},
"mbid": "547bb51b-a016-402e-8120-50c3e8def75b",
"name": "Med Mouha",
"sort_name": "Mouha, Med",
"type": "Person"
},
"begin-year": 2019,
"direction": "backward",
"end-year": null,
"type": "label founder",
"type-id": "577996f3-7ff9-45cf-877e-740fb1267a63"
}
],
"comment": "Records, Dabass",
"external-urls": [
{
"begin-year": null,
"direction": "forward",
"end-year": null,
"icon": "discogs-16.png",
"name": "Discogs",
"type": "discogs",
"type-id": "5b987f87-25bc-4a2d-b3f1-3618795b8207",
"url": {
"mbid": "e810e5d5-d2cc-406a-9365-a2bc84e35754",
"url": "https://www.discogs.com/label/2385910"
}
}
],
"life-span": {
"begin": "2019"
},
"mbid": "e268deeb-31bc-4428-9caf-c7e2726cd496",
"name": "Dabass Records",
"rating": 100,
"type": "Production"
},
"latest_reviews": [
{
"created": "Sun, 05 Sep 2021 16:57:39 GMT",
"edits": 0,
"entity_id": "e268deeb-31bc-4428-9caf-c7e2726cd496",
"entity_type": "label",
"full_name": "Creative Commons Attribution-ShareAlike 3.0 Unported",
"id": "5d941575-29ff-4dde-a48a-ad0ba5daec8c",
"info_url": "https://creativecommons.org/licenses/by-sa/3.0/",
"is_draft": false,
"is_hidden": false,
"language": "en",
"last_revision": {
"id": 10991,
"rating": 5,
"review_id": "5d941575-29ff-4dde-a48a-ad0ba5daec8c",
"text": null,
"timestamp": "Sun, 05 Sep 2021 16:57:39 GMT"
},
"last_updated": "Sun, 05 Sep 2021 16:57:39 GMT",
"license_id": "CC BY-SA 3.0",
"popularity": 0,
"published_on": "Sun, 05 Sep 2021 16:57:39 GMT",
"rating": 5,
"source": null,
"source_url": null,
"text": null,
"user": {
"created": "Sun, 05 Sep 2021 14:20:33 GMT",
"display_name": "Medmouha",
"id": "20d9fb34-dc1f-4aa4-8082-c69e4b20f660",
"karma": 0,
"user_type": "Noob"
},
"votes_negative_count": 0,
"votes_positive_count": 0
}
],
"ratings_stats": {
"1": 0,
"2": 0,
"3": 0,
"4": 0,
"5": 1
},
"reviews_count": 1,
"top_reviews": [
{
"created": "Sun, 05 Sep 2021 16:57:39 GMT",
"edits": 0,
"entity_id": "e268deeb-31bc-4428-9caf-c7e2726cd496",
"entity_type": "label",
"full_name": "Creative Commons Attribution-ShareAlike 3.0 Unported",
"id": "5d941575-29ff-4dde-a48a-ad0ba5daec8c",
"info_url": "https://creativecommons.org/licenses/by-sa/3.0/",
"is_draft": false,
"is_hidden": false,
"language": "en",
"last_revision": {
"id": 10991,
"rating": 5,
"review_id": "5d941575-29ff-4dde-a48a-ad0ba5daec8c",
"text": null,
"timestamp": "Sun, 05 Sep 2021 16:57:39 GMT"
},
"last_updated": "Sun, 05 Sep 2021 16:57:39 GMT",
"license_id": "CC BY-SA 3.0",
"popularity": 0,
"published_on": "Sun, 05 Sep 2021 16:57:39 GMT",
"rating": 5,
"source": null,
"source_url": null,
"text": null,
"user": {
"created": "Sun, 05 Sep 2021 14:20:33 GMT",
"display_name": "Medmouha",
"id": "20d9fb34-dc1f-4aa4-8082-c69e4b20f660",
"karma": 0,
"user_type": "Noob"
},
"votes_negative_count": 0,
"votes_positive_count": 0
}
]
}
:statuscode 200: no error
:statuscode 404: label not found
:resheader Content-Type: *application/json*
"""

label = db_label.get_label_by_mbid(str(label_mbid))

if not label:
raise NotFound("Can't find a label with ID: {label_mbid}".format(label_mbid=label_mbid))

user_id = Parser.uuid('uri', 'user_id', optional=True)
if user_id:
user_review, _ = db_review.list_reviews(
entity_id=label['mbid'],
entity_type='label',
user_id=user_id
)
if user_review:
user_review = db_review.to_dict(user_review[0])
else:
user_review = None

ratings_stats, average_rating = db_rating_stats.get_stats(label_mbid, "label")

top_reviews, reviews_count = db_review.list_reviews(
entity_id=label['mbid'],
entity_type='label',
sort='popularity',
limit=REVIEWS_LIMIT,
offset=0,
)

latest_reviews, reviews_count = db_review.list_reviews(
entity_id=label['mbid'],
entity_type='label',
sort='published_on',
limit=REVIEWS_LIMIT,
offset=0,
)

top_reviews = [db_review.to_dict(review) for review in top_reviews]
latest_reviews = [db_review.to_dict(review) for review in latest_reviews]

result = {
"label": label,
"average_rating": average_rating,
"ratings_stats": ratings_stats,
"reviews_count": reviews_count,
"top_reviews": top_reviews,
"latest_reviews": latest_reviews
}
if user_id:
result['user_review'] = user_review

return jsonify(**result)
Empty file.
Loading

0 comments on commit f7f436b

Please sign in to comment.