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.

65 lines
1.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
  1. #!/usr/bin/env python2
  2. ''' Youtube-dlG module to download youtube-dl. '''
  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. '''
  15. Download latest youtube-dl.
  16. Params
  17. download_path: Absolute path where UpdateThread
  18. should download the latest youtube-dl.
  19. quiet: If True UpdateThread won't send any messages back to caller.
  20. '''
  21. LATEST_YOUTUBE_DL = 'https://yt-dl.org/latest/'
  22. PUBLISHER_TOPIC = 'update'
  23. DOWNLOAD_TIMEOUT = 20
  24. def __init__(self, download_path, quiet):
  25. super(UpdateThread, self).__init__()
  26. self.download_path = download_path
  27. self.quiet = quiet
  28. self.start()
  29. def run(self):
  30. self._callafter("Downloading latest youtube-dl. Please wait...")
  31. source_file = self.LATEST_YOUTUBE_DL + YOUTUBEDL_BIN
  32. destination_file = os.path.join(self.download_path, YOUTUBEDL_BIN)
  33. check_path(self.download_path)
  34. try:
  35. stream = urlopen(source_file, timeout=self.DOWNLOAD_TIMEOUT)
  36. with open(destination_file, 'wb') as dest_file:
  37. dest_file.write(stream.read())
  38. msg = 'Youtube-dl downloaded correctly'
  39. except (HTTPError, URLError, IOError) as e:
  40. msg = 'Youtube-dl download failed ' + str(e)
  41. self._callafter(msg)
  42. if not self.quiet:
  43. self._callafter('finish')
  44. def _callafter(self, data):
  45. ''' CallAfter wrapper. '''
  46. CallAfter(Publisher.sendMessage, self.PUBLISHER_TOPIC, data)