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.

51 lines
1.4 KiB

10 years ago
10 years ago
10 years ago
10 years ago
  1. #! /usr/bin/env python
  2. from wx import CallAfter
  3. from wx.lib.pubsub import setuparg1
  4. from wx.lib.pubsub import pub as Publisher
  5. from threading import Thread
  6. from urllib2 import urlopen, URLError, HTTPError
  7. from .Utils import (
  8. get_youtubedl_filename,
  9. check_path,
  10. fix_path
  11. )
  12. class UpdateThread(Thread):
  13. LATEST_YOUTUBE_DL = 'https://yt-dl.org/latest/'
  14. PUBLISHER_TOPIC = 'update'
  15. DOWNLOAD_TIMEOUT = 20
  16. def __init__(self, download_path):
  17. super(UpdateThread, self).__init__()
  18. self.download_path = fix_path(download_path)
  19. self._youtubedl_file = get_youtubedl_filename()
  20. self.start()
  21. def run(self):
  22. self._callafter("Downloading latest youtube-dl. Please wait...")
  23. dl_url = self.LATEST_YOUTUBE_DL + self._youtubedl_file
  24. dst_file = self.download_path + self._youtubedl_file
  25. check_path(self.download_path)
  26. try:
  27. f = urlopen(dl_url, timeout=self.DOWNLOAD_TIMEOUT)
  28. with open(dst_file, 'wb') as bf:
  29. bf.write(f.read())
  30. msg = 'Youtube-dl downloaded correctly'
  31. except (HTTPError, URLError, IOError) as e:
  32. msg = 'Youtube-dl download failed ' + str(e)
  33. self._callafter(msg)
  34. self._callafter('finish')
  35. def _callafter(self, data):
  36. CallAfter(Publisher.sendMessage, self.PUBLISHER_TOPIC, data)