Example #1
0
 def _html_to_json(self, view_html, lecture_id):
     data = parse_json(
         search_regex(
             r'videojs-setup-data=(["\'])(?P<data>{.+?})\1',
             view_html,
             "setup data",
             default="{}",
             group="data",
         ),
         lecture_id,
         transform_source=unescapeHTML,
         fatal=False,
     )
     text_tracks = parse_json(
         search_regex(
             r'text-tracks=(["\'])(?P<data>\[.+?\])\1',
             view_html,
             "text tracks",
             default="{}",
             group="data",
         ),
         lecture_id,
         transform_source=lambda s: js_to_json(unescapeHTML(s)),
         fatal=False,
     )
     return data, text_tracks
Example #2
0
    def convert(self, filename=None, keep_vtt=False):
        if filename:
            seq = 1
            fname = filename.replace(".vtt", ".srt")
            content = self._vttcontents(fname=filename)
            if content and isinstance(content, list):
                timecode_loc = self._locate_timecode(content)
                if not timecode_loc.get("status"):
                    return {
                        "status": "False",
                        "msg": "subtitle file seems to have malfunction skipping conversion ..",
                    }
                for line in content[timecode_loc.get("location") :]:
                    flag = self._is_timecode(timecode=line)
                    if flag:
                        timecode = self._generate_timecode(seq, unescapeHTML(line))
                        self._write_srtcontent(fname, timecode)
                        seq += 1
                    if not flag:
                        match = re.match("^([0-9]{1,3})$", line, flags=re.U)
                        if not match:
                            data = "{content}\r\n".format(content=line)
                            self._write_srtcontent(fname, data)
            else:
                return content

            if not keep_vtt:
                try:
                    os.unlink(filename)
                except Exception:  # pylint: disable=W
                    pass
        return {"status": "True", "msg": "successfully generated subtitle in srt ..."}