Skip to content

Commit

Permalink
Merge pull request #143 from lahmacunradio/dev/api_checks-and-swagger…
Browse files Browse the repository at this point in the history
…_update

Dev/api checks and swagger update
  • Loading branch information
tuz666 authored Apr 6, 2022
2 parents 96ceded + 0741615 commit 35527e6
Show file tree
Hide file tree
Showing 5 changed files with 611 additions and 264 deletions.
57 changes: 43 additions & 14 deletions arcsi/api/item.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime, timedelta
import json
import os
import requests
Expand All @@ -9,6 +10,7 @@
from flask import flash, jsonify, make_response, request, send_file, url_for, redirect
from flask import current_app as app
from marshmallow import fields, post_load, Schema, ValidationError
from sqlalchemy import func

from uuid import uuid4

Expand All @@ -17,10 +19,11 @@
broadcast_audio,
dict_to_obj,
media_path,
normalise,
save_file,
item_duplications_number
)
from arcsi.api import arcsi
from . import arcsi
from arcsi.handler.upload import DoArchive
from arcsi.model import db
from arcsi.model.item import Item
Expand All @@ -33,6 +36,7 @@ class ItemDetailsSchema(Schema):
required=True
) # TODO value can't be 0 -- reserved for show itself
name = fields.Str(required=True, min=1)
name_slug = fields.Str(dump_only=True)
description = fields.Str()
language = fields.Str(max=5)
play_date = fields.Date(required=True)
Expand All @@ -51,7 +55,7 @@ class ItemDetailsSchema(Schema):
shows = fields.List(
fields.Nested(
"ShowDetailsSchema",
only=("id", "name"),
only=("id", "name", "archive_lahmastore_base_url"),
),
required=True,
)
Expand All @@ -62,14 +66,14 @@ def make_item(self, data, **kwargs):


item_schema = ItemDetailsSchema()
item_archive_schema = ItemDetailsSchema(only = ("name", "number", "play_date", "language",
"description", "image_url", "play_file_name", "download_count"))
item_partial_schema = ItemDetailsSchema(partial=True,)
items_schema = ItemDetailsSchema(many=True)
items_archive_schema = ItemDetailsSchema(many=True,
only=("name", "description",
"play_date", "play_file_name",
"image_url", "download_count"))
item_archive_schema = ItemDetailsSchema(
only = ("id", "number", "name", "name_slug", "description", "language", "play_date",
"image_url", "play_file_name", "archived", "download_count", "shows"))
item_partial_schema = ItemDetailsSchema(partial = True,)
items_schema = ItemDetailsSchema(many = True)
items_archive_schema = ItemDetailsSchema(many = True,
only = ("id", "number", "name", "name_slug", "description", "language", "play_date",
"image_url", "play_file_name", "archived", "download_count", "shows"))

headers = {"Content-Type": "application/json"}

Expand All @@ -84,20 +88,25 @@ def list_items():
item.image_url = do.download(
item.shows[0].archive_lahmastore_base_url, item.image_url
)
item.name_slug=normalise(item.name)
return items_schema.dumps(items)

@arcsi.route("/item/latest/", methods=["GET"])

@arcsi.route("/item/latest", methods=["GET"])
def list_items_latest():
do = DoArchive()
page = request.args.get('page', 1, type=int)
size = request.args.get('size', 12, type=int)
items = Item.query.order_by(Item.play_date.desc()).paginate(
page, size, False)
items = Item.query.filter(Item.play_date < datetime.today() - timedelta(days=1)
).filter(Item.archived == True
).order_by(Item.play_date.desc()
).paginate(page, size, False)
for item in items.items:
if item.image_url:
item.image_url = do.download(
item.shows[0].archive_lahmastore_base_url, item.image_url
)
item.name_slug=normalise(item.name)
return items_archive_schema.dumps(items.items)


Expand All @@ -111,6 +120,7 @@ def view_item(id):
item.image_url = do.download(
item.shows[0].archive_lahmastore_base_url, item.image_url
)
item.name_slug=normalise(item.name)
return item_schema.dump(item)
else:
return make_response("Item not found", 404, headers)
Expand Down Expand Up @@ -454,4 +464,23 @@ def edit_item(id):
return make_response(
jsonify(item_partial_schema.dump(item)), 200, headers
)
return "Some error happened, check server logs for details. Note that your media may have been uploaded (to DO and/or Azurcast)."
return "Some error happened, check server logs for details. Note that your media may have been uploaded (to DO and/or Azurcast)."


