InfoExtractors.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. import base64
  2. import datetime
  3. import itertools
  4. import netrc
  5. import os
  6. import re
  7. import socket
  8. import time
  9. import email.utils
  10. import xml.etree.ElementTree
  11. import random
  12. import math
  13. import operator
  14. import hashlib
  15. import binascii
  16. import urllib
  17. from .utils import *
  18. from .extractor.common import InfoExtractor, SearchInfoExtractor
  19. from .extractor.ard import ARDIE
  20. from .extractor.arte import ArteTvIE
  21. from .extractor.bliptv import BlipTVIE, BlipTVUserIE
  22. from .extractor.comedycentral import ComedyCentralIE
  23. from .extractor.collegehumor import CollegeHumorIE
  24. from .extractor.dailymotion import DailymotionIE
  25. from .extractor.depositfiles import DepositFilesIE
  26. from .extractor.eighttracks import EightTracksIE
  27. from .extractor.escapist import EscapistIE
  28. from .extractor.facebook import FacebookIE
  29. from .extractor.funnyordie import FunnyOrDieIE
  30. from .extractor.gametrailers import GametrailersIE
  31. from .extractor.generic import GenericIE
  32. from .extractor.googleplus import GooglePlusIE
  33. from .extractor.googlesearch import GoogleSearchIE
  34. from .extractor.infoq import InfoQIE
  35. from .extractor.justintv import JustinTVIE
  36. from .extractor.keek import KeekIE
  37. from .extractor.liveleak import LiveLeakIE
  38. from .extractor.metacafe import MetacafeIE
  39. from .extractor.mixcloud import MixcloudIE
  40. from .extractor.mtv import MTVIE
  41. from .extractor.myspass import MySpassIE
  42. from .extractor.myvideo import MyVideoIE
  43. from .extractor.nba import NBAIE
  44. from .extractor.statigram import StatigramIE
  45. from .extractor.photobucket import PhotobucketIE
  46. from .extractor.pornotube import PornotubeIE
  47. from .extractor.rbmaradio import RBMARadioIE
  48. from .extractor.soundcloud import SoundcloudIE, SoundcloudSetIE
  49. from .extractor.spiegel import SpiegelIE
  50. from .extractor.stanfordoc import StanfordOpenClassroomIE
  51. from .extractor.steam import SteamIE
  52. from .extractor.ted import TEDIE
  53. from .extractor.tumblr import TumblrIE
  54. from .extractor.ustream import UstreamIE
  55. from .extractor.vimeo import VimeoIE
  56. from .extractor.worldstarhiphop import WorldStarHipHopIE
  57. from .extractor.xnxx import XNXXIE
  58. from .extractor.xvideos import XVideosIE
  59. from .extractor.yahoo import YahooIE, YahooSearchIE
  60. from .extractor.youjizz import YouJizzIE
  61. from .extractor.youku import YoukuIE
  62. from .extractor.youporn import YouPornIE
  63. from .extractor.youtube import YoutubeIE, YoutubePlaylistIE, YoutubeSearchIE, YoutubeUserIE, YoutubeChannelIE
  64. from .extractor.zdf import ZDFIE
  65. class BandcampIE(InfoExtractor):
  66. _VALID_URL = r'http://.*?\.bandcamp\.com/track/(?P<title>.*)'
  67. def _real_extract(self, url):
  68. mobj = re.match(self._VALID_URL, url)
  69. title = mobj.group('title')
  70. webpage = self._download_webpage(url, title)
  71. # We get the link to the free download page
  72. m_download = re.search(r'freeDownloadPage: "(.*?)"', webpage)
  73. if m_download is None:
  74. raise ExtractorError(u'No free songs found')
  75. download_link = m_download.group(1)
  76. id = re.search(r'var TralbumData = {(.*?)id: (?P<id>\d*?)$',
  77. webpage, re.MULTILINE|re.DOTALL).group('id')
  78. download_webpage = self._download_webpage(download_link, id,
  79. 'Downloading free downloads page')
  80. # We get the dictionary of the track from some javascrip code
  81. info = re.search(r'items: (.*?),$',
  82. download_webpage, re.MULTILINE).group(1)
  83. info = json.loads(info)[0]
  84. # We pick mp3-320 for now, until format selection can be easily implemented.
  85. mp3_info = info[u'downloads'][u'mp3-320']
  86. # If we try to use this url it says the link has expired
  87. initial_url = mp3_info[u'url']
  88. re_url = r'(?P<server>http://(.*?)\.bandcamp\.com)/download/track\?enc=mp3-320&fsig=(?P<fsig>.*?)&id=(?P<id>.*?)&ts=(?P<ts>.*)$'
  89. m_url = re.match(re_url, initial_url)
  90. #We build the url we will use to get the final track url
  91. # This url is build in Bandcamp in the script download_bunde_*.js
  92. request_url = '%s/statdownload/track?enc=mp3-320&fsig=%s&id=%s&ts=%s&.rand=665028774616&.vrs=1' % (m_url.group('server'), m_url.group('fsig'), id, m_url.group('ts'))
  93. final_url_webpage = self._download_webpage(request_url, id, 'Requesting download url')
  94. # If we could correctly generate the .rand field the url would be
  95. #in the "download_url" key
  96. final_url = re.search(r'"retry_url":"(.*?)"', final_url_webpage).group(1)
  97. track_info = {'id':id,
  98. 'title' : info[u'title'],
  99. 'ext' : 'mp3',
  100. 'url' : final_url,
  101. 'thumbnail' : info[u'thumb_url'],
  102. 'uploader' : info[u'artist']
  103. }
  104. return [track_info]
  105. class RedTubeIE(InfoExtractor):
  106. """Information Extractor for redtube"""
  107. _VALID_URL = r'(?:http://)?(?:www\.)?redtube\.com/(?P<id>[0-9]+)'
  108. def _real_extract(self,url):
  109. mobj = re.match(self._VALID_URL, url)
  110. if mobj is None:
  111. raise ExtractorError(u'Invalid URL: %s' % url)
  112. video_id = mobj.group('id')
  113. video_extension = 'mp4'
  114. webpage = self._download_webpage(url, video_id)
  115. self.report_extraction(video_id)
  116. video_url = self._html_search_regex(r'<source src="(.+?)" type="video/mp4">',
  117. webpage, u'video URL')
  118. video_title = self._html_search_regex('<h1 class="videoTitle slidePanelMovable">(.+?)</h1>',
  119. webpage, u'title')
  120. return [{
  121. 'id': video_id,
  122. 'url': video_url,
  123. 'ext': video_extension,
  124. 'title': video_title,
  125. }]
  126. class InaIE(InfoExtractor):
  127. """Information Extractor for Ina.fr"""
  128. _VALID_URL = r'(?:http://)?(?:www\.)?ina\.fr/video/(?P<id>I[0-9]+)/.*'
  129. def _real_extract(self,url):
  130. mobj = re.match(self._VALID_URL, url)
  131. video_id = mobj.group('id')
  132. mrss_url='http://player.ina.fr/notices/%s.mrss' % video_id
  133. video_extension = 'mp4'
  134. webpage = self._download_webpage(mrss_url, video_id)
  135. self.report_extraction(video_id)
  136. video_url = self._html_search_regex(r'<media:player url="(?P<mp4url>http://mp4.ina.fr/[^"]+\.mp4)',
  137. webpage, u'video URL')
  138. video_title = self._search_regex(r'<title><!\[CDATA\[(?P<titre>.*?)]]></title>',
  139. webpage, u'title')
  140. return [{
  141. 'id': video_id,
  142. 'url': video_url,
  143. 'ext': video_extension,
  144. 'title': video_title,
  145. }]
  146. class HowcastIE(InfoExtractor):
  147. """Information Extractor for Howcast.com"""
  148. _VALID_URL = r'(?:https?://)?(?:www\.)?howcast\.com/videos/(?P<id>\d+)'
  149. def _real_extract(self, url):
  150. mobj = re.match(self._VALID_URL, url)
  151. video_id = mobj.group('id')
  152. webpage_url = 'http://www.howcast.com/videos/' + video_id
  153. webpage = self._download_webpage(webpage_url, video_id)
  154. self.report_extraction(video_id)
  155. video_url = self._search_regex(r'\'?file\'?: "(http://mobile-media\.howcast\.com/[0-9]+\.mp4)',
  156. webpage, u'video URL')
  157. video_title = self._html_search_regex(r'<meta content=(?:"([^"]+)"|\'([^\']+)\') property=\'og:title\'',
  158. webpage, u'title')
  159. video_description = self._html_search_regex(r'<meta content=(?:"([^"]+)"|\'([^\']+)\') name=\'description\'',
  160. webpage, u'description', fatal=False)
  161. thumbnail = self._html_search_regex(r'<meta content=\'(.+?)\' property=\'og:image\'',
  162. webpage, u'thumbnail', fatal=False)
  163. return [{
  164. 'id': video_id,
  165. 'url': video_url,
  166. 'ext': 'mp4',
  167. 'title': video_title,
  168. 'description': video_description,
  169. 'thumbnail': thumbnail,
  170. }]
  171. class VineIE(InfoExtractor):
  172. """Information Extractor for Vine.co"""
  173. _VALID_URL = r'(?:https?://)?(?:www\.)?vine\.co/v/(?P<id>\w+)'
  174. def _real_extract(self, url):
  175. mobj = re.match(self._VALID_URL, url)
  176. video_id = mobj.group('id')
  177. webpage_url = 'https://vine.co/v/' + video_id
  178. webpage = self._download_webpage(webpage_url, video_id)
  179. self.report_extraction(video_id)
  180. video_url = self._html_search_regex(r'<meta property="twitter:player:stream" content="(.+?)"',
  181. webpage, u'video URL')
  182. video_title = self._html_search_regex(r'<meta property="og:title" content="(.+?)"',
  183. webpage, u'title')
  184. thumbnail = self._html_search_regex(r'<meta property="og:image" content="(.+?)(\?.*?)?"',
  185. webpage, u'thumbnail', fatal=False)
  186. uploader = self._html_search_regex(r'<div class="user">.*?<h2>(.+?)</h2>',
  187. webpage, u'uploader', fatal=False, flags=re.DOTALL)
  188. return [{
  189. 'id': video_id,
  190. 'url': video_url,
  191. 'ext': 'mp4',
  192. 'title': video_title,
  193. 'thumbnail': thumbnail,
  194. 'uploader': uploader,
  195. }]
  196. class FlickrIE(InfoExtractor):
  197. """Information Extractor for Flickr videos"""
  198. _VALID_URL = r'(?:https?://)?(?:www\.)?flickr\.com/photos/(?P<uploader_id>[\w\-_@]+)/(?P<id>\d+).*'
  199. def _real_extract(self, url):
  200. mobj = re.match(self._VALID_URL, url)
  201. video_id = mobj.group('id')
  202. video_uploader_id = mobj.group('uploader_id')
  203. webpage_url = 'http://www.flickr.com/photos/' + video_uploader_id + '/' + video_id
  204. webpage = self._download_webpage(webpage_url, video_id)
  205. secret = self._search_regex(r"photo_secret: '(\w+)'", webpage, u'secret')
  206. first_url = 'https://secure.flickr.com/apps/video/video_mtl_xml.gne?v=x&photo_id=' + video_id + '&secret=' + secret + '&bitrate=700&target=_self'
  207. first_xml = self._download_webpage(first_url, video_id, 'Downloading first data webpage')
  208. node_id = self._html_search_regex(r'<Item id="id">(\d+-\d+)</Item>',
  209. first_xml, u'node_id')
  210. second_url = 'https://secure.flickr.com/video_playlist.gne?node_id=' + node_id + '&tech=flash&mode=playlist&bitrate=700&secret=' + secret + '&rd=video.yahoo.com&noad=1'
  211. second_xml = self._download_webpage(second_url, video_id, 'Downloading second data webpage')
  212. self.report_extraction(video_id)
  213. mobj = re.search(r'<STREAM APP="(.+?)" FULLPATH="(.+?)"', second_xml)
  214. if mobj is None:
  215. raise ExtractorError(u'Unable to extract video url')
  216. video_url = mobj.group(1) + unescapeHTML(mobj.group(2))
  217. video_title = self._html_search_regex(r'<meta property="og:title" content=(?:"([^"]+)"|\'([^\']+)\')',
  218. webpage, u'video title')
  219. video_description = self._html_search_regex(r'<meta property="og:description" content=(?:"([^"]+)"|\'([^\']+)\')',
  220. webpage, u'description', fatal=False)
  221. thumbnail = self._html_search_regex(r'<meta property="og:image" content=(?:"([^"]+)"|\'([^\']+)\')',
  222. webpage, u'thumbnail', fatal=False)
  223. return [{
  224. 'id': video_id,
  225. 'url': video_url,
  226. 'ext': 'mp4',
  227. 'title': video_title,
  228. 'description': video_description,
  229. 'thumbnail': thumbnail,
  230. 'uploader_id': video_uploader_id,
  231. }]
  232. class TeamcocoIE(InfoExtractor):
  233. _VALID_URL = r'http://teamcoco\.com/video/(?P<url_title>.*)'
  234. def _real_extract(self, url):
  235. mobj = re.match(self._VALID_URL, url)
  236. if mobj is None:
  237. raise ExtractorError(u'Invalid URL: %s' % url)
  238. url_title = mobj.group('url_title')
  239. webpage = self._download_webpage(url, url_title)
  240. video_id = self._html_search_regex(r'<article class="video" data-id="(\d+?)"',
  241. webpage, u'video id')
  242. self.report_extraction(video_id)
  243. video_title = self._html_search_regex(r'<meta property="og:title" content="(.+?)"',
  244. webpage, u'title')
  245. thumbnail = self._html_search_regex(r'<meta property="og:image" content="(.+?)"',
  246. webpage, u'thumbnail', fatal=False)
  247. video_description = self._html_search_regex(r'<meta property="og:description" content="(.*?)"',
  248. webpage, u'description', fatal=False)
  249. data_url = 'http://teamcoco.com/cvp/2.0/%s.xml' % video_id
  250. data = self._download_webpage(data_url, video_id, 'Downloading data webpage')
  251. video_url = self._html_search_regex(r'<file type="high".*?>(.*?)</file>',
  252. data, u'video URL')
  253. return [{
  254. 'id': video_id,
  255. 'url': video_url,
  256. 'ext': 'mp4',
  257. 'title': video_title,
  258. 'thumbnail': thumbnail,
  259. 'description': video_description,
  260. }]
  261. class XHamsterIE(InfoExtractor):
  262. """Information Extractor for xHamster"""
  263. _VALID_URL = r'(?:http://)?(?:www.)?xhamster\.com/movies/(?P<id>[0-9]+)/.*\.html'
  264. def _real_extract(self,url):
  265. mobj = re.match(self._VALID_URL, url)
  266. video_id = mobj.group('id')
  267. mrss_url = 'http://xhamster.com/movies/%s/.html' % video_id
  268. webpage = self._download_webpage(mrss_url, video_id)
  269. mobj = re.search(r'\'srv\': \'(?P<server>[^\']*)\',\s*\'file\': \'(?P<file>[^\']+)\',', webpage)
  270. if mobj is None:
  271. raise ExtractorError(u'Unable to extract media URL')
  272. if len(mobj.group('server')) == 0:
  273. video_url = compat_urllib_parse.unquote(mobj.group('file'))
  274. else:
  275. video_url = mobj.group('server')+'/key='+mobj.group('file')
  276. video_extension = video_url.split('.')[-1]
  277. video_title = self._html_search_regex(r'<title>(?P<title>.+?) - xHamster\.com</title>',
  278. webpage, u'title')
  279. # Can't see the description anywhere in the UI
  280. # video_description = self._html_search_regex(r'<span>Description: </span>(?P<description>[^<]+)',
  281. # webpage, u'description', fatal=False)
  282. # if video_description: video_description = unescapeHTML(video_description)
  283. mobj = re.search(r'hint=\'(?P<upload_date_Y>[0-9]{4})-(?P<upload_date_m>[0-9]{2})-(?P<upload_date_d>[0-9]{2}) [0-9]{2}:[0-9]{2}:[0-9]{2} [A-Z]{3,4}\'', webpage)
  284. if mobj:
  285. video_upload_date = mobj.group('upload_date_Y')+mobj.group('upload_date_m')+mobj.group('upload_date_d')
  286. else:
  287. video_upload_date = None
  288. self._downloader.report_warning(u'Unable to extract upload date')
  289. video_uploader_id = self._html_search_regex(r'<a href=\'/user/[^>]+>(?P<uploader_id>[^<]+)',
  290. webpage, u'uploader id', default=u'anonymous')
  291. video_thumbnail = self._search_regex(r'\'image\':\'(?P<thumbnail>[^\']+)\'',
  292. webpage, u'thumbnail', fatal=False)
  293. return [{
  294. 'id': video_id,
  295. 'url': video_url,
  296. 'ext': video_extension,
  297. 'title': video_title,
  298. # 'description': video_description,
  299. 'upload_date': video_upload_date,
  300. 'uploader_id': video_uploader_id,
  301. 'thumbnail': video_thumbnail
  302. }]
  303. class HypemIE(InfoExtractor):
  304. """Information Extractor for hypem"""
  305. _VALID_URL = r'(?:http://)?(?:www\.)?hypem\.com/track/([^/]+)/([^/]+)'
  306. def _real_extract(self, url):
  307. mobj = re.match(self._VALID_URL, url)
  308. if mobj is None:
  309. raise ExtractorError(u'Invalid URL: %s' % url)
  310. track_id = mobj.group(1)
  311. data = { 'ax': 1, 'ts': time.time() }
  312. data_encoded = compat_urllib_parse.urlencode(data)
  313. complete_url = url + "?" + data_encoded
  314. request = compat_urllib_request.Request(complete_url)
  315. response, urlh = self._download_webpage_handle(request, track_id, u'Downloading webpage with the url')
  316. cookie = urlh.headers.get('Set-Cookie', '')
  317. self.report_extraction(track_id)
  318. html_tracks = self._html_search_regex(r'<script type="application/json" id="displayList-data">(.*?)</script>',
  319. response, u'tracks', flags=re.MULTILINE|re.DOTALL).strip()
  320. try:
  321. track_list = json.loads(html_tracks)
  322. track = track_list[u'tracks'][0]
  323. except ValueError:
  324. raise ExtractorError(u'Hypemachine contained invalid JSON.')
  325. key = track[u"key"]
  326. track_id = track[u"id"]
  327. artist = track[u"artist"]
  328. title = track[u"song"]
  329. serve_url = "http://hypem.com/serve/source/%s/%s" % (compat_str(track_id), compat_str(key))
  330. request = compat_urllib_request.Request(serve_url, "" , {'Content-Type': 'application/json'})
  331. request.add_header('cookie', cookie)
  332. song_data_json = self._download_webpage(request, track_id, u'Downloading metadata')
  333. try:
  334. song_data = json.loads(song_data_json)
  335. except ValueError:
  336. raise ExtractorError(u'Hypemachine contained invalid JSON.')
  337. final_url = song_data[u"url"]
  338. return [{
  339. 'id': track_id,
  340. 'url': final_url,
  341. 'ext': "mp3",
  342. 'title': title,
  343. 'artist': artist,
  344. }]
  345. class Vbox7IE(InfoExtractor):
  346. """Information Extractor for Vbox7"""
  347. _VALID_URL = r'(?:http://)?(?:www\.)?vbox7\.com/play:([^/]+)'
  348. def _real_extract(self,url):
  349. mobj = re.match(self._VALID_URL, url)
  350. if mobj is None:
  351. raise ExtractorError(u'Invalid URL: %s' % url)
  352. video_id = mobj.group(1)
  353. redirect_page, urlh = self._download_webpage_handle(url, video_id)
  354. new_location = self._search_regex(r'window\.location = \'(.*)\';', redirect_page, u'redirect location')
  355. redirect_url = urlh.geturl() + new_location
  356. webpage = self._download_webpage(redirect_url, video_id, u'Downloading redirect page')
  357. title = self._html_search_regex(r'<title>(.*)</title>',
  358. webpage, u'title').split('/')[0].strip()
  359. ext = "flv"
  360. info_url = "http://vbox7.com/play/magare.do"
  361. data = compat_urllib_parse.urlencode({'as3':'1','vid':video_id})
  362. info_request = compat_urllib_request.Request(info_url, data)
  363. info_request.add_header('Content-Type', 'application/x-www-form-urlencoded')
  364. info_response = self._download_webpage(info_request, video_id, u'Downloading info webpage')
  365. if info_response is None:
  366. raise ExtractorError(u'Unable to extract the media url')
  367. (final_url, thumbnail_url) = map(lambda x: x.split('=')[1], info_response.split('&'))
  368. return [{
  369. 'id': video_id,
  370. 'url': final_url,
  371. 'ext': ext,
  372. 'title': title,
  373. 'thumbnail': thumbnail_url,
  374. }]
  375. def gen_extractors():
  376. """ Return a list of an instance of every supported extractor.
  377. The order does matter; the first extractor matched is the one handling the URL.
  378. """
  379. return [
  380. YoutubePlaylistIE(),
  381. YoutubeChannelIE(),
  382. YoutubeUserIE(),
  383. YoutubeSearchIE(),
  384. YoutubeIE(),
  385. MetacafeIE(),
  386. DailymotionIE(),
  387. GoogleSearchIE(),
  388. PhotobucketIE(),
  389. YahooIE(),
  390. YahooSearchIE(),
  391. DepositFilesIE(),
  392. FacebookIE(),
  393. BlipTVIE(),
  394. BlipTVUserIE(),
  395. VimeoIE(),
  396. MyVideoIE(),
  397. ComedyCentralIE(),
  398. EscapistIE(),
  399. CollegeHumorIE(),
  400. XVideosIE(),
  401. SoundcloudSetIE(),
  402. SoundcloudIE(),
  403. InfoQIE(),
  404. MixcloudIE(),
  405. StanfordOpenClassroomIE(),
  406. MTVIE(),
  407. YoukuIE(),
  408. XNXXIE(),
  409. YouJizzIE(),
  410. PornotubeIE(),
  411. YouPornIE(),
  412. GooglePlusIE(),
  413. ArteTvIE(),
  414. NBAIE(),
  415. WorldStarHipHopIE(),
  416. JustinTVIE(),
  417. FunnyOrDieIE(),
  418. SteamIE(),
  419. UstreamIE(),
  420. RBMARadioIE(),
  421. EightTracksIE(),
  422. KeekIE(),
  423. TEDIE(),
  424. MySpassIE(),
  425. SpiegelIE(),
  426. LiveLeakIE(),
  427. ARDIE(),
  428. ZDFIE(),
  429. TumblrIE(),
  430. BandcampIE(),
  431. RedTubeIE(),
  432. InaIE(),
  433. HowcastIE(),
  434. VineIE(),
  435. FlickrIE(),
  436. TeamcocoIE(),
  437. XHamsterIE(),
  438. HypemIE(),
  439. Vbox7IE(),
  440. GametrailersIE(),
  441. StatigramIE(),
  442. GenericIE()
  443. ]
  444. def get_info_extractor(ie_name):
  445. """Returns the info extractor class with the given ie_name"""
  446. return globals()[ie_name+'IE']