/xbmc-addons/src/plugin.video.polishtv.live/cmd/record.py

http://sd-xbmc.googlecode.com/ · Python · 104 lines · 88 code · 14 blank · 2 comment · 19 complexity · 0f40810e5690422d56870dc9ed62d567 MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. import urllib, urllib2, httplib
  3. import re, sys, os
  4. import threading
  5. import datetime
  6. import time
  7. try:
  8. import simplejson as json
  9. except ImportError:
  10. import json
  11. #REC_JSON_PATH = os.path.join(os.getcwd(), "../recs")
  12. HOST = 'XBMC'
  13. class Parser:
  14. def __init__(self):
  15. pass
  16. def getParam(self, params, name):
  17. try:
  18. result = params[name]
  19. result = urllib.unquote_plus(result)
  20. return result
  21. except:
  22. return None
  23. def getIntParam (self, params, name):
  24. try:
  25. param = self.getParam(params, name)
  26. return int(param)
  27. except:
  28. return None
  29. def getBoolParam (self, params, name):
  30. try:
  31. param = self.getParam(params,name)
  32. return 'True' == param
  33. except:
  34. return None
  35. def getParams(self, paramstring):
  36. param=[]
  37. if len(paramstring) >= 2:
  38. params = paramstring
  39. cleanedparams = params.replace('?', '')
  40. if (params[len(params)-1] == '/'):
  41. params = params[0:len(params)-2]
  42. pairsofparams = cleanedparams.split('&')
  43. param = {}
  44. for i in range(len(pairsofparams)):
  45. splitparams = {}
  46. splitparams = pairsofparams[i].split('=')
  47. if (len(splitparams)) == 2:
  48. param[splitparams[0]] = splitparams[1]
  49. return param
  50. class Record:
  51. def getOptions(self):
  52. file = sys.argv[1]
  53. if os.path.isfile(file):
  54. raw = open(file, 'r').read()
  55. res = json.loads(raw)
  56. return res
  57. def download(self, rtmp, rectime, dstpath, name, opts = {}):
  58. if os.path.isfile(rtmp) and os.access(rtmp, os.X_OK) and os.path.isdir(dstpath):
  59. file = os.path.join(str(dstpath), name + ".flv")
  60. os.system(str(rtmp) + " -B " + str(rectime) + " -r " + str(opts['rtmp']) + " -s " + str(opts['ticket']) + " -p token -v live -o " + file)
  61. os.remove(sys.argv[1])
  62. def getParams(self, playerUrl, login, password, channel, hq):
  63. data = None
  64. if login == '' and password == '':
  65. values = { 'cid': channel, 'platform': 'XBMC' }
  66. else:
  67. values = { 'cid': channel, 'username': login, 'userpassword': password, 'platform': 'XBMC' }
  68. try:
  69. parser = Parser()
  70. headers = { 'User-Agent' : HOST }
  71. data = urllib.urlencode(values)
  72. reqUrl = urllib2.Request(playerUrl, data, headers)
  73. response = urllib2.urlopen(reqUrl)
  74. resLink = response.read()
  75. params = parser.getParams(resLink)
  76. ticket = parser.getParam(params, "73")
  77. rtmpLink = parser.getParam(params, "10")
  78. playPath = parser.getParam(params, "11")
  79. if hq == 'true':
  80. playPath = playPath + 'HI'
  81. rtmp = str(rtmpLink) + '/' + str(playPath)
  82. data = { 'rtmp': rtmp, 'ticket': ticket }
  83. except urllib2.URLError, urlerr:
  84. data = { 'rtmp': None, 'ticket': None }
  85. print urlerr
  86. return data
  87. rec = Record()
  88. opts = rec.getOptions()
  89. params = rec.getParams(opts['urlPlayer'], opts['login'], opts['password'], int(opts['channel']), opts['hq'])
  90. rec.download(opts['rtmp_path'], opts['rectime'], opts['dst_path'], opts['name'], params)