Kodi plugin to to play various online streams (mostly Latvian)

queue_management.py 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. # -*- coding: utf-8 -*-
  2. try:
  3. import wingdbstub
  4. except:
  5. pass
  6. import sys, os, urllib2, urllib, re, requests, datetime, time, json
  7. #CLI_MODE = True
  8. from kodiswift import xbmc, xbmcgui, xbmcplugin, CLI_MODE
  9. from kodiswift import Plugin, storage
  10. import pyxbmct.addonwindow as pyxbmct
  11. from downloadqueue import DownloadQueue
  12. import traceback, threading
  13. from resources.lib.content import file
  14. plugin = Plugin(addon_id="plugin.video.playstream")
  15. prefix = "plugin://plugin.video.playstream/"
  16. #plugin.load_addon_settings()
  17. timer = None
  18. timer_file = os.path.join(xbmc.translatePath("special://temp"), "timer_active") if not CLI_MODE else "timer_active"
  19. if os.path.exists(timer_file): os.remove(timer_file)
  20. sleep_time = 5 # TODO jāliek iekš parametriem
  21. #print "argv=",sys.argv
  22. handle = int(sys.argv[1])
  23. handle = 7777
  24. cmd = sys.argv[2]
  25. job_id = sys.argv[3] if len(sys.argv) > 3 else None
  26. queue_dir = os.path.join(xbmc.translatePath("special://temp"), "download_queue") if not CLI_MODE else "download_queue"
  27. download_queue = DownloadQueue(queue_dir)
  28. if not os.path.exists(queue_dir):
  29. os.mkdir(queue_dir)
  30. def main():
  31. if cmd == "list":
  32. ret = downloads_list()
  33. elif cmd == "view":
  34. ret = downloads_view_job(job_id)
  35. else:
  36. ret = downloads_cmd(cmd, job_id)
  37. def refresh_container():
  38. global timer
  39. if os.path.exists(timer_file):
  40. xbmc.executebuiltin('Container.Refresh')
  41. else:
  42. timer.stop()
  43. def downloads_list():
  44. #plugin.finish(items, view_mode=get_view_mode("WideList"), update_listing=True, cache_to_disc=False)
  45. #xbmcplugin.endOfDirectory(handle, succeeded=True, updateListing=True, cacheToDisc=False)
  46. items = ["Item {0}".format(i) for i in xrange(1, 11)]
  47. dialog = MultiChoiceDialog("Select items", [])
  48. dialog.doModal()
  49. xbmcgui.Dialog().notification("Finished", "Selected: {0}".format(str(dialog.selected)))
  50. del dialog #You need to delete your instance when it is no longer needed
  51. timer = threading.Timer(4.0, refresh_container)
  52. #timer.start()
  53. #open(timer_file, "w").write("started")
  54. def get_jobs():
  55. global timer
  56. jobs = download_queue.jobs()
  57. items = []
  58. i = 1
  59. for job in jobs:
  60. progress = float(job["currentbytes"])/float(job["totalbytes"])*100 if job["currentbytes"] and job["totalbytes"] else ""
  61. msg = "%.1f%% (%iMB/%iMB)"%(progress, job["currentbytes"] / 1024 / 1024, job["totalbytes"] / 1024 / 1024)
  62. title = "%s - %s: %s" % (job["status"], msg, job["output"])
  63. data2 = prefix + "downloads::view/%s" % job["job_id"]
  64. is_playable = False
  65. img = None
  66. desc = "%s\n%s\n%s\n%s\n%s" % (job["output"], msg, job["job_id"], job["file"], job["url"])
  67. #print title.encode("utf8"),data2,img
  68. context_menu = [
  69. ("Cancel download job", u'RunPlugin(plugin://plugin.video.playstream/downloads::cancel/%s'%(job["job_id"])),
  70. ("Pause download job", u'RunPlugin(plugin://plugin.video.playstream/downloads::pause/%s'%(job["job_id"])),
  71. ("Continue paused download job", u'RunPlugin(plugin://plugin.video.playstream/downloads::continue/%s'%(job["job_id"])),
  72. ("Cancel all jobs", u'RunPlugin(plugin://plugin.video.playstream/downloads::cancel_all'),
  73. ]
  74. item2 = xbmcgui.ListItem(title, "", "", "", data2)
  75. item2.setInfo("Video", {"title":title, "plot": desc,})
  76. item2.addContextMenuItems(context_menu)
  77. #item.setProperty("is_folder", "true")
  78. items.append(item2)
  79. #xbmcplugin.addDirectoryItem(handle=handle, url=data2, listitem=item2, isFolder=False)
  80. i += 1
  81. return items
  82. def downloads_view_job(job_id):
  83. pass
  84. def downloads_cmd(cmd, params):
  85. pass
  86. def get_view_mode(vm):
  87. modes = {
  88. "skin.estuary": {
  89. "None": None,
  90. "List": 50,
  91. "Poster": 51,
  92. "IconWall":52 ,
  93. "Shift": 53,
  94. "InfoWall": 54,
  95. "WideList": 55,
  96. "Wall": 500,
  97. "Banner": 501,
  98. "FanArt": 502
  99. },
  100. "skin.estuary.is": {
  101. "None": None,
  102. "List": 50,
  103. "Poster": 51,
  104. "IconWall":52 ,
  105. "Shift": 53,
  106. "InfoWall": 54,
  107. "WideList": 55,
  108. "Wall": 500,
  109. "Banner": 501,
  110. "FanArt": 502
  111. },
  112. }
  113. skin = xbmc.getSkinDir()
  114. if skin in modes and vm in modes[skin]:
  115. view_mode = modes[skin][vm]
  116. else:
  117. view_mode = 50
  118. return view_mode
  119. class MultiChoiceDialog(pyxbmct.AddonDialogWindow):
  120. def __init__(self, title="", items=None):
  121. super(MultiChoiceDialog, self).__init__(title)
  122. self.setGeometry(1200, 760, 10, 10)
  123. self.selected = []
  124. self.set_controls()
  125. self.connect_controls()
  126. self.update_list()
  127. self.set_navigation()
  128. def onAction(self, action):
  129. action_id = action.getId()
  130. if action_id in (101, 117):
  131. print "Context menu"
  132. def update_list(self):
  133. items = get_jobs()
  134. self.listing.reset()
  135. self.listing.addItems(items)
  136. def set_controls(self):
  137. self.listing = pyxbmct.List(_imageWidth=15)
  138. self.placeControl(self.listing, 0, 0, rowspan=10, columnspan=9)
  139. self.ok_button = pyxbmct.Button("OK")
  140. self.placeControl(self.ok_button, 1, 9)
  141. self.cancel_button = pyxbmct.Button("Cancel")
  142. self.placeControl(self.cancel_button, 2, 9)
  143. def connect_controls(self):
  144. self.connect(self.listing, self.item_selected)
  145. self.connect(self.ok_button, self.ok)
  146. self.connect(self.cancel_button, self.close)
  147. self.connect(117, self.context_menu)
  148. def set_navigation(self):
  149. self.listing.controlRight(self.ok_button)
  150. self.listing.controlLeft(self.ok_button)
  151. self.ok_button.setNavigation(self.listing, self.listing, self.cancel_button, self.cancel_button)
  152. self.cancel_button.setNavigation(self.listing, self.listing, self.ok_button, self.ok_button)
  153. if self.listing.size():
  154. self.setFocus(self.listing)
  155. else:
  156. self.setFocus(self.cancel_button)
  157. def context_menu(self):
  158. list_item = self.listing.getSelectedItem()
  159. print "aaaa"
  160. def item_selected(self):
  161. list_item = self.listing.getSelectedItem()
  162. pass
  163. def ok(self):
  164. self.selected = self.listing.getSelectedItem()
  165. super(MultiChoiceDialog, self).close()
  166. def close(self):
  167. self.selected = None
  168. super(MultiChoiceDialog, self).close()
  169. if __name__ == '__main__':
  170. main()