funimation.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. class FunimationIE(InfoExtractor):
  13. _VALID_URL = r'https?://(?:www\.)?funimation\.com/shows/.+[^ ]/videos/official/(?P<id>[^?]+)'
  14. _TEST = {
  15. 'url': 'http://www.funimation.com/shows/air/videos/official/breeze',
  16. 'info_dict': {
  17. 'id': 'AIRENG0001',
  18. 'title': 'Air - 1 - Breeze ',
  19. 'ext': 'mp4',
  20. 'thumbnail': 'http://www.funimation.com/admin/uploads/default/recap_thumbnails/7555590/home_spotlight/AIR0001.jpg',
  21. '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. ',
  22. }
  23. }
  24. def _login(self):
  25. (username, password) = self._get_login_info()
  26. if username is None:
  27. return
  28. login_url = 'http://www.funimation.com/login'
  29. data = urlencode_postdata(encode_dict({
  30. 'loginForm2': 'loginform',
  31. 'email_field': username,
  32. 'password_field': password,
  33. }))
  34. login_request = sanitized_Request(login_url, data)
  35. login_request.add_header('Content-Type', 'application/x-www-form-urlencoded')
  36. try:
  37. login = self._download_webpage(
  38. login_request, None, 'Logging in as %s' % username)
  39. except ExtractorError as e:
  40. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
  41. raise ExtractorError('Funimation is not available in your region.', expected=True)
  42. raise
  43. if re.search(r'<meta property="og:url" content="http://www.funimation.com/login"/>', login) is not None:
  44. raise ExtractorError('Unable to login, wrong username or password.', expected=True)
  45. def _real_initialize(self):
  46. self._login()
  47. def _real_extract(self, url):
  48. mobj = re.match(self._VALID_URL, url)
  49. video_id = mobj.group('id')
  50. try:
  51. webpage = self._download_webpage(url, video_id)
  52. except ExtractorError as e:
  53. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
  54. raise ExtractorError('Funimation is not available in your region.', expected=True)
  55. raise
  56. if re.search(r'"sdUrl":"http', webpage) is None:
  57. raise ExtractorError('You are not logged-in or the stream requires subscription.', expected=True)
  58. m3u8 = self._search_regex(r'".+Url":"(.+?m3u8)"', webpage, 'm3u8') + self._search_regex(r'"authToken":"(.+?)"', webpage, 'm3u8')
  59. formats = self._extract_m3u8_formats(m3u8.replace('\\', ''), video_id, ext='mp4', entry_protocol='m3u8_native')
  60. video_show = self._search_regex(r'"artist":"(.+?)"', webpage, 'video_show')
  61. video_track = self._search_regex(r'"videoNumber":"(\d+).0"', webpage, 'video_track')
  62. video_title = self._search_regex(r'"title":"({0}.+?)"'.format(video_track), webpage, 'video_title')
  63. video_id = self._search_regex(r'"FUNImationID":"(.+?)"', webpage, 'video_id')
  64. return {
  65. 'id': video_id,
  66. 'title': video_show + ' - ' + video_title + ' ',
  67. 'formats': formats,
  68. 'thumbnail': self._og_search_thumbnail(webpage),
  69. 'description': self._og_search_description(webpage)
  70. }