PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/addons/plugin.video.gametrailers/resources/lib/gametrailers_play.py

http://dandar3-xbmc-addons.googlecode.com/
Python | 473 lines | 264 code | 63 blank | 146 comment | 47 complexity | 95fd6516cc9f1091347bf2cfbba5c0ce MD5 | raw file
  1. #
  2. # Imports
  3. #
  4. from BeautifulSoup import BeautifulSoup, SoupStrainer
  5. from gametrailers_const import __settings__, __language__
  6. from gametrailers_utils import HTTPCommunicator
  7. import os
  8. import re
  9. import sys
  10. import urllib
  11. import xbmc
  12. import xbmcgui
  13. import xml.dom.minidom
  14. #
  15. # Main class
  16. #
  17. class Main:
  18. #
  19. # Init
  20. #
  21. def __init__( self ) :
  22. #
  23. # Constants
  24. #
  25. self.DEBUG = False
  26. self.PLAYER_URL_RE = re.compile( ".*/player/(\d+).html" )
  27. self.EPISODE_BONUSROUND_URL_RE = re.compile( ".*/episode/bonusround/.*" )
  28. self.BONUSROUND_PHP_URL_RE = re.compile( ".*/bonusround.php\?ep=(\d+)" )
  29. self.VIDEO_URL_RE = re.compile( ".*/video/(.+)?/(\d+)" )
  30. self.GAMETRAILERS_TV_PLAYER_RE = re.compile( ".*/gametrailerstv_player.php?.*" )
  31. self.EPISODE_GAMETRAILER_TV_RE = re.compile( ".*/episode/gametrailers-tv/.*" )
  32. self.USER_MOVIES_URL_RE = re.compile( ".*/usermovies/(\d+).html" )
  33. self.MOSES_MOVIES_THUMBS = re.compile(".*/moses/moviesthumbs/(\d+)-.*")
  34. #
  35. # Parse parameters...
  36. #
  37. params = dict(part.split('=') for part in sys.argv[ 2 ][ 1: ].split('&'))
  38. self.video_page_url = urllib.unquote_plus( params[ "video_page_url" ] )
  39. # Settings
  40. self.video_format = __settings__.getSetting("video_format")
  41. self.video_quality = __settings__.getSetting("video_quality")
  42. #
  43. # Play video...
  44. #
  45. self.playVideo()
  46. #
  47. # Play video...
  48. #
  49. def playVideo( self ) :
  50. if (self.DEBUG) :
  51. print "video_page_url = " + self.video_page_url
  52. #
  53. # Get current list item details...
  54. #
  55. title = unicode( xbmc.getInfoLabel( "ListItem.Title" ), "utf-8" )
  56. thumbnail = xbmc.getInfoImage( "ListItem.Thumb" )
  57. studio = unicode( xbmc.getInfoLabel( "ListItem.Studio" ), "utf-8" )
  58. plot = unicode( xbmc.getInfoLabel( "ListItem.Plot" ), "utf-8" )
  59. genre = unicode( xbmc.getInfoLabel( "ListItem.Genre" ), "utf-8" )
  60. #
  61. # Show wait dialog while parsing data...
  62. #
  63. dialogWait = xbmcgui.DialogProgress()
  64. dialogWait.create( __language__(30504), title )
  65. #
  66. # Video page URL = /player/48119.html
  67. #
  68. if (self.PLAYER_URL_RE.match( self.video_page_url ) ) :
  69. video_urls = self._getVideoUrl1( self.video_page_url )
  70. #
  71. # Video page URL = /episode/bonusround/303?ch=2&sd=1
  72. #
  73. elif (self.EPISODE_BONUSROUND_URL_RE.match ( self.video_page_url ) ) :
  74. video_urls = self._getVideoUrl2( self.video_page_url )
  75. #
  76. # Video page URL = /video/brutal-b-roll-darksiders/49436
  77. #
  78. elif (self.VIDEO_URL_RE.match( self.video_page_url ) ) :
  79. video_urls = self._getVideoUrl3( self.video_page_url )
  80. #
  81. # Video page URL = /gametrailerstv_player.php?ep=60&ch=1&sd=1
  82. # Video page URL = /episode/gametrailers-tv/64&ch=2&sd=0?ep=64&ch=2&sd=0
  83. #
  84. elif (self.GAMETRAILERS_TV_PLAYER_RE.match( self.video_page_url ) or
  85. self.EPISODE_GAMETRAILER_TV_RE.match( self.video_page_url ) ) :
  86. video_urls = self._getVideoUrl4( self.video_page_url )
  87. #
  88. # Video page URL = /bonusround.php?ep=15
  89. #
  90. elif (self.BONUSROUND_PHP_URL_RE.match( self.video_page_url ) ) :
  91. video_urls = self._getVideoUrl5( self.video_page_url )
  92. #
  93. # Video page URL = player/usermovies/1.html
  94. #
  95. elif (self.USER_MOVIES_URL_RE.match( self.video_page_url ) ) :
  96. video_urls = self._getVideoUrl6( self.video_page_url )
  97. #
  98. # Check video URLs...
  99. #
  100. httpCommunicator = HTTPCommunicator()
  101. have_valid_url = False
  102. for video_url in video_urls :
  103. if httpCommunicator.exists( video_url ) :
  104. have_valid_url = True
  105. break
  106. #
  107. # Play video...
  108. #
  109. if have_valid_url :
  110. playlist = xbmc.PlayList( xbmc.PLAYLIST_VIDEO )
  111. playlist.clear()
  112. for video_url in video_urls :
  113. listitem = xbmcgui.ListItem( title, iconImage="DefaultVideo.png", thumbnailImage=thumbnail )
  114. listitem.setInfo( "video", { "Title": title, "Studio" : studio, "Plot" : plot, "Genre" : genre } )
  115. playlist.add( video_url, listitem )
  116. # Close wait dialog...
  117. dialogWait.close()
  118. del dialogWait
  119. # Play video...
  120. xbmcPlayer = xbmc.Player()
  121. xbmcPlayer.play( playlist )
  122. #
  123. # Alert user...
  124. #
  125. else :
  126. xbmcgui.Dialog().ok( __language__(30000), __language__(30505) )
  127. #
  128. # Video page URL = /player/48119.html
  129. #
  130. def _getVideoUrl1( self, video_page_url ):
  131. movie_id = self.PLAYER_URL_RE.search( video_page_url ).group(1)
  132. #
  133. # Get HTML page...
  134. #
  135. usock = urllib.urlopen( "http://mosii.gametrailers.com/getmediainfo4.php?hd=1&mid=%s" % movie_id )
  136. htmlData = usock.read()
  137. usock.close()
  138. # Debug
  139. if (self.DEBUG) :
  140. f = open(os.path.join( xbmc.translatePath( "special://profile" ), "plugin_data", "video", sys.modules[ "__main__" ].__plugin__, "video_page.html" ), "w")
  141. f.write( htmlData )
  142. f.close()
  143. # Parse HTML response...
  144. params = dict(part.split('=') for part in htmlData.split('&'))
  145. umfilename = urllib.unquote( params[ "umfilename" ] )
  146. # Video URL...
  147. if (self.video_format == "0") :
  148. video_url = "http://trailers.gametrailers.com/gt_vault/%s.flv" % umfilename
  149. elif (self.video_format == "1") :
  150. video_url = "http://trailers.gametrailers.com/gt_vault/%s.mov" % umfilename
  151. elif (self.video_format == "2") :
  152. video_url = "http://trailers.gametrailers.com/gt_vault/%s.wmv" % umfilename
  153. #
  154. # Return value
  155. #
  156. video_urls = []
  157. video_urls.append( video_url )
  158. return video_urls
  159. #
  160. # Video page URL = /episode/bonusround/303?ch=2&sd=1
  161. #
  162. def _getVideoUrl2(self, video_page_url ):
  163. #
  164. # Get HTML page...
  165. #
  166. httpCommunicator = HTTPCommunicator()
  167. htmlData = httpCommunicator.get( video_page_url )
  168. # Parse HTML page...
  169. beautifulSoup = BeautifulSoup( htmlData )
  170. embed = beautifulSoup.find( "embed" )
  171. if embed == None :
  172. return []
  173. filename = embed[ "flashvars" ][ 9: ]
  174. url = re.compile( "(.+?)&" ).search( filename ).group( 1 )
  175. #
  176. # Get XML data....
  177. #
  178. if not url.startswith( "http" ) :
  179. wins_xml_url = "http://www.gametrailers.com%s" % url
  180. else :
  181. wins_xml_url = url
  182. usock = urllib.urlopen( wins_xml_url )
  183. xmldoc = xml.dom.minidom.parse( usock )
  184. usock.close()
  185. # Debug
  186. if (self.DEBUG) :
  187. f = open(os.path.join( xbmc.translatePath( "special://profile" ), "plugin_data", "video", sys.modules[ "__main__" ].__plugin__, "wins_xml_reply.xml" ), "w")
  188. f.write( xmldoc.toxml() )
  189. f.close()
  190. # Get the movie url...
  191. fileinfoNode = xmldoc.documentElement.getElementsByTagName( "fileinfo" )[0]
  192. sdNode = fileinfoNode.getElementsByTagName( "sd" )[0]
  193. sdFlvNode = sdNode.getElementsByTagName( "flv" )[0]
  194. video_url = sdFlvNode.childNodes[0].nodeValue
  195. if self.video_quality == "1" :
  196. hdNode = fileinfoNode.getElementsByTagName( "hd" )[0]
  197. hdFlvNode = hdNode.getElementsByTagName ( "flv" )[0]
  198. if hdFlvNode.childNodes[0].nodeValue :
  199. video_url = hdFlvNode.childNodes[0].nodeValue
  200. #
  201. # Return value
  202. #
  203. video_urls = []
  204. video_urls.append( video_url )
  205. return video_urls
  206. #
  207. # Video page URL = /video/brutal-b-roll-darksiders/49436
  208. #
  209. def _getVideoUrl3( self, video_page_url ):
  210. movie_id = self.VIDEO_URL_RE.search( video_page_url ).group(2)
  211. #
  212. # Get video URL (method #1)...
  213. #
  214. httpCommunicator = HTTPCommunicator()
  215. htmlData = httpCommunicator.get( "http://mosii.gametrailers.com/getmediainfo4.php?hd=1&mid=%s" % movie_id )
  216. # Debug
  217. if (self.DEBUG) :
  218. f = open(os.path.join( xbmc.translatePath( "special://profile" ), "plugin_data", "video", sys.modules[ "__main__" ].__plugin__, "video_page.html" ), "w")
  219. f.write( htmlData )
  220. f.close()
  221. # Parse HTML response...
  222. params = dict(part.split('=', 1) for part in htmlData.split('&'))
  223. umfilename = urllib.unquote( params[ "umfilename" ] )
  224. hasHD = urllib.unquote( params.get("hasHD") )
  225. # SD preferred, but the URL is for HD...
  226. if self.video_quality == "0" and hasHD == "0":
  227. umthumbnail = urllib.unquote( params.get("umthumbnail") )
  228. movie_id_sd = self.MOSES_MOVIES_THUMBS.search( umthumbnail ).group(1)
  229. # Get data...
  230. usock = urllib.urlopen( "http://mosii.gametrailers.com/getmediainfo4.php?hd=1&mid=%s" % movie_id_sd )
  231. htmlData = usock.read()
  232. usock.close()
  233. # Parse response...
  234. params = dict(part.split('=', 1) for part in htmlData.split('&'))
  235. umfilename = urllib.unquote( params[ "umfilename" ] )
  236. #
  237. # Video URL...
  238. #
  239. if (self.video_format == "0") :
  240. video_url = "http://trailers.gametrailers.com/gt_vault/%s.flv" % umfilename
  241. elif (self.video_format == "1") :
  242. video_url = "http://trailers.gametrailers.com/gt_vault/%s.mov" % umfilename
  243. elif (self.video_format == "2") :
  244. video_url = "http://trailers.gametrailers.com/gt_vault/%s.wmv" % umfilename
  245. #
  246. # Get video URL (method #2)...
  247. #
  248. if not httpCommunicator.exists(video_url) :
  249. neo_xml_url = "http://www.gametrailers.com/neo/?page=xml.mediaplayer.Mediagen&movieId=%s" % movie_id
  250. usock = urllib.urlopen( neo_xml_url )
  251. xmldoc = xml.dom.minidom.parse( usock )
  252. usock.close()
  253. video_nodes = xmldoc.documentElement.getElementsByTagName( "video" )
  254. if video_nodes != None :
  255. src_nodes = video_nodes[0].getElementsByTagName( "src" )
  256. if src_nodes != None :
  257. video_url = src_nodes[0].childNodes[0].nodeValue
  258. #
  259. # Return value
  260. #
  261. video_urls = []
  262. video_urls.append( video_url )
  263. return video_urls
  264. #
  265. # Video page URL = /gametrailerstv_player.php?ep=60&ch=1&sd=1
  266. # Video page URL = /episode/gametrailers-tv/64&ch=2&sd=0?ep=64&ch=2&sd=0
  267. #
  268. def _getVideoUrl4(self, video_page_url ):
  269. #
  270. # Init
  271. #
  272. if not video_page_url.startswith( "http" ) :
  273. video_page_url = "http://www.gametrailers.com%s" % video_page_url
  274. #
  275. # Get HTML page...
  276. #
  277. usock = urllib.urlopen( video_page_url )
  278. htmlData = usock.read()
  279. usock.close()
  280. # Debug
  281. if (self.DEBUG) :
  282. f = open(os.path.join( xbmc.translatePath( "special://profile" ), "plugin_data", "video", sys.modules[ "__main__" ].__plugin__, "video_page.html" ), "w")
  283. f.write( htmlData )
  284. f.close()
  285. # Parse HTML page...
  286. soupStrainer = SoupStrainer ( "div", { "id" : "gttv_player" } )
  287. beautifulSoup = BeautifulSoup( htmlData, soupStrainer )
  288. div_gttv_player = beautifulSoup.find ( "div", { "id" : "gttv_player" } )
  289. xml_filename = re.compile( "myFlash.addVariable\('filename', '(.+)'\);" ).search( div_gttv_player.script.string ).group( 1 )
  290. #
  291. # Get XML data....
  292. #
  293. gttv_xml_url = "http://moses.gametrailers.com/moses/gttv_xml/%s" % urllib.quote( xml_filename )
  294. usock = urllib.urlopen( gttv_xml_url )
  295. xmlData = usock.read().strip()
  296. usock.close()
  297. # Debug
  298. if (self.DEBUG) :
  299. f = open(os.path.join( xbmc.translatePath( "special://profile" ), "plugin_data", "video", sys.modules[ "__main__" ].__plugin__, "gttv.xml" ), "w")
  300. f.write( xmlData )
  301. f.close()
  302. #
  303. # Parse XML...
  304. #
  305. xmldoc = xml.dom.minidom.parseString( xmlData )
  306. video_urls = []
  307. #
  308. # Get the movie url (single FLV for all episodes)...
  309. #
  310. if len( xmldoc.documentElement.getElementsByTagName( "fileinfo" ) ) == 1 :
  311. fileinfoNode = xmldoc.documentElement.getElementsByTagName( "fileinfo" )[ 0 ]
  312. sdNode = fileinfoNode.getElementsByTagName( "sd" )[0]
  313. sdFlvNode = sdNode.getElementsByTagName( "flv" )[0]
  314. video_url = sdFlvNode.childNodes[0].nodeValue
  315. if self.video_quality == "1" :
  316. hdNode = fileinfoNode.getElementsByTagName( "hd" )[0]
  317. hdFlvNode = hdNode.getElementsByTagName ( "flv" )[0]
  318. if hdFlvNode.childNodes[0].nodeValue :
  319. video_url = hdFlvNode.childNodes[0].nodeValue
  320. video_urls.append ( video_url )
  321. #
  322. # Get the movie urls (separate FLV for each episode)...
  323. #
  324. else :
  325. chapterNodes = xmldoc.documentElement.getElementsByTagName( "chapter" )
  326. for chapterNode in chapterNodes :
  327. fileinfoNode = chapterNode.getElementsByTagName( "fileinfo" )[0]
  328. sdNode = fileinfoNode.getElementsByTagName( "sd" )[0]
  329. video_url = sdNode.childNodes[0].nodeValue.strip()
  330. if self.video_quality == "1" :
  331. hdNode = fileinfoNode.getElementsByTagName( "hd" )[0]
  332. if hdNode.childNodes[0].nodeValue :
  333. video_url = hdNode.childNodes[0].nodeValue.strip()
  334. video_urls.append ( video_url )
  335. #
  336. # Return value
  337. #
  338. return video_urls
  339. #
  340. # Video page URL = /bonusround.php?ep=15
  341. #
  342. def _getVideoUrl5( self, video_page_url ):
  343. #
  344. # Get HTML page...
  345. #
  346. httpCommunicator = HTTPCommunicator()
  347. htmlData = httpCommunicator.get( video_page_url )
  348. # Debug
  349. if (self.DEBUG) :
  350. f = open(os.path.join( xbmc.translatePath( "special://profile" ), "plugin_data", "video", sys.modules[ "__main__" ].__plugin__, "play_url_5.html" ), "w")
  351. f.write( htmlData )
  352. f.close()
  353. #
  354. # Parse HTML page (get number of parts)...
  355. #
  356. soupStrainer = SoupStrainer ( "div", { "id" : "media_div" } )
  357. beautifulSoup = BeautifulSoup( htmlData, parseOnlyThese=soupStrainer )
  358. episode_no = re.compile( "myFlash.addVariable\('episode', '(.*)'\)" ).search( beautifulSoup.script.string ).group( 1 )
  359. part_count = int( re.compile( "myFlash.addVariable\('eppartcount', '(.*)'\)" ).search( beautifulSoup.script.string ).group( 1 ) )
  360. #
  361. # Return value
  362. #
  363. video_urls = []
  364. for part_no in range(1, part_count + 1) :
  365. hd_part = ""
  366. if self.video_quality == "1" :
  367. if ( 4 <= int(episode_no) and int(episode_no) <= 21 ) :
  368. hd_part = "_h264"
  369. video_url = "http://trailers.gametrailers.com/gt_vault/br_ep%s_pt%s%s.flv" % ( episode_no, part_no, hd_part )
  370. video_urls.append( video_url )
  371. return video_urls
  372. #
  373. # Video page URL = player/usermovies/1.html
  374. #
  375. def _getVideoUrl6( self, video_page_url ):
  376. movie_id = self.USER_MOVIES_URL_RE.search( video_page_url ).group(1)
  377. #
  378. # Get HTML page...
  379. #
  380. httpCommunicator = HTTPCommunicator()
  381. htmlData = httpCommunicator.get( "http://mosii.gametrailers.com/getmediainfo4.php?umid=%s" % movie_id )
  382. # Debug
  383. if (self.DEBUG) :
  384. f = open(os.path.join( xbmc.translatePath( "special://profile" ), "plugin_data", "video", sys.modules[ "__main__" ].__plugin__, "video_page.html" ), "w")
  385. f.write( htmlData )
  386. f.close()
  387. # Parse HTML response...
  388. params = dict(part.split('=') for part in htmlData.split('&'))
  389. umfilename = urllib.unquote( params[ "umfilename" ] )
  390. # Video URL...
  391. video_url = "http://umtrailers.gametrailers.com/gt_usermovies/um_%s.flv" % umfilename
  392. #
  393. # Return value
  394. #
  395. video_urls = []
  396. video_urls.append( video_url )
  397. return video_urls
  398. #
  399. # The End
  400. #