diff --git a/sarsoo/api/__init__.py b/sarsoo/api/__init__.py new file mode 100644 index 0000000..2783394 --- /dev/null +++ b/sarsoo/api/__init__.py @@ -0,0 +1 @@ +from .art_api import art_api_print diff --git a/sarsoo/api/art_api.py b/sarsoo/api/art_api.py new file mode 100644 index 0000000..a834cfa --- /dev/null +++ b/sarsoo/api/art_api.py @@ -0,0 +1,57 @@ +from flask import Blueprint, jsonify, abort +from sarsoo.art import db +from sarsoo.art.art import getAssetUrl + +art_api_print = Blueprint('artapi', __name__) + + +@art_api_print.route('/', methods=['GET']) +def collections(): + + tagdicts = db.getTagDicts() + + collections = [] + + for dict in tagdicts: + collections.append({ + 'name': dict['name'], + 'id': dict['doc_name'], + 'description': dict['description'], + 'index': dict['index'], + 'splash_url': getAssetUrl(dict['splash']['file_name']) + }) + + response = {'collections': collections} + + return jsonify(response) + + +@art_api_print.route('/', methods=['GET']) +def getcollection(id): + + try: + tagdict = db.getPopulatedTagDict(id) + except TypeError as e: + abort(404) + + artlist = [] + + for image in tagdict['images']: + artlist.append({ + 'file_url': getAssetUrl(image['file_name']), + 'date': image['date'].strftime('%d %B %y') + }) + + response = {'name': tagdict['name'], + 'description': tagdict['description'], + 'art': artlist} + + return jsonify(response) + + +@art_api_print.errorhandler(404) +def error400(error): + + errorresponse = {'error': 'collection not found'} + + return jsonify(errorresponse), 404 diff --git a/sarsoo/art/art.py b/sarsoo/art/art.py index 6875dc1..0260ddf 100644 --- a/sarsoo/art/art.py +++ b/sarsoo/art/art.py @@ -25,3 +25,7 @@ def tag_view(tag): def all(): sections = getPopulatedTagDicts() return render_template('art/all.html', staticroot = staticbucketurl, tags=sections) + + +def getAssetUrl(name): + return staticbucketurl + 'art/' + name + '.jpg' diff --git a/sarsoo/art/db.py b/sarsoo/art/db.py index e278d0a..2163c86 100644 --- a/sarsoo/art/db.py +++ b/sarsoo/art/db.py @@ -1,4 +1,5 @@ from google.cloud import firestore +from google.cloud import exceptions staticbucketurl = 'https://storage.googleapis.com/sarsooxyzstatic/' @@ -11,7 +12,7 @@ def getTagDicts(): try: tags = art_tags_collection.get() - except google.cloud.exceptions.NotFound: + except exceptions.NotFound: return 'no such document' dicts = list(map(lambda x: x.to_dict(), tags)) @@ -32,7 +33,7 @@ def getPopulatedTagDict(name): for image in tag_dicts['art']: image_list.append(image.get().to_dict()) - tag_dicts['images'] = sorted(image_list, key = lambda k: k['date'], reverse = True) + tag_dicts['images'] = sorted(image_list, key=lambda k: k['date'], reverse=True) return tag_dicts diff --git a/sarsoo/sarsoo.py b/sarsoo/sarsoo.py index 17b9851..ef5c574 100644 --- a/sarsoo/sarsoo.py +++ b/sarsoo/sarsoo.py @@ -4,6 +4,7 @@ import os from .art import art_print from .music import music_print +from .api import art_api_print # Project ID is determined by the GCLOUD_PROJECT environment variable db = firestore.Client() @@ -12,6 +13,7 @@ app = Flask(__name__, static_folder=os.path.join(os.path.dirname(__file__), '..' app.register_blueprint(art_print, url_prefix='/art') app.register_blueprint(music_print, url_prefix='/music') +app.register_blueprint(art_api_print, url_prefix='/api/art') staticbucketurl = 'https://storage.googleapis.com/sarsooxyzstatic/'