PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/sc2casts.py

https://github.com/bsdfish/xbmc-plugin-sc2casts
Python | 258 lines | 250 code | 6 blank | 2 comment | 9 complexity | f4565b199d36eaea4da82784f04a94a2 MD5 | raw file
Possible License(s): AGPL-3.0
  1. import os
  2. import re
  3. import sys
  4. import urllib
  5. import urllib2
  6. import xbmc
  7. import xbmcaddon
  8. import xbmcgui
  9. import xbmcplugin
  10. class SC2Casts:
  11. SC2CASTS_URL = 'http://sc2casts.com'
  12. VIDEO_URL = 'http://www.youtube.com'
  13. VIDEO_PLUGIN_URL = 'plugin://plugin.video.youtube'
  14. USERAGENT = ('Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.8) '
  15. 'Gecko/20100722 Firefox/3.6.8')
  16. __settings__ = xbmcaddon.Addon(id='plugin.video.sc2casts')
  17. __language__ = __settings__.getLocalizedString
  18. def getCastsURL(self, path):
  19. return self.SC2CASTS_URL + path
  20. def action(self, params):
  21. get = params.get
  22. if (get('action') == 'rootTop'):
  23. self.rootTop()
  24. if (get('action') == 'rootBrowse'):
  25. self.rootBrowse()
  26. if (get('action') == 'browseEvents'):
  27. self.browseEvents(params)
  28. if (get('action') == 'browseMatchups'):
  29. self.browseMatchups()
  30. if (get('action') == 'browseCasters'):
  31. self.browseCasters(params)
  32. if (get('action') == 'showTitles'
  33. or get('action') == 'showTitlesTop'
  34. or get('action') == 'showTitlesSearch'):
  35. self.showTitles(params)
  36. if (get('action') == 'showGames'):
  37. self.showGames(params)
  38. # Menu functions #
  39. # display the root menu
  40. def root(self):
  41. self.addCategory(self.__language__(31000),
  42. self.getCastsURL('/index.php'),
  43. 'showTitles')
  44. self.addCategory(self.__language__(31001), '', 'rootTop')
  45. self.addCategory(self.__language__(31002), '', 'rootBrowse')
  46. self.addCategory(self.__language__(31003), '', 'showTitlesSearch')
  47. # display the top casts menu
  48. def rootTop(self):
  49. self.addCategory(self.__language__(31004),
  50. self.getCastsURL('/top/index.php?all'),
  51. 'showTitlesTop')
  52. self.addCategory(self.__language__(31005),
  53. self.getCastsURL('/top/index.php?month'),
  54. 'showTitlesTop')
  55. self.addCategory(self.__language__(31006),
  56. self.getCastsURL('/top/index.php?week'),
  57. 'showTitlesTop')
  58. self.addCategory(self.__language__(31007),
  59. self.getCastsURL('/top/index.php'),
  60. 'showTitlesTop')
  61. # display the browse casts menu
  62. def rootBrowse(self):
  63. self.addCategory(self.__language__(31008),
  64. self.getCastsURL('/browse/index.php'),
  65. 'browseEvents')
  66. self.addCategory(self.__language__(31009), '', 'browseMatchups')
  67. self.addCategory(self.__language__(31010),
  68. self.getCastsURL('/browse/index.php'),
  69. 'browseCasters')
  70. # display the browse events menu
  71. def browseEvents(self, params = {}):
  72. get = params.get
  73. link = self.getRequest(get('url'))
  74. event = re.compile('<a href="/event(.*?)">(.*?)</a>').findall(link)
  75. for i in range(len(event)):
  76. self.addCategory(event[i][1],
  77. self.getCastsURL('/event'+event[i][0]),
  78. 'showTitles')
  79. # display the browse casters menu
  80. def browseMatchups(self):
  81. self.addCategory('PvZ', self.getCastsURL('/matchups-PvZ'),
  82. 'showTitles')
  83. self.addCategory('PvT', self.getCastsURL('/matchups-PvT'),
  84. 'showTitles')
  85. self.addCategory('TvZ', self.getCastsURL('/matchups-TvZ'),
  86. 'showTitles')
  87. self.addCategory('PvP', self.getCastsURL('/matchups-PvP'),
  88. 'showTitles')
  89. self.addCategory('TvT', self.getCastsURL('/matchups-TvT'),
  90. 'showTitles')
  91. self.addCategory('ZvZ', self.getCastsURL('/matchups-ZvZ'),
  92. 'showTitles')
  93. # display the browse casters menu
  94. def browseCasters(self, params = {}):
  95. get = params.get
  96. link = self.getRequest(get('url'))
  97. caster = re.compile('<a href="/caster(.*?)">(.*?)</a>').findall(link)
  98. for i in range(len(caster)):
  99. self.addCategory(caster[i][1],
  100. self.getCastsURL('/caster'+caster[i][0]),
  101. 'showTitles', len(caster))
  102. # Add functions #
  103. def addCategory(self, title, url, action, count = 0):
  104. url=(sys.argv[0] + '?url=' + urllib.quote_plus(url) + '&title=' +
  105. urllib.quote_plus(title) + '&action=' + urllib.quote_plus(action))
  106. listitem=xbmcgui.ListItem(title, iconImage='DefaultFolder.png',
  107. thumbnailImage='DefaultFolder.png')
  108. listitem.setInfo(type='Video', infoLabels={ 'Title': title })
  109. xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=url,
  110. listitem=listitem, isFolder=True,
  111. totalItems=count)
  112. def addVideo(self, title, url):
  113. # Check if URL is a 'fillUp' URL
  114. if url != 'fillUp':
  115. url = ('%s/?action=play_video&videoid=%s'
  116. %(self.VIDEO_PLUGIN_URL, url))
  117. liz=xbmcgui.ListItem(title, iconImage='DefaultVideo.png',
  118. thumbnailImage='DefaultVideo.png')
  119. liz.setInfo(type='Video', infoLabels={ 'Title': title })
  120. liz.setProperty('IsPlayable','true')
  121. xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=url,
  122. listitem=liz)
  123. # Show functions #
  124. def showTitles(self, params = {}):
  125. get = params.get
  126. url = get('url')
  127. # Check if user want to search
  128. if get('action') == 'showTitlesSearch':
  129. keyboard = xbmc.Keyboard('')
  130. keyboard.doModal()
  131. url = self.getCastsURL('/?q=' + keyboard.getText())
  132. link = self.getRequest(url)
  133. # Get settings
  134. boolMatchup = self.__settings__.getSetting('matchup')
  135. boolNr_games = self.__settings__.getSetting('nr_games')
  136. boolEvent = self.__settings__.getSetting('event')
  137. boolRound = self.__settings__.getSetting('round')
  138. boolCaster = self.__settings__.getSetting('caster')
  139. # Get info to show
  140. caster = re.compile('<a href="/.+?"><span class="caster_name">'
  141. '(.*?)</span></a>').findall(link)
  142. matchup = (re.compile('<span style="color:#cccccc">(.*?)</span>')
  143. .findall(link))
  144. roundname = (re.compile('<span class="round_name">(.*?)</span>')
  145. .findall(link))
  146. checkSource = (re.compile('<span class="source_name">(.*?)</span>')
  147. .findall(link))
  148. event = (re.compile('<span class="event_name".*?>(.*?)</span>')
  149. .findall(link))
  150. #Different source if URL is .../top
  151. if get('action') == 'showTitlesTop':
  152. title = (re.compile('<h3><a href="(.+?)"><b >(.+?)</b> vs '
  153. '<b >(.+?)</b>&nbsp;\((.*?)\)</a></h3>')
  154. .findall(link))
  155. else:
  156. title = re.compile('<h2><a href="(.+?)"><b >(.+?)</b> vs <b >'
  157. '(.+?)</b> \((.*?)\)</a>').findall(link)
  158. for i in range(len(event)):
  159. if checkSource[i] != '@ YouTube':
  160. pass
  161. else:
  162. url = ''
  163. if boolMatchup == 'true':
  164. url += matchup[i] + ' | '
  165. url += title[i][1] + ' vs ' + title[i][2] + ' | '
  166. if boolNr_games == 'true':
  167. url += title[i][3] + ' | '
  168. if boolEvent == 'true':
  169. url += event[i] + ' | '
  170. if boolRound == 'true':
  171. url += roundname[i] + ' | '
  172. if boolCaster == 'true':
  173. url += 'cast by: ' + caster[i]
  174. self.addCategory(url,title[i][0],'showGames')
  175. def showGames(self, params = {}):
  176. get = params.get
  177. link = self.getRequest(self.getCastsURL(get('url')))
  178. matchCount = (re.compile('<div id="g(.+?)"(.+?)</div></div>')
  179. .findall(link))
  180. if len(matchCount) > 0:
  181. for i in range(len(matchCount)):
  182. videoContent=re.compile('src="https://www.youtube.com/embed'
  183. '/(.+?)"').findall(matchCount[i][1])
  184. if len(videoContent) == 0:
  185. self.addVideo('Game ' + str(i + 1), 'fillUp')
  186. if len(videoContent) == 1:
  187. self.addVideo('Game ' + str(i + 1), videoContent[0])
  188. if len(videoContent) > 1:
  189. for k in range(len(videoContent)):
  190. self.addVideo('Game ' + str(i + 1) + ', part ' +
  191. str(k + 1),
  192. videoContent[k])
  193. else:
  194. videoContent=(re.compile('<param name="movie" '
  195. 'value="%s/v/(.+?)'
  196. '\?.+?"></param>' % self.VIDEO_URL)
  197. .findall(link))
  198. if len(videoContent) > 1:
  199. for n in range(len(videoContent)):
  200. self.addVideo('Game 1, part '+ str(n+1), videoContent[n])
  201. else:
  202. self.addVideo('Game 1', videoContent[0])
  203. # Data functions #
  204. def getParams(self, paramList):
  205. splitParams = paramList[paramList.find('?') + 1:].split('&')
  206. paramsFinal = {}
  207. for value in splitParams:
  208. splitParams = value.split('=')
  209. type = splitParams[0]
  210. content = splitParams[1]
  211. if type == 'url':
  212. content = urllib.unquote_plus(content)
  213. paramsFinal[type] = content
  214. return paramsFinal
  215. def getRequest(self, url):
  216. req = urllib2.Request(url)
  217. req.add_header('User-Agent', self.USERAGENT)
  218. response = urllib2.urlopen(req)
  219. link=response.read()
  220. response.close()
  221. return link