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.

109 lines
3.2 KiB

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