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.

191 lines
5.9 KiB

  1. #! /usr/bin/env python
  2. from .Utils import (
  3. remove_empty_items,
  4. string_to_array,
  5. get_filename
  6. )
  7. class DownloadHandler():
  8. def __init__(self, ListCtrl):
  9. self.ListCtrl = ListCtrl
  10. self.finished = False
  11. self.closed = False
  12. self.error = False
  13. self.init_handlers()
  14. def init_handlers(self):
  15. ''' Initialise handlers '''
  16. self.handlers = [None for i in range(self.ListCtrl.ListIndex)]
  17. def _has_closed(self):
  18. return self.closed
  19. def _has_finished(self):
  20. return self.finished
  21. def _has_error(self):
  22. return self.error
  23. def _add_empty_handler(self):
  24. self.handlers.append(None)
  25. def handle(self, msg):
  26. ''' Handles msg base to Signals.txt '''
  27. pack = msg.data
  28. if pack.index == -1:
  29. self.global_handler(pack)
  30. else:
  31. self.index_handler(pack)
  32. def global_handler(self, pack):
  33. ''' Manage global index = -1 '''
  34. if pack.header == 'close':
  35. self.closed = True
  36. elif pack.header == 'finish':
  37. self.finished = True
  38. def index_handler(self, pack):
  39. ''' Manage handlers base on index '''
  40. if self.handlers[pack.index] == None:
  41. self.handlers[pack.index] = IndexDownloadHandler(self.ListCtrl, pack.index)
  42. self.handlers[pack.index].handle(pack)
  43. if self.handlers[pack.index].has_error():
  44. self.error = True
  45. class IndexDownloadHandler():
  46. def __init__(self, ListCtrl, index):
  47. self.ListCtrl = ListCtrl
  48. self.index = index
  49. self.err = False
  50. self.info = ''
  51. def has_error(self):
  52. return self.err
  53. def handle(self, pack):
  54. ''' Handle its pack for current index '''
  55. if pack.header == 'finish':
  56. self.finish()
  57. elif pack.header == 'close':
  58. self.close()
  59. elif pack.header == 'error':
  60. self.error()
  61. elif pack.header == 'playlist':
  62. self.playlist(pack.data)
  63. elif pack.header == 'youtube':
  64. self.pre_proc()
  65. elif pack.header == 'download':
  66. self.download(pack.data)
  67. elif pack.header == 'ffmpeg':
  68. self.post_proc()
  69. elif pack.header == 'remove':
  70. self.remove()
  71. elif pack.header == 'filename':
  72. self.filename(pack.data)
  73. def finish(self):
  74. self.ListCtrl._write_data(self.index, 4, '')
  75. self.ListCtrl._write_data(self.index, 5, 'Finished')
  76. def close(self):
  77. self.ListCtrl._write_data(self.index, 3, '')
  78. self.ListCtrl._write_data(self.index, 4, '')
  79. self.ListCtrl._write_data(self.index, 5, 'Stopped')
  80. def error(self):
  81. self.err = True
  82. self.ListCtrl._write_data(self.index, 3, '')
  83. self.ListCtrl._write_data(self.index, 4, '')
  84. self.ListCtrl._write_data(self.index, 5, 'Error')
  85. def pre_proc(self):
  86. self.ListCtrl._write_data(self.index, 5, 'Pre-Processing %s' % self.info)
  87. def post_proc(self):
  88. self.ListCtrl._write_data(self.index, 4, '')
  89. self.ListCtrl._write_data(self.index, 5, 'Post-Processing %s' % self.info)
  90. def download(self, data):
  91. self.ListCtrl._write_data(self.index, 1, data[0])
  92. self.ListCtrl._write_data(self.index, 2, data[1])
  93. self.ListCtrl._write_data(self.index, 3, data[2])
  94. self.ListCtrl._write_data(self.index, 4, data[3])
  95. self.ListCtrl._write_data(self.index, 5, 'Downloading %s' % self.info)
  96. def playlist(self, data):
  97. self.ListCtrl._write_data(self.index, 1, '')
  98. self.ListCtrl._write_data(self.index, 2, '')
  99. self.ListCtrl._write_data(self.index, 3, '')
  100. self.ListCtrl._write_data(self.index, 4, '')
  101. self.info = '%s/%s' % (data[0], data[1])
  102. def remove(self):
  103. self.ListCtrl._write_data(self.index, 5, 'Removing DASH Files')
  104. def filename(self, fl):
  105. self.ListCtrl._write_data(self.index, 0, get_filename(fl))
  106. class DataPack():
  107. def __init__(self, header, index=-1, data=None):
  108. self.header = header
  109. self.index = index
  110. self.data = data
  111. class OutputFormatter():
  112. def __init__(self, stdout):
  113. dataPack = None
  114. self.stdout = remove_empty_items(string_to_array(stdout))
  115. # extract header from stdout
  116. header = self.extract_header()
  117. # extract special headers filename, playlist
  118. header = self.set_filename_header(header)
  119. header = self.set_playlist_header(header)
  120. # extract data base on header
  121. data = self.extract_data(header)
  122. # extract special ignore header base on header, data
  123. header = self.set_ignore_header(header, data)
  124. # create data pack
  125. self.dataPack = DataPack(header, data=data)
  126. def extract_header(self):
  127. header = self.stdout.pop(0).replace('[', '').replace(']', '')
  128. return header
  129. def extract_data(self, header):
  130. ''' Extract data base on header '''
  131. if header == 'download':
  132. if '%' in self.stdout[0]:
  133. if self.stdout[0] != '100%':
  134. ''' size, percent, eta, speed '''
  135. return [self.stdout[2], self.stdout[0], self.stdout[6], self.stdout[4]]
  136. if header == 'playlist':
  137. return [self.stdout[2], self.stdout[4]]
  138. if header == 'filename':
  139. return ' '.join(self.stdout[1:])
  140. return None
  141. def set_filename_header(self, header):
  142. if header != 'ffmpeg':
  143. if self.stdout[0] == 'Destination:':
  144. return 'filename'
  145. return header
  146. def set_playlist_header(self, header):
  147. if header == 'download':
  148. if self.stdout[0] == 'Downloading' and self.stdout[1] == 'video':
  149. return 'playlist'
  150. return header
  151. def set_ignore_header(self, header, data):
  152. if header == 'download' and data == None:
  153. return 'ignore'
  154. return header
  155. def get_data(self):
  156. return self.dataPack