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.

67 lines
1.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
  1. #!/usr/bin/env python2
  2. ''' Youtube-dlG module to download youtube-dl. '''
  3. from threading import Thread
  4. from urllib2 import urlopen, URLError, HTTPError
  5. from wx import CallAfter
  6. from wx.lib.pubsub import setuparg1
  7. from wx.lib.pubsub import pub as Publisher
  8. from .utils import (
  9. get_youtubedl_filename,
  10. check_path,
  11. fix_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 = fix_path(download_path)
  27. self.quiet = quiet
  28. self.start()
  29. def run(self):
  30. self._callafter("Downloading latest youtube-dl. Please wait...")
  31. youtubedl = get_youtubedl_filename()
  32. source_file = self.LATEST_YOUTUBE_DL + youtubedl
  33. destination_file = self.download_path + youtubedl
  34. check_path(self.download_path)
  35. try:
  36. stream = urlopen(source_file, timeout=self.DOWNLOAD_TIMEOUT)
  37. with open(destination_file, 'wb') as dest_file:
  38. dest_file.write(stream.read())
  39. msg = 'Youtube-dl downloaded correctly'
  40. except (HTTPError, URLError, IOError) as e:
  41. msg = 'Youtube-dl download failed ' + str(e)
  42. self._callafter(msg)
  43. if not self.quiet:
  44. self._callafter('finish')
  45. def _callafter(self, data):
  46. ''' CallAfter wrapper. '''
  47. CallAfter(Publisher.sendMessage, self.PUBLISHER_TOPIC, data)