def review_entity_handler(review_id): """Get review with a specified UUID. **Request Example:** .. code-block:: bash $ curl https://critiquebrainz.org/ws/1/review/b7575c23-13d5-4adc-ac09-2f55a647d3de \\ -X GET **Response Example:** .. code-block:: json { "review": { "created": "Tue, 10 Aug 2010 00:00:00 GMT", "edits": 0, "entity_id": "03e0a99c-3530-4e64-8f50-6592325c2082", "entity_type": "release_group", "id": "b7575c23-13d5-4adc-ac09-2f55a647d3de", "language": "en", "last_updated": "Tue, 10 Aug 2010 00:00:00 GMT", "license": { "full_name": "Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported", "id": "CC BY-NC-SA 3.0", "info_url": "https://creativecommons.org/licenses/by-nc-sa/3.0/" }, "popularity": 0, "source": "BBC", "source_url": "http://www.bbc.co.uk/music/reviews/3vfd", "text": "TEXT CONTENT OF REVIEW", "rating": 5, "user": { "created": "Wed, 07 May 2014 14:55:23 GMT", "display_name": "Paul Clarke", "id": "f5857a65-1eb1-4574-8843-ae6195de16fa", "karma": 0, "user_type": "Noob" }, "votes": { "positive": 0, "negative": 0 } } } :statuscode 200: no error :statuscode 404: review not found :resheader Content-Type: *application/json* """ review = get_review_or_404(review_id) if review["is_hidden"]: raise NotFound("Review has been hidden.") return jsonify(review=db_review.to_dict(review))
def json(location, rotate=False): """Create JSON dumps with all reviews. This command will create an archive for each license available on CB. Archives will be put into a specified directory (default is *dump*). """ create_path(location) current_app.json_encoder = DumpJSONEncoder print("Creating new archives...") with db.engine.begin() as connection: for license in db_license.get_licenses_list(connection): safe_name = slugify(license["id"]) with tarfile.open(os.path.join(location, "critiquebrainz-%s-%s-json.tar.bz2" % (datetime.today().strftime('%Y%m%d'), safe_name)), "w:bz2") as tar: temp_dir = tempfile.mkdtemp() license_dir = os.path.join(temp_dir, safe_name) create_path(license_dir) # Finding entities that have reviews with current license entities = db_review.get_distinct_entities(connection) for entity in entities: entity = str(entity) # Creating directory structure and dumping reviews dir_part = os.path.join(entity[0:1], entity[0:2]) reviews = db_review.get_reviews_list(connection, entity_id=entity, license_id=license["id"], limit=None)[0] if reviews: rg_dir = '%s/%s' % (license_dir, dir_part) create_path(rg_dir) f = open('%s/%s.json' % (rg_dir, entity), 'w+') f.write(jsonify(reviews=[db_review.to_dict(r, connection=connection) for r in reviews]) .data.decode("utf-8")) f.close() tar.add(license_dir, arcname='reviews') # Copying legal text tar.add(os.path.join(os.path.dirname(os.path.realpath(__file__)), "licenses", safe_name + ".txt"), arcname='COPYING') print(" + %s/critiquebrainz-%s-%s-json.tar.bz2" % (location, datetime.today().strftime('%Y%m%d'), safe_name)) shutil.rmtree(temp_dir) # Cleanup if rotate: print("Removing old sets of archives (except two latest)...") remove_old_archives(location, r"critiquebrainz-[0-9]+-[-\w]+-json.tar.bz2", is_dir=False, sort_key=os.path.getmtime) print("Done!")
def review_list_handler(): """Get list of reviews. **Request Example:** .. code-block:: bash $ curl "https://critiquebrainz.org/ws/1/review/?limit=1&offset=50" \\ -X GET **Response Example:** .. code-block:: json { "count": 9197, "limit": 1, "offset": 50, "reviews": [ { "created": "Fri, 16 May 2008 00:00:00 GMT", "edits": 0, "entity_id": "09259937-6477-3959-8b10-af1cbaea8e6e", "entity_type": "release_group", "id": "c807d0b4-0dd0-43fe-a7c4-d29bb61f389e", "language": "en", "last_updated": "Fri, 16 May 2008 00:00:00 GMT", "license": { "full_name": "Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported", "id": "CC BY-NC-SA 3.0", "info_url": "https://creativecommons.org/licenses/by-nc-sa/3.0/" }, "popularity": 0, "source": "BBC", "source_url": "http://www.bbc.co.uk/music/reviews/vh54", "text": "TEXT CONTENT OF REVIEW", "rating": 5, "user": { "created": "Wed, 07 May 2014 16:20:47 GMT", "display_name": "Jenny Nelson", "id": "3bf3fe0c-6db2-4746-bcf1-f39912113852", "karma": 0, "user_type": "Noob" }, "votes": { "positive": 0, "negative": 0 } } ] } :json uuid entity_id: UUID of the release group that is being reviewed :json string entity_type: One of the supported reviewable entities. 'release_group' or 'event' etc. **(optional)** :query user_id: user's UUID **(optional)** :query sort: ``popularity`` or ``published_on`` **(optional)** :query limit: results limit, min is 0, max is 50, default is 50 **(optional)** :query offset: result offset, default is 0 **(optional)** :query language: language code (ISO 639-1) **(optional)** :resheader Content-Type: *application/json* """ # TODO: This checking is added to keep old clients working and needs to be removed. release_group = Parser.uuid('uri', 'release_group', optional=True) if release_group: entity_id = release_group entity_type = 'release_group' else: entity_id = Parser.uuid('uri', 'entity_id', optional=True) entity_type = Parser.string('uri', 'entity_type', valid_values=ENTITY_TYPES, optional=True) user_id = Parser.uuid('uri', 'user_id', optional=True) sort = Parser.string( 'uri', 'sort', valid_values=['popularity', 'published_on', 'rating', 'created'], optional=True) # "rating" and "created" sort values are deprecated and but allowed here for backward compatibility if sort == 'created': sort = 'published_on' if sort == 'rating': sort = 'popularity' limit = Parser.int('uri', 'limit', min=1, max=50, optional=True) or 50 offset = Parser.int('uri', 'offset', optional=True) or 0 language = Parser.string('uri', 'language', min=2, max=3, optional=True) if language and language not in supported_languages: raise InvalidRequest(desc='Unsupported language') # TODO(roman): Ideally caching logic should live inside the model. Otherwise it # becomes hard to track all this stuff. cache_key = cache.gen_key('list', entity_id, user_id, sort, limit, offset, language) cached_result = cache.get(cache_key, REVIEW_CACHE_NAMESPACE) if cached_result: reviews = cached_result['reviews'] count = cached_result['count'] else: reviews, count = db_review.list_reviews( entity_id=entity_id, entity_type=entity_type, user_id=user_id, sort=sort, limit=limit, offset=offset, language=language, ) reviews = [db_review.to_dict(p) for p in reviews] cache.set(cache_key, { 'reviews': reviews, 'count': count, }, namespace=REVIEW_CACHE_NAMESPACE) return jsonify(limit=limit, offset=offset, count=count, reviews=reviews)
def review_list_handler(): """Get list of reviews. **Request Example:** .. code-block:: bash $ curl "https://critiquebrainz.org/ws/1/review/?limit=1&offset=50" \\ -X GET **Response Example:** .. code-block:: json { "count": 9197, "limit": 1, "offset": 50, "reviews": [ { "created": "Fri, 16 May 2008 00:00:00 GMT", "edits": 0, "entity_id": "09259937-6477-3959-8b10-af1cbaea8e6e", "entity_type": "release_group", "id": "c807d0b4-0dd0-43fe-a7c4-d29bb61f389e", "language": "en", "last_updated": "Fri, 16 May 2008 00:00:00 GMT", "license": { "full_name": "Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported", "id": "CC BY-NC-SA 3.0", "info_url": "https://creativecommons.org/licenses/by-nc-sa/3.0/" }, "popularity": 0, "source": "BBC", "source_url": "http://www.bbc.co.uk/music/reviews/vh54", "text": "TEXT CONTENT OF REVIEW", "rating": 5, "user": { "created": "Wed, 07 May 2014 16:20:47 GMT", "display_name": "Jenny Nelson", "id": "3bf3fe0c-6db2-4746-bcf1-f39912113852", "karma": 0, "user_type": "Noob" }, "votes": { "positive": 0, "negative": 0 } } ] } :json uuid entity_id: UUID of the release group that is being reviewed :json string entity_type: One of the supported reviewable entities. 'release_group' or 'event' etc. **(optional)** :query user_id: user's UUID **(optional)** :query sort: ``popularity`` or ``published_on`` **(optional)** :query limit: results limit, min is 0, max is 50, default is 50 **(optional)** :query offset: result offset, default is 0 **(optional)** :query language: language code (ISO 639-1) **(optional)** :resheader Content-Type: *application/json* """ # TODO: This checking is added to keep old clients working and needs to be removed. release_group = Parser.uuid('uri', 'release_group', optional=True) if release_group: entity_id = release_group entity_type = 'release_group' else: entity_id = Parser.uuid('uri', 'entity_id', optional=True) entity_type = Parser.string('uri', 'entity_type', valid_values=ENTITY_TYPES, optional=True) user_id = Parser.uuid('uri', 'user_id', optional=True) # TODO: "rating" sort value is deprecated and needs to be removed. sort = Parser.string('uri', 'sort', valid_values=['popularity', 'published_on', 'rating'], optional=True) if sort == 'rating': sort = 'popularity' limit = Parser.int('uri', 'limit', min=1, max=50, optional=True) or 50 offset = Parser.int('uri', 'offset', optional=True) or 0 language = Parser.string('uri', 'language', min=2, max=3, optional=True) if language and language not in supported_languages: raise InvalidRequest(desc='Unsupported language') # TODO(roman): Ideally caching logic should live inside the model. Otherwise it # becomes hard to track all this stuff. cache_key = cache.gen_key('list', entity_id, user_id, sort, limit, offset, language) cached_result = cache.get(cache_key, REVIEW_CACHE_NAMESPACE) if cached_result: reviews = cached_result['reviews'] count = cached_result['count'] else: reviews, count = db_review.list_reviews( entity_id=entity_id, entity_type=entity_type, user_id=user_id, sort=sort, limit=limit, offset=offset, language=language, ) reviews = [db_review.to_dict(p) for p in reviews] cache.set(cache_key, { 'reviews': reviews, 'count': count, }, namespace=REVIEW_CACHE_NAMESPACE) return jsonify(limit=limit, offset=offset, count=count, reviews=reviews)