You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.4 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. #!/usr/bin/env python2
  2. """Youtubedlg module to update youtube-dl binary. """
  3. import os.path
  4. from threading import Thread
  5. from urllib2 import urlopen, URLError, HTTPError
  6. from wx import CallAfter
  7. from wx.lib.pubsub import setuparg1
  8. from wx.lib.pubsub import pub as Publisher
  9. from .utils import (
  10. YOUTUBEDL_BIN,
  11. check_path
  12. )
  13. class UpdateThread(Thread):
  14. """Python Thread that downloads youtube-dl binary.
  15. Attributes:
  16. LATEST_YOUTUBE_DL (string): URL with the latest youtube-dl binary.
  17. PUBLISHER_TOPIC (string): Subscription topic for the wx Publisher.
  18. DOWNLOAD_TIMEOUT (int): Download timeout in seconds.
  19. Args:
  20. download_path (string): Absolute path where UpdateThread will download
  21. the latest youtube-dl.
  22. quiet (boolean): If True UpdateThread won't send the finish signal
  23. back to the caller. Finish signal can be used to make sure that
  24. UpdateThread has been terminated in an asynchronous way.
  25. """
  26. LATEST_YOUTUBE_DL = 'https://yt-dl.org/latest/'
  27. PUBLISHER_TOPIC = 'update'
  28. DOWNLOAD_TIMEOUT = 20
  29. def __init__(self, download_path, quiet=False):
  30. super(UpdateThread, self).__init__()
  31. self.download_path = download_path
  32. self.quiet = quiet
  33. self.start()
  34. def run(self):
  35. self._talk_to_gui("Downloading latest youtube-dl. Please wait...")
  36. source_file = self.LATEST_YOUTUBE_DL + YOUTUBEDL_BIN
  37. destination_file = os.path.join(self.download_path, YOUTUBEDL_BIN)
  38. check_path(self.download_path)
  39. try:
  40. stream = urlopen(source_file, timeout=self.DOWNLOAD_TIMEOUT)
  41. with open(destination_file, 'wb') as dest_file:
  42. dest_file.write(stream.read())
  43. msg = 'Youtube-dl downloaded correctly'
  44. except (HTTPError, URLError, IOError) as e:
  45. msg = 'Youtube-dl download failed ' + str(e)
  46. self._talk_to_gui(msg)
  47. if not self.quiet:
  48. self._talk_to_gui('finish')
  49. def _talk_to_gui(self, data):
  50. """Send data back to the GUI using wx CallAfter and wx Publisher.
  51. Args:
  52. data (string): Can be either a message that informs for the
  53. update process or a 'finish' signal that shows that the
  54. update process has been completed.
  55. """
  56. CallAfter(Publisher.sendMessage, self.PUBLISHER_TOPIC, data)