]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/funimation.py
[funimation] Real UA is required for login
[yt-dlp.git] / youtube_dl / extractor / funimation.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3 from .common import InfoExtractor
4 from ..compat import compat_HTTPError
5 from ..utils import (
6 encode_dict,
7 sanitized_Request,
8 ExtractorError,
9 urlencode_postdata
10 )
11 import re
12
13
14 class FunimationIE(InfoExtractor):
15 _VALID_URL = r'https?://(?:www\.)?funimation\.com/shows/.+[^ ]/videos/official/(?P<id>[^?]+)'
16
17 _TEST = {
18 'url': 'http://www.funimation.com/shows/air/videos/official/breeze',
19 'info_dict': {
20 'id': 'AIRENG0001',
21 'title': 'Air - 1 - Breeze ',
22 'ext': 'mp4',
23 'thumbnail': 'http://www.funimation.com/admin/uploads/default/recap_thumbnails/7555590/home_spotlight/AIR0001.jpg',
24 'description': 'Travelling puppeteer Yukito arrives in a small town where he hopes to earn money through the magic of his puppets. When a young girl named Misuzu lures him to her home with the promise of food, his life changes forever. ',
25 }
26 }
27
28 def _login(self):
29 (username, password) = self._get_login_info()
30 if username is None:
31 return
32 login_url = 'http://www.funimation.com/login'
33 data = urlencode_postdata(encode_dict({
34 'email_field': username,
35 'password_field': password,
36 }))
37 login_request = sanitized_Request(login_url, data, headers={
38 'User-Agent': 'Mozilla/5.0 (Windows NT 5.2; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0',
39 'Content-Type': 'application/x-www-form-urlencoded'
40 })
41 try:
42 login = self._download_webpage(
43 login_request, None, 'Logging in as %s' % username)
44 except ExtractorError as e:
45 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
46 raise ExtractorError('Funimation is not available in your region.', expected=True)
47 raise
48 if re.search(r'<meta property="og:url" content="http://www.funimation.com/login"/>', login) is not None:
49 raise ExtractorError('Unable to login, wrong username or password.', expected=True)
50
51 def _real_initialize(self):
52 self._login()
53
54 def _real_extract(self, url):
55 mobj = re.match(self._VALID_URL, url)
56 video_id = mobj.group('id')
57 try:
58 webpage = self._download_webpage(url, video_id)
59 except ExtractorError as e:
60 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
61 raise ExtractorError('Funimation is not available in your region.', expected=True)
62 raise
63 if re.search(r'"sdUrl":"http', webpage) is None:
64 raise ExtractorError('You are not logged-in or the stream requires subscription.', expected=True)
65
66 m3u8 = self._search_regex(r'".+Url":"(.+?m3u8)"', webpage, 'm3u8') + self._search_regex(r'"authToken":"(.+?)"', webpage, 'm3u8')
67 formats = self._extract_m3u8_formats(m3u8.replace('\\', ''), video_id, ext='mp4', entry_protocol='m3u8_native')
68
69 video_show = self._search_regex(r'"artist":"(.+?)"', webpage, 'video_show')
70 video_track = self._search_regex(r'"videoNumber":"(\d+).0"', webpage, 'video_track')
71 video_title = self._search_regex(r'"title":"({0}.+?)"'.format(video_track), webpage, 'video_title')
72 video_id = self._search_regex(r'"FUNImationID":"(.+?)"', webpage, 'video_id')
73
74 return {
75 'id': video_id,
76 'title': video_show + ' - ' + video_title + ' ',
77 'formats': formats,
78 'thumbnail': self._og_search_thumbnail(webpage),
79 'description': self._og_search_description(webpage)
80 }