home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / python / flashscraper.py < prev    next >
Encoding:
Python Source  |  2007-11-12  |  8.2 KB  |  223 lines

  1. # Miro - an RSS based video player application
  2. # Copyright (C) 2005-2007 Participatory Culture Foundation
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  17.  
  18. import re
  19. import httpclient
  20. import urlparse
  21. import cgi
  22. from xml.dom import minidom
  23. from urllib import unquote_plus
  24. from util import checkU, returnsUnicode
  25.  
  26. # =============================================================================
  27.  
  28. def tryScrapingURL(url, callback):
  29.     checkU(url)
  30.     scrape =_getScrapeFunctionFor(url)
  31.     if scrape is not None:
  32.         scrape(url,lambda x:_actualURLCallback(url,callback,x))
  33.     else:
  34.         callback(url)
  35.     
  36. # =============================================================================
  37.  
  38. # The callback is wrapped in this for flv videos
  39. def _actualURLCallback(url, callback, newURL):
  40.     if newURL:
  41.         checkU(newURL)
  42.     #print "Changed:"
  43.     #print url
  44.     #print "   to"
  45.     #print newURL
  46.     callback(newURL, contentType = u"video/x-flv")
  47.  
  48. def _getScrapeFunctionFor(url):
  49.     checkU(url)
  50.     for scrapeInfo in scraperInfoMap:
  51.         if re.compile(scrapeInfo['pattern']).match(url) is not None:
  52.             return scrapeInfo['func']
  53.     return None
  54.  
  55. def _scrapeYouTubeURL(url, callback):
  56.     checkU(url)
  57.     httpclient.grabHeaders(url, lambda x:_youTubeCallback(x,callback),
  58.                            lambda x:_youTubeErrback(x,callback))
  59.  
  60. def _youTubeCallback(info, callback):
  61.     url = info['redirected-url']
  62.     try:
  63.         components = urlparse.urlsplit(url)
  64.         params = cgi.parse_qs(components[3])
  65.         videoID = params['video_id'][0]
  66.         t = params['t'][0]
  67.         url = u"http://youtube.com/get_video.php?video_id=%s&t=%s" % (videoID, t)
  68.         callback(url)
  69.     except:
  70.         print "DTV: WARNING, unable to scrape You Tube Video URL: %s" % url
  71.         callback(None)
  72.  
  73. def _youTubeErrback(err, callback):
  74.     print "DTV: WARNING, network error scraping You Tube Video URL"
  75.     callback(None)
  76.  
  77. def _scrapeGoogleVideoURL(url, callback):
  78.     try:
  79.         components = urlparse.urlsplit(url)
  80.         params = cgi.parse_qs(components[3])
  81.         docId = params['docId'][0]
  82.         url = u"http://video.google.com/videofile/%s.flv?docid=%s&itag=5" % (docId, docId)
  83.         callback(url)
  84.     except:
  85.         print "DTV: WARNING, unable to scrape Google Video URL: %s" % url
  86.         callback(None)
  87.  
  88. def _scrapeLuLuVideoURL(url, callback):
  89.     try:
  90.         components = urlparse.urlsplit(url)
  91.         params = cgi.parse_qs(components[3])
  92.         url = unquote_plus(params['file'][0]).decode('ascii','replace')
  93.         callback(url)
  94.     except:
  95.         print "DTV: WARNING, unable to scrape LuLu.tv Video URL: %s" % url
  96.         callback(None)
  97.  
  98. def _scrapeVMixVideoURL(url, callback):
  99.     try:
  100.         components = urlparse.urlsplit(url)
  101.         params = cgi.parse_qs(components[3])
  102.         t = params['type'][0]
  103.         ID = params['id'][0]
  104.         l = params['l'][0]
  105.         url = u"http://sdstage01.vmix.com/videos.php?type=%s&id=%s&l=%s" % (t,ID,l)
  106.         httpclient.grabURL(url, lambda x:_scrapeVMixCallback(x,callback),
  107.                            lambda x:_scrapeVMixErrback(x,callback))
  108.  
  109.     except:
  110.         print "DTV: WARNING, unable to scrape VMix Video URL: %s" % url
  111.         callback(None)
  112.  
  113. def _scrapeVMixCallback(info, callback):
  114.     try:
  115.         doc = minidom.parseString(info['body'])
  116.         url = doc.getElementsByTagName('file').item(0).firstChild.data.decode('ascii','replace')
  117.         callback(url)
  118.     except:
  119.         print "DTV: WARNING, unsable to scrape XML for VMix Video URL %s" % info['redirected-url']
  120.         callback(None)
  121.  
  122. def _scrapeVMixErrback(err, callback):
  123.     print "DTV: WARNING, network error scraping VMix Video URL"
  124.     callback(None)
  125.  
  126. def _scrapeDailyMotionVideoURL(url, callback):
  127.     httpclient.grabHeaders(url, lambda x:_scrapeDailyMotionCallback(x,callback),
  128.                            lambda x:_scrapeDailyMotionErrback(x,callback))
  129.  
  130. def _scrapeDailyMotionCallback(info, callback):
  131.     url = info['redirected-url']
  132.     try:
  133.         components = urlparse.urlsplit(url)
  134.         params = cgi.parse_qs(components[3])
  135.         url = unquote_plus(params['url'][0]).decode('ascii','replace')
  136.         callback(url)
  137.     except:
  138.         print "DTV: WARNING, unable to scrape Daily Motion URL: %s" % url
  139.         callback(None)
  140.  
  141. def _scrapeDailyMotionErrback(info, callback):
  142.     print "DTV: WARNING, network error scraping Daily Motion Video URL"
  143.     callback(None)
  144.  
  145. def _scrapeVSocialVideoURL(url, callback):
  146.     try:
  147.         components = urlparse.urlsplit(url)
  148.         params = cgi.parse_qs(components[3])
  149.         v = params['v'][0]
  150.         url = u'http://static.vsocial.com/varmedia/vsocial/flv/%s_out.flv' % v
  151.         callback(url)
  152.     except:
  153.         print "DTV: WARNING, unable to scrape VSocial URL: %s" % url
  154.         callback(None)
  155.  
  156. def _scrapeVeohTVVideoURL(url, callback):
  157.     try:
  158.         components = urlparse.urlsplit(url)
  159.         params = cgi.parse_qs(components[3])
  160.         t = params['type'][0]
  161.         permalinkId= params['permalinkId'][0]
  162.         url = u'http://www.veoh.com/movieList.html?type=%s&permalinkId=%s&numResults=45' % (t, permalinkId)
  163.         httpclient.grabURL(url, lambda x: _scrapeVeohTVCallback(x, callback),
  164.                            lambda x:_scrapeVeohTVErrback(x, callback))
  165.     except:
  166.         print "DTV: WARNING, unable to scrape Veoh URL: %s" % url
  167.         callback(None)
  168.  
  169. def _scrapeVeohTVCallback(info, callback):
  170.     url = info['redirected-url']
  171.     try:
  172.         params = cgi.parse_qs(info['body'])
  173.         fileHash = params['previewHashLow'][0]
  174.         if fileHash[-1] == ",":
  175.             fileHash=fileHash[:-1]
  176.         url = u'http://ll-previews.veoh.com/previews/get.jsp?fileHash=%s' % fileHash
  177.         callback(url)
  178.     except:
  179.         print "DTV: WARNING, unable to scrape Veoh URL data: %s" % url
  180.         callback(None)
  181.  
  182. def _scrapeVeohTVErrback(err, callback):
  183.     print "DTV: WARNING, network error scraping Veoh TV Video URL"
  184.     callback(None)
  185.  
  186. def _scrapeBreakVideoURL(url, callback):
  187.     httpclient.grabHeaders(url, lambda x:_scrapeBreakCallback(x,callback),
  188.                            lambda x:_scrapeBreakErrback(x,callback))
  189.  
  190. def _scrapeBreakCallback(info, callback):
  191.     url = info['redirected-url']
  192.     try:
  193.         components = urlparse.urlsplit(url)
  194.         params = cgi.parse_qs(components[3])
  195.         url = unquote_plus(params['sVidLoc'][0]).decode('ascii','replace')
  196.         callback(url)
  197.     except:
  198.         print "DTV: WARNING, unable to scrape Break URL: %s" % url
  199.         callback(None)
  200.  
  201. def _scrapeBreakErrback(info, callback):
  202.     print "DTV: WARNING, network error scraping Break Video URL"
  203.     callback(None)
  204.  
  205. def _scrapeGreenPeaceVideoURL(url, callback):
  206.     print "DTV: Warning, unable to scrape Green peace Video URL %s" % url
  207.     print callback(None)
  208.  
  209. # =============================================================================
  210.  
  211. scraperInfoMap = [
  212.     {'pattern': 'http://([^/]+\.)?youtube.com/(?!get_video\.php)',         'func': _scrapeYouTubeURL},
  213.     {'pattern': 'http://video.google.com/googleplayer.swf', 'func': _scrapeGoogleVideoURL},
  214.     {'pattern': 'http://([^/]+\.)?lulu.tv/wp-content/flash_play/flvplayer', 'func': _scrapeLuLuVideoURL},
  215.     {'pattern': 'http://([^/]+\.)?vmix.com/flash/super_player.swf', 'func': _scrapeVMixVideoURL},
  216.     {'pattern': 'http://([^/]+\.)?dailymotion.com/swf', 'func': _scrapeDailyMotionVideoURL},
  217.     {'pattern': 'http://([^/]+\.)?vsocial.com/flash/vp.swf', 'func': _scrapeVSocialVideoURL},
  218.     {'pattern': 'http://([^/]+\.)?veoh.com/multiplayer.swf', 'func': _scrapeVeohTVVideoURL},
  219.     {'pattern': 'http://([^/]+\.)?greenpeaceweb.org/GreenpeaceTV1Col.swf', 'func': _scrapeGreenPeaceVideoURL},
  220.     {'pattern': 'http://([^/]+\.)?break.com/', 'func': _scrapeBreakVideoURL},
  221.  
  222. ]
  223.