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.

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