Skip to content
This repository has been archived by the owner on Aug 1, 2021. It is now read-only.

Commit

Permalink
fixed #60, UnicodeEncodeError: 'charmap' codec can't encode character
Browse files Browse the repository at this point in the history
  • Loading branch information
r0oth3x49 committed Dec 28, 2017
1 parent c35e66f commit 20c9084
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 15 deletions.
1 change: 1 addition & 0 deletions udemy/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"re",
"os",
"sys",
"pyver",
"compat_str",
"login_popup",
"compat_urllib",
Expand Down
85 changes: 70 additions & 15 deletions udemy/_utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#!/usr/bin/python


from pprint import pprint
from . import __author__
from . import __version__
from ._compat import (
os,
re,
sys,
json,
pyver,
NO_DEFAULT,
compat_HTMLParser,
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(':')
Expand All @@ -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

0 comments on commit 20c9084

Please sign in to comment.