Skip to content
Snippets Groups Projects
Select Git revision
  • 1fb4c15d368c5f44ee9c00266426f7d008842e1b
  • master default protected
2 results

Program.cs

Blame
  • modify_comment_segment.py 3.40 KiB
    import elasticsearch
    
    from fastapi import Response
    from fastapi.responses import JSONResponse
    
    from pydantic import BaseModel
    
    from app.elastic_search import AUDIO_TAPES_INDEX, COMMENT_SEGMENTS_INDEX
    from app.elastic_search import get_elasticsearch_client
    from app.routers import OrchiveAPIRouter
    
    
    router = OrchiveAPIRouter()
    
    
    class CommentSegmentInput(BaseModel):
        channel: str
        start: int
        end: int
        transcript: str
        words: str
    
    
    @router.post("/years/{year}/tapes/{tape_name}/comment")
    async def add_comment(year: int, tape_name: str, comment: CommentSegmentInput):
        """Inserts a comment segment into the database."""
        es = get_elasticsearch_client()
        result = es.search(index=AUDIO_TAPES_INDEX, body={
            "query": {
                "bool": {
                    "must": [
                        {"match": {"name": tape_name}},
                        {"match": {"year": year}}
                    ]
                }
            }
        })
    
        if result["hits"]["total"]["value"] == 0:
            return JSONResponse(status_code=404, content={
                "message": "Tape with name '{}' in year {} not found.".format(tape_name, year)
            })
    
        tape = result["hits"]["hits"][0]
    
        es.index(index=COMMENT_SEGMENTS_INDEX, body={
            **vars(comment),
            "audio_tape_ref": tape["_id"],
            "search_timeline_start": str(tape["_source"]["year"]) + "-01-01",
            "search_timeline_end": str(tape["_source"]["year"]) + "-12-31"
        })
    
        return JSONResponse(status_code=201, content={
            "message": "Comment inserted."
        })
    
    
    class CommentSegmentReplace(BaseModel):
        channel: str
        start: int
        end: int
        transcript: str
        words: str
    
    
    @router.put("/tape/comment_segment/{comment_segment_id}")
    async def replace_comment_segment(comment_segment_id: str, comment_segment_replace: CommentSegmentReplace):
        """Replaces a comment segment."""
        es = get_elasticsearch_client()
    
        try:
            result = es.update(index=COMMENT_SEGMENTS_INDEX, id=comment_segment_id, body={
                "doc": {
                    "channel": comment_segment_replace.channel,
                    "start": comment_segment_replace.start,
                    "end": comment_segment_replace.end,
                    "transcript": comment_segment_replace.transcript,
                    "words": comment_segment_replace.words,
                }
            })
        except elasticsearch.exceptions.NotFoundError:
            return JSONResponse(status_code=404, content={
                "message": "The comment segment has not been found."
            })
    
        if result['result'] == 'updated' or result['result'] == 'noop':
            return Response(status_code=204)
        else:
            return JSONResponse(status_code=400, content={
                "message": "Failed at replacing comment segment."
            })
    
    
    @router.delete("/tape/comment_segment/{comment_segment_id}")
    async def delete_comment_segment(comment_segment_id: str):
        """Deletes a comment segment from the database."""
        es = get_elasticsearch_client()
    
        try:
            result = es.delete(index=COMMENT_SEGMENTS_INDEX, id=comment_segment_id)
        except elasticsearch.exceptions.NotFoundError:
            return JSONResponse(status_code=404, content={
                "message": "The comment segment has not been found."
            })
    
        if result['result'] == 'deleted':
            return Response(status_code=204)
        else:
            return JSONResponse(status_code=500, content={
                "message": "An internal server error occurred."
            })