You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.4 KiB

10 years ago
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_urlparse
  6. from ..utils import (
  7. float_or_none,
  8. month_by_abbreviation,
  9. ExtractorError,
  10. )
  11. class YamIE(InfoExtractor):
  12. _VALID_URL = r'http://mymedia.yam.com/m/(?P<id>\d+)'
  13. _TESTS = [{
  14. # An audio hosted on Yam
  15. 'url': 'http://mymedia.yam.com/m/2283921',
  16. 'md5': 'c011b8e262a52d5473d9c2e3c9963b9c',
  17. 'info_dict': {
  18. 'id': '2283921',
  19. 'ext': 'mp3',
  20. 'title': '發現 - 趙薇 京華煙雲主題曲',
  21. 'uploader_id': 'princekt',
  22. 'upload_date': '20080807',
  23. 'duration': 313.0,
  24. }
  25. }, {
  26. # An external video hosted on YouTube
  27. 'url': 'http://mymedia.yam.com/m/3599430',
  28. 'md5': '03127cf10d8f35d120a9e8e52e3b17c6',
  29. 'info_dict': {
  30. 'id': 'CNpEoQlrIgA',
  31. 'ext': 'mp4',
  32. 'upload_date': '20150306',
  33. 'uploader': '新莊社大瑜伽社',
  34. 'description': 'md5:11e2e405311633ace874f2e6226c8b17',
  35. 'uploader_id': '2323agoy',
  36. 'title': '20090412陽明山二子坪-1',
  37. },
  38. 'skip': 'Video does not exist',
  39. }, {
  40. 'url': 'http://mymedia.yam.com/m/3598173',
  41. 'info_dict': {
  42. 'id': '3598173',
  43. 'ext': 'mp4',
  44. },
  45. 'skip': 'cause Yam system error',
  46. }, {
  47. 'url': 'http://mymedia.yam.com/m/3599437',
  48. 'info_dict': {
  49. 'id': '3599437',
  50. 'ext': 'mp4',
  51. },
  52. 'skip': 'invalid YouTube URL',
  53. }]
  54. def _real_extract(self, url):
  55. video_id = self._match_id(url)
  56. page = self._download_webpage(url, video_id)
  57. # Check for errors
  58. system_msg = self._html_search_regex(
  59. r'系統訊息(?:<br>|\n|\r)*([^<>]+)<br>', page, 'system message',
  60. default=None)
  61. if system_msg:
  62. raise ExtractorError(system_msg, expected=True)
  63. # Is it hosted externally on YouTube?
  64. youtube_url = self._html_search_regex(
  65. r'<embed src="(http://www.youtube.com/[^"]+)"',
  66. page, 'YouTube url', default=None)
  67. if youtube_url:
  68. return self.url_result(youtube_url, 'Youtube')
  69. api_page = self._download_webpage(
  70. 'http://mymedia.yam.com/api/a/?pID=' + video_id, video_id,
  71. note='Downloading API page')
  72. api_result_obj = compat_urlparse.parse_qs(api_page)
  73. uploader_id = self._html_search_regex(
  74. r'<!-- 發表作者 -->:[\n ]+<a href="/([a-z]+)"',
  75. page, 'uploader id', fatal=False)
  76. mobj = re.search(r'<!-- 發表於 -->(?P<mon>[A-Z][a-z]{2}) ' +
  77. r'(?P<day>\d{1,2}), (?P<year>\d{4})', page)
  78. if mobj:
  79. upload_date = '%s%02d%02d' % (
  80. mobj.group('year'),
  81. month_by_abbreviation(mobj.group('mon')),
  82. int(mobj.group('day')))
  83. else:
  84. upload_date = None
  85. duration = float_or_none(api_result_obj['totaltime'][0], scale=1000)
  86. return {
  87. 'id': video_id,
  88. 'url': api_result_obj['mp3file'][0],
  89. 'title': self._html_search_meta('description', page),
  90. 'duration': duration,
  91. 'uploader_id': uploader_id,
  92. 'upload_date': upload_date,
  93. }