Browse Source

downloaders.py: Use the utils.convert_item function

Use the utils.convert_item function in order to encode & decode
strings between 'str' and 'unicode' types instead of using a
separate method to do our job.
doc-issue-template
MrS0m30n3 7 years ago
parent
commit
d01307129c
1 changed files with 6 additions and 19 deletions
  1. 25
      youtube_dl_gui/downloaders.py

25
youtube_dl_gui/downloaders.py

@ -6,10 +6,6 @@
This module contains the actual downloaders responsible This module contains the actual downloaders responsible
for downloading the video files. for downloading the video files.
Note:
downloaders.py is part of the youtubedlg package but it can be used
as a stand alone module for downloading videos.
""" """
from __future__ import unicode_literals from __future__ import unicode_literals
@ -25,6 +21,8 @@ from time import sleep
from Queue import Queue from Queue import Queue
from threading import Thread from threading import Thread
from .utils import convert_item
class PipeReader(Thread): class PipeReader(Thread):
"""Helper class to avoid deadlocks when reading from subprocess pipes. """Helper class to avoid deadlocks when reading from subprocess pipes.
@ -132,7 +130,6 @@ class YoutubeDLDownloader(object):
self._return_code = self.OK self._return_code = self.OK
self._proc = None self._proc = None
self._encoding = self._get_encoding()
self._stderr_queue = Queue() self._stderr_queue = Queue()
self._stderr_reader = PipeReader(self._stderr_queue) self._stderr_reader = PipeReader(self._stderr_queue)
@ -166,7 +163,7 @@ class YoutubeDLDownloader(object):
while self._proc_is_alive(): while self._proc_is_alive():
stdout = self._proc.stdout.readline().rstrip() stdout = self._proc.stdout.readline().rstrip()
stdout = stdout.decode(self._encoding, 'ignore')
stdout = convert_item(stdout, to_unicode=True)
if stdout: if stdout:
data_dict = extract_data(stdout) data_dict = extract_data(stdout)
@ -177,7 +174,7 @@ class YoutubeDLDownloader(object):
# We don't need to read stderr in real time # We don't need to read stderr in real time
while not self._stderr_queue.empty(): while not self._stderr_queue.empty():
stderr = self._stderr_queue.get_nowait().rstrip() stderr = self._stderr_queue.get_nowait().rstrip()
stderr = stderr.decode(self._encoding, 'ignore')
stderr = convert_item(stderr, to_unicode=True)
self._log(stderr) self._log(stderr)
@ -317,16 +314,6 @@ class YoutubeDLDownloader(object):
return cmd return cmd
def _get_encoding(self):
"""Return system encoding. """
try:
encoding = locale.getpreferredencoding()
'TEST'.encode(encoding)
except:
encoding = 'UTF-8'
return encoding
def _create_process(self, cmd): def _create_process(self, cmd):
"""Create new subprocess. """Create new subprocess.
@ -351,7 +338,7 @@ class YoutubeDLDownloader(object):
# Encode command for subprocess # Encode command for subprocess
# Refer to http://stackoverflow.com/a/9951851/35070 # Refer to http://stackoverflow.com/a/9951851/35070
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
cmd = [item.encode(self._encoding, 'ignore') for item in cmd]
cmd = convert_item(cmd, to_unicode=False)
try: try:
self._proc = subprocess.Popen(cmd, self._proc = subprocess.Popen(cmd,
@ -361,7 +348,7 @@ class YoutubeDLDownloader(object):
startupinfo=info) startupinfo=info)
except (ValueError, OSError) as error: except (ValueError, OSError) as error:
self._log('Failed to start process: {}'.format(ucmd)) self._log('Failed to start process: {}'.format(ucmd))
self._log(str(error).decode(self._encoding, 'ignore'))
self._log(convert_item(str(error), to_unicode=True))
def extract_data(stdout): def extract_data(stdout):

Loading…
Cancel
Save