diff --git a/udemy/_compat.py b/udemy/_compat.py index eb6a7c7..9998a1a 100644 --- a/udemy/_compat.py +++ b/udemy/_compat.py @@ -76,6 +76,7 @@ "re", "os", "sys", + "pyver", "compat_str", "login_popup", "compat_urllib", diff --git a/udemy/_utils.py b/udemy/_utils.py index 3d0faeb..2165498 100644 --- a/udemy/_utils.py +++ b/udemy/_utils.py @@ -1,6 +1,6 @@ #!/usr/bin/python - +from pprint import pprint from . import __author__ from . import __version__ from ._compat import ( @@ -8,6 +8,7 @@ re, sys, json, + pyver, NO_DEFAULT, compat_HTMLParser, ) @@ -184,10 +185,58 @@ def fix_kv(m): '''.format(comment=COMMENT_RE, skip=SKIP_RE), fix_kv, code) class WEBVTT2SRT: - def _fix_subtitles(self, content): + + def _write_srtcontent(self, filename, _srtfilename, _srtcontent): + retVal = {} + if pyver == 3: + with open(_srtfilename, 'w', encoding='utf-8') as sub: + try: + sub.write('{}'.format(_srtcontent)) + except Exception as e: + retVal = {'status' : 'False', 'msg' : 'Python3 Exception : {}'.format(e)} + else: + retVal = {'status' : 'True', 'msg' : 'srt content written successfully'} + try: + os.unlink(filename) + except Exception as e: + pass + sub.close() + else: + with open(_srtfilename, 'w') as sub: + try: + sub.write('{}'.format(_srtcontent)) + except Exception as e: + retVal = {'status' : 'False', 'msg' : 'Python2 Exception : {}'.format(e)} + else: + retVal = {'status' : 'True', 'msg' : 'srt content written successfully'} + try: + os.unlink(filename) + except Exception as e: + pass + sub.close() + + return retVal + + def _get_index(self, content, flag=True): + if flag: + i = 0 + for line in content: + if '-->' in line: + index = i + break + i += 1 + return index + if not flag: + try: + index = content.index('1\r\n') if pyver == 2 else content.index(b'1\r\n') + except Exception as e: + index = 2 + return index + + def _fix_subtitles(self, content, index): _container = '' - for line in content[2:]: - if sys.version_info[:2] >= (3, 0): + for line in content[index:]: + if pyver == 3: _container += line.decode('utf-8', 'ignore') else: _container += line @@ -226,15 +275,21 @@ def convert(self, filename=None): else: content = [line for line in (l.decode('utf-8', 'ignore').strip() for l in f_in) if line] f_in.close() + try: + check = content.index('1') or content.index('1\r\n') + except: + check = 0 if len(content) > 4: if content[0] == 'WEBVTT' or content[0].endswith('WEBVTT') or 'WEBVTT' in content[0]: - if content[1] == '1': + if content[check] == '1': f = open(filename, 'rb') content = f.readlines() f.close() - _srtcontent = self._fix_subtitles(content) + index = self._get_index(content, flag=False) + _srtcontent = self._fix_subtitles(content, index) else: - for line in content[1:]: + index = self._get_index(content) + for line in content[index:]: if '-->' in line: _start, _end = line.split(' --> ') _stcode = _start.split(':') @@ -249,14 +304,14 @@ def convert(self, filename=None): _srtcontent += '{}\r\n{} --> {}\r\n{}\r\n\r\n'.format(_seqcounter, _appeartime, _disappertime, _textcontainer) if _srtcontent: - with open(_srtfilename, 'w') as sub: - sub.write('{}'.format(_srtcontent)) - sub.close() - try: - os.unlink(filename) - except Exception as e: - pass - _flag = {'status' : 'True', 'msg' : 'successfully generated subtitle in srt ...'} + retVal = self._write_srtcontent(filename, _srtfilename, _srtcontent) + if isinstance(retVal, dict) and len(retVal) > 0: + status = retVal.get('status') + msg = retVal.get('msg') + if status == 'True': + _flag = {'status' : 'True', 'msg' : 'successfully generated subtitle in srt ...'} + else: + _flag = {'status' : 'False', 'msg' : '{}'.format(msg)} else: _flag = {'status' : 'False', 'msg' : 'subtitle file seems to be empty skipping conversion from WEBVTT to SRT ..'} return _flag \ No newline at end of file