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.

93 lines
2.7 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. Attributes:
  4. UPDATE_PUB_TOPIC (string): wxPublisher subscription topic of the
  5. UpdateThread thread.
  6. """
  7. import os.path
  8. from threading import Thread
  9. from urllib2 import urlopen, URLError, HTTPError
  10. from wx import CallAfter
  11. from wx.lib.pubsub import setuparg1
  12. from wx.lib.pubsub import pub as Publisher
  13. from .utils import (
  14. YOUTUBEDL_BIN,
  15. check_path
  16. )
  17. UPDATE_PUB_TOPIC = 'update'
  18. class UpdateThread(Thread):
  19. """Python Thread that downloads youtube-dl binary.
  20. Attributes:
  21. LATEST_YOUTUBE_DL (string): URL with the latest youtube-dl binary.
  22. DOWNLOAD_TIMEOUT (int): Download timeout in seconds.
  23. Args:
  24. download_path (string): Absolute path where UpdateThread will download
  25. the latest youtube-dl.
  26. quiet (boolean): If True UpdateThread won't send the finish signal
  27. back to the caller. Finish signal can be used to make sure that
  28. the UpdateThread has been completed in an asynchronous way.
  29. """
  30. LATEST_YOUTUBE_DL = 'https://yt-dl.org/latest/'
  31. DOWNLOAD_TIMEOUT = 20
  32. def __init__(self, download_path, quiet=False):
  33. super(UpdateThread, self).__init__()
  34. self.download_path = download_path
  35. self.quiet = quiet
  36. self.start()
  37. def run(self):
  38. self._talk_to_gui('download')
  39. source_file = self.LATEST_YOUTUBE_DL + YOUTUBEDL_BIN
  40. destination_file = os.path.join(self.download_path, YOUTUBEDL_BIN)
  41. check_path(self.download_path)
  42. try:
  43. stream = urlopen(source_file, timeout=self.DOWNLOAD_TIMEOUT)
  44. with open(destination_file, 'wb') as dest_file:
  45. dest_file.write(stream.read())
  46. self._talk_to_gui('correct')
  47. except (HTTPError, URLError, IOError) as error:
  48. self._talk_to_gui('error', str(error))
  49. if not self.quiet:
  50. self._talk_to_gui('finish')
  51. def _talk_to_gui(self, signal, data=None):
  52. """Communicate with the GUI using wxCallAfter and wxPublisher.
  53. Args:
  54. signal (string): Unique signal string that informs the GUI for the
  55. update process.
  56. data (string): Can be any string data to pass along with the
  57. given signal. Default is None.
  58. Note:
  59. UpdateThread supports 4 signals.
  60. 1) download: The update process started
  61. 2) correct: The update process completed successfully
  62. 3) error: An error occured while downloading youtube-dl binary
  63. 4) finish: The update thread is ready to join
  64. """
  65. CallAfter(Publisher.sendMessage, UPDATE_PUB_TOPIC, (signal, data))