@arcsi.route("/item/search", methods=["GET"])
def search_item():
do = DoArchive()
page = request.args.get('page', 1, type=int)
size = request.args.get('size', 12, type=int)
param = request.args.get('param', "", type=str)
items = Item.query.filter(func.lower(Item.name).contains(func.lower(param)) |
func.lower(Item.description).contains(func.lower(param))
).filter(Item.play_date < datetime.today() - timedelta(days=1)
).order_by(Item.play_date.desc()).paginate(page, size, False)
for item in items.items:
if item.image_url:
item.image_url = do.download(
item.shows[0].archive_lahmastore_base_url, item.image_url
)
item.name_slug=normalise(item.name)
return items_schema.dumps(items.items)
141 changes: 112 additions & 29 deletions arcsi/api/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
from flask import flash, jsonify, make_response, request, url_for
from flask import current_app as app
from marshmallow import fields, post_load, Schema, ValidationError
from sqlalchemy import false, func
from werkzeug import secure_filename

from .utils import archive, get_shows, save_file, slug, sort_for
from arcsi.api import arcsi
from .utils import archive, get_shows, save_file, slug, sort_for, normalise
from . import arcsi
from arcsi.handler.upload import DoArchive
from arcsi.model import db
from arcsi.model.show import Show
Expand All @@ -20,7 +21,6 @@

class ShowDetailsSchema(Schema):
id = fields.Int()
name = fields.Str(required=True)
active = fields.Boolean(required=True)
name = fields.Str(required=True)
description = fields.Str(required=True)
Expand All @@ -44,6 +44,7 @@ class ShowDetailsSchema(Schema):
"description",
"number",
"name",
"name_slug",
"play_file_name",
"play_date",
"image_url",
Expand All @@ -66,25 +67,20 @@ def make_show(self, data, **kwargs):
return Show(**data)


show_schema = ShowDetailsSchema(only=("id", "name", "active", "description",
"cover_image_url", "playlist_name", "items",
"language", "frequency", "week", "day", "start",
"end", "archive_lahmastore", "archive_lahmastore_base_url",
"archive_mixcloud", "users"))
show_archive_schema = ShowDetailsSchema(only=("name", "cover_image_url",
"day", "start", "end",
"frequency", "language",
"active", "description",
"items"))
show_schema = ShowDetailsSchema()
show_archive_schema = ShowDetailsSchema(only=("id", "active", "name", "description", "cover_image_url",
"day", "start", "end", "frequency", "language",
"playlist_name", "archive_lahmastore_base_url", "items"))
show_partial_schema = ShowDetailsSchema(partial=True)
shows_schema = ShowDetailsSchema(many=True)
shows_schedule_schema = ShowDetailsSchema(many=True,
only=("active", "name", "cover_image_url",
"day", "start", "end",
"description", "archive_lahmastore_base_url"))
shows_schedule_schema = ShowDetailsSchema(many=True, exclude=("items",))
shows_schedule_by_schema = ShowDetailsSchema(many=True,
only=("id", "active", "name", "description", "cover_image_url",
"day", "start", "end", "frequency", "language",
"playlist_name", "archive_lahmastore_base_url", "items"))
shows_archive_schema = ShowDetailsSchema(many=True,
only=("active", "name", "cover_image_url",
"description", "archive_lahmastore_base_url"))
only=("id", "active", "name", "description", "cover_image_url",
"playlist_name", "archive_lahmastore_base_url"))

headers = {"Content-Type": "application/json"}

Expand All @@ -95,15 +91,60 @@ def make_show(self, data, **kwargs):
def list_shows():
return shows_schema.dumps(get_shows())


@arcsi.route("/show/all_without_items", methods=["GET"])
def list_shows_without_items():
return shows_schedule_schema.dumps(get_shows())

@arcsi.route("/show/schedule", methods=["GET"])
def list_shows_for_schedule():
return shows_schedule_schema.dumps(get_shows())
do = DoArchive()
shows = Show.query.filter(Show.active == True).all()
for show in shows:
if show.cover_image_url:
show.cover_image_url = do.download(
show.archive_lahmastore_base_url, show.cover_image_url
)
return shows_schedule_schema.dumps(shows)

@arcsi.route("/show/schedule_by", methods=["GET"])
def list_shows_for_schedule_by():
do = DoArchive()
day = request.args.get('day', 1, type=int)
shows = Show.query.filter(Show.day == day and Show.active == True).all()
shows_json = shows_schedule_by_schema.dump(shows)
# iterate through shows
for show_json in shows_json:
if show_json["cover_image_url"]:
show_json["cover_image_url"] = do.download(
show_json["archive_lahmastore_base_url"], show_json["cover_image_url"]
)
if show_json["items"]:
latest_item_found = False
# iterate through show's items
for item in show_json["items"]:
# search for the first one which is archived & already aired
if (latest_item_found == False and
item["archived"] == True and
((datetime.strptime(item["play_date"], "%Y-%m-%d") + timedelta(days=1)) < datetime.today())):
latest_item_found = True
item["image_url"] = do.download(
show_json["archive_lahmastore_base_url"], item["image_url"]
)
item["name_slug"] = normalise(item["name"])
show_json["items"] = item
# if there is no archived show return empty array
if (latest_item_found == False):
show_json["items"] = []
return json.dumps(shows_json)


