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

downloadqueue.py 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # -*- coding: utf-8 -*-
  2. try:
  3. import wingdbstub
  4. except:
  5. pass
  6. import sys, os, urllib2, urllib, re, requests, datetime, json
  7. from resources.lib.content import file
  8. #CLI_MODE = True
  9. # from kodiswift import xbmc, xbmcgui, CLI_MODE
  10. # from kodiswift import Plugin, storage
  11. class DownloadQueue(object):
  12. def __init__(self, queue_dir):
  13. self.queue_dir = queue_dir
  14. if not os.path.isdir(queue_dir):
  15. os.mkdir(queue_dir)
  16. def job_get(self, job_id):
  17. try:
  18. job = json.loads(open(os.path.join(self.queue_dir, job_id), "r").read())
  19. except:
  20. job = None
  21. return job
  22. def job_put(self, job_id, job):
  23. try:
  24. s = json.dumps(job)
  25. open(os.path.join(self.queue_dir, job_id), "w").write(s)
  26. except:
  27. raise Exception("Can not store job")
  28. def job_remove(self, job_id):
  29. try:
  30. os.remove(os.path.join(self.queue_dir, job_id))
  31. except:
  32. pass
  33. def job_ids(self, filer=""):
  34. lst = os.listdir(self.queue_dir)
  35. lst.sort()
  36. return lst
  37. def jobs(self, filter=""):
  38. jobs = []
  39. for job_id in self.job_ids():
  40. jobs.append(self.job_get(job_id))
  41. return jobs
  42. def jobs_count(self):
  43. count = {}
  44. for job in jobs():
  45. stat = job["status"]
  46. if stat in count:
  47. count[stat] += 1
  48. else:
  49. count[stat] = 1
  50. return stat
  51. def clear(self):
  52. for job_id in self.job_ids():
  53. os.remove(file.join(self.queue_dir, job_id))