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.

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