# We are gonna use this on the new page as the show/all
@arcsi.route("/show/list", methods=["GET"])
def list_shows_page():
return shows_archive_schema.dumps(get_shows())


# TODO /item/<uuid>/add route so that each upload has unique id to begin with
# no need for different methods for `POST` & `PUT`
@arcsi.route("/show/add", methods=["POST"])
Expand Down Expand Up @@ -261,6 +302,7 @@ def edit_show(id):
jsonify(show_partial_schema.dump(show)), 200, headers
)


@arcsi.route("show/<id>", methods=["GET"])
def view_show(id):
do = DoArchive()
Expand All @@ -276,11 +318,14 @@ def view_show(id):
serial_show = show_schema.dump(show)
date_desc_episodes = sort_for(serial_show["items"], "play_date", "desc")
serial_show["items"] = date_desc_episodes
for item in serial_show["items"]:
item["name_slug"] = normalise(item["name"])

return serial_show
else:
return make_response("Show not found", 404, headers)


# We use this route on the legacy front-end show page
@arcsi.route("show/<string:show_slug>/archive", methods=["GET"])
def view_show_archive(show_slug):
Expand All @@ -300,6 +345,7 @@ def view_show_archive(show_slug):
show_item["image_url"] = do.download(
show.archive_lahmastore_base_url, show_item["image_url"]
)
show_item["name_slug"]=normalise(show_item["name"])
return json.dumps(show_items)
else:
return make_response("Show not found", 404, headers)
Expand All @@ -313,23 +359,60 @@ def view_show_archive(show_slug):
# This will be the one that we are gonna use at the new page
@arcsi.route("show/<string:show_slug>/page", methods=["GET"])
def view_show_page(show_slug):
do = DoArchive()
show_query = Show.query.filter_by(archive_lahmastore_base_url=show_slug)
show = show_query.first()
if show:
# subquery = session.query(Item.id).filter(blabla -timedelta(day=1)).all().subquery()
# query = session.query(Show).filter_by(blabla).(Item.id.in_(subquery))
show.items.filter(Item.play_date < datetime.today() - timedelta(days=1)).all()
return show_archive_schema.dump(show)
if show.cover_image_url:
show.cover_image_url = do.download(
show.archive_lahmastore_base_url, show.cover_image_url
)
serial_show = show_archive_schema.dump(show)
show_items = [
show_item
for show_item in serial_show["items"]
if datetime.strptime(show_item.get("play_date"), "%Y-%m-%d")
+ timedelta(days=1)
< datetime.today()
]
serial_show["items"]=show_items
for item in serial_show["items"]:
item["image_url"] = do.download(
show.archive_lahmastore_base_url, item["image_url"]
)
item["name_slug"]=normalise(item["name"])
return serial_show
else:
return make_response("Show not found", 404, headers)


@arcsi.route("show/<string:show_slug>/episode/<string:episode_slug>", methods=["GET"])
def view_episode_archive(show_slug, episode_slug):
episode_slug = episode_slug + ".mp3"
@arcsi.route("show/<string:show_slug>/item/<string:item_slug>", methods=["GET"])
def view_episode_archive(show_slug, item_slug):
do = DoArchive()
show_query = Show.query.filter_by(archive_lahmastore_base_url=show_slug)
show = show_query.first_or_404()
for i in show.items:
if i.play_file_name == episode_slug:
return item_archive_schema.dump(i)
return make_response("Episode not found", 404, headers)
for item in show.items:
if (normalise(item.name) == item_slug):
item.image_url = do.download(
show.archive_lahmastore_base_url, item.image_url
)
item.name_slug=item_slug
return item_archive_schema.dump(item)
return make_response("Episode not found", 404, headers)


@arcsi.route("/show/search", methods=["GET"])
def search_show():
do = DoArchive()
page = request.args.get('page', 1, type=int)
size = request.args.get('size', 12, type=int)
param = request.args.get('param', "", type=str)
shows = Show.query.filter(func.lower(Show.name).contains(func.lower(param)) | func.lower(Show.description).contains(func.lower(param))).paginate(page, size, False)
for show in shows.items:
if show.cover_image_url:
show.cover_image_url = do.download(
show.archive_lahmastore_base_url, show.cover_image_url
)
return shows_schedule_schema.dumps(shows.items)
1 change: 1 addition & 0 deletions arcsi/model/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Show(db.Model):
"Item",
secondary=items_shows,
backref=db.backref("shows"),
order_by="Item.play_date.desc()",
lazy="dynamic",
# TODO why is this not working? see item.shows assignment in api/item
cascade_backrefs=False,
Expand Down
Loading

0 comments on commit 35527e6

Please sign in to comment.