InfoExtractors.py 16 KB

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