InfoExtractors.py 18 KB

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