|
| 1 | +# coding=utf-8 |
| 2 | +import logging |
| 3 | +import srt |
| 4 | + |
| 5 | +from flask_restx import Resource, Namespace, reqparse, fields, marshal |
| 6 | + |
| 7 | +from ..utils import authenticate |
| 8 | + |
| 9 | + |
| 10 | +api_ns_subtitle_contents = Namespace('Subtitle Contents', description='Retrieve contents of subtitle file') |
| 11 | + |
| 12 | + |
| 13 | +@api_ns_subtitle_contents.route('subtitles/contents') |
| 14 | +class SubtitleNameContents(Resource): |
| 15 | + get_request_parser = reqparse.RequestParser() |
| 16 | + get_request_parser.add_argument('subtitlePath', type=str, required=True, help='Subtitle filepath') |
| 17 | + |
| 18 | + time_modal = api_ns_subtitle_contents.model('time_modal', { |
| 19 | + 'hours': fields.Integer(), |
| 20 | + 'minutes': fields.Integer(), |
| 21 | + 'seconds': fields.Integer(), |
| 22 | + 'total_seconds': fields.Integer(), |
| 23 | + 'microseconds': fields.Integer(), |
| 24 | + }) |
| 25 | + |
| 26 | + get_response_model = api_ns_subtitle_contents.model('SubtitlesContentsGetResponse', { |
| 27 | + 'index': fields.Integer(), |
| 28 | + 'content': fields.String(), |
| 29 | + 'proprietary': fields.String(), |
| 30 | + 'start': fields.Nested(time_modal), |
| 31 | + 'end': fields.Nested(time_modal), |
| 32 | + # 'duration': fields.Nested(time_modal), |
| 33 | + }) |
| 34 | + |
| 35 | + @authenticate |
| 36 | + @api_ns_subtitle_contents.response(200, 'Success') |
| 37 | + @api_ns_subtitle_contents.response(401, 'Not Authenticated') |
| 38 | + @api_ns_subtitle_contents.doc(parser=get_request_parser) |
| 39 | + def get(self): |
| 40 | + """Retrieve subtitle file contents""" |
| 41 | + |
| 42 | + args = self.get_request_parser.parse_args() |
| 43 | + path = args.get('subtitlePath') |
| 44 | + |
| 45 | + results = [] |
| 46 | + |
| 47 | + # Load the SRT content |
| 48 | + with open(path, "r", encoding="utf-8") as f: |
| 49 | + file_content = f.read() |
| 50 | + |
| 51 | + # Map contents |
| 52 | + for sub in srt.parse(file_content): |
| 53 | + |
| 54 | + start_total_seconds = int(sub.start.total_seconds()) |
| 55 | + end_total_seconds = int(sub.end.total_seconds()) |
| 56 | + duration_timedelta = sub.end - sub.start |
| 57 | + |
| 58 | + results.append(dict( |
| 59 | + index=sub.index, |
| 60 | + content=sub.content, |
| 61 | + proprietary=sub.proprietary, |
| 62 | + start=dict( |
| 63 | + hours = start_total_seconds // 3600, |
| 64 | + minutes = (start_total_seconds % 3600) // 60, |
| 65 | + seconds = start_total_seconds % 60, |
| 66 | + total_seconds=int(sub.start.total_seconds()), |
| 67 | + microseconds = sub.start.microseconds |
| 68 | + ), |
| 69 | + end=dict( |
| 70 | + hours = end_total_seconds // 3600, |
| 71 | + minutes = (end_total_seconds % 3600) // 60, |
| 72 | + seconds = end_total_seconds % 60, |
| 73 | + total_seconds=int(sub.end.total_seconds()), |
| 74 | + microseconds = sub.end.microseconds |
| 75 | + ), |
| 76 | + # duration=dict( |
| 77 | + # seconds=int(duration_timedelta.total_seconds()), |
| 78 | + # microseconds=duration_timedelta.microseconds |
| 79 | + # ), |
| 80 | + )) |
| 81 | + |
| 82 | + return marshal(results, self.get_response_model, envelope='data') |
0 commit comments