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.

94 lines
2.8 KiB

  1. #! /usr/bin/env python
  2. class DownloadHandler():
  3. def __init__(self, ListCtrl):
  4. self.ListCtrl = ListCtrl
  5. self.finished = False
  6. self.close = False
  7. self.handlers = []
  8. def _has_closed(self):
  9. return self.close
  10. def _has_finished(self):
  11. return self.finished
  12. def handle(self, msg):
  13. ''' Handles msg base to Signals.txt '''
  14. data = msg.data
  15. index = self.get_index(data)
  16. if index == -1:
  17. if data[0] == 'finish':
  18. self.finished = True
  19. elif data[0] == 'close':
  20. self.close = True
  21. else:
  22. ''' Manage handlers for its index '''
  23. if index == len(self.handlers):
  24. ''' Create new IndexDownloadHandler and add it to handlers '''
  25. self.handlers.append(IndexDownloadHandler(self.ListCtrl, index))
  26. ''' Let IndexDownloadHandler handle message data for current index '''
  27. self.handlers[index].handle(data)
  28. def get_index(self, data):
  29. return data.pop()
  30. class IndexDownloadHandler():
  31. def __init__(self, ListCtrl, index):
  32. self.ListCtrl = ListCtrl
  33. self.index = index
  34. self.info = ''
  35. def handle(self, data):
  36. ''' Handle its data message for current index '''
  37. if data[0] == 'finish':
  38. self.finish()
  39. elif data[0] == 'close':
  40. self.close()
  41. elif data[0] == 'error':
  42. self.error()
  43. elif data[0] == 'playlist':
  44. self.playlist(data)
  45. elif data[0] == '[youtube]':
  46. self.pre_proc()
  47. elif data[0] == '[download]':
  48. self.download(data)
  49. elif data[0] == '[ffmpeg]':
  50. self.post_proc()
  51. def finish(self):
  52. self.ListCtrl._write_data(self.index, 4, '')
  53. self.ListCtrl._write_data(self.index, 5, 'Finished')
  54. def close(self):
  55. self.ListCtrl._write_data(self.index, 3, '')
  56. self.ListCtrl._write_data(self.index, 4, '')
  57. self.ListCtrl._write_data(self.index, 5, 'Stopped')
  58. def error(self):
  59. self.ListCtrl._write_data(self.index, 3, '')
  60. self.ListCtrl._write_data(self.index, 4, '')
  61. self.ListCtrl._write_data(self.index, 5, 'Error')
  62. def pre_proc(self):
  63. self.ListCtrl._write_data(self.index, 5, 'Pre-Processing %s' % self.info)
  64. def post_proc(self):
  65. self.ListCtrl._write_data(self.index, 4, '')
  66. self.ListCtrl._write_data(self.index, 5, 'Converting to Audio %s' % self.info)
  67. def download(self, data):
  68. self.ListCtrl._write_data(self.index, 1, data[3])
  69. self.ListCtrl._write_data(self.index, 2, data[1])
  70. self.ListCtrl._write_data(self.index, 3, data[7])
  71. self.ListCtrl._write_data(self.index, 4, data[5])
  72. self.ListCtrl._write_data(self.index, 5, 'Downloading %s' % self.info)
  73. def playlist(self, data):
  74. self.ListCtrl._write_data(self.index, 1, '')
  75. self.ListCtrl._write_data(self.index, 2, '')
  76. self.ListCtrl._write_data(self.index, 3, '')
  77. self.ListCtrl._write_data(self.index, 4, '')
  78. self.info = '%s/%s' % (data[1], data[2])