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.

123 lines
4.1 KiB

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