yam.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. }, {
  39. 'url': 'http://mymedia.yam.com/m/3598173',
  40. 'info_dict': {
  41. 'id': '3598173',
  42. 'ext': 'mp4',
  43. },
  44. 'skip': 'cause Yam system error',
  45. }, {
  46. 'url': 'http://mymedia.yam.com/m/3599437',
  47. 'info_dict': {
  48. 'id': '3599437',
  49. 'ext': 'mp4',
  50. },
  51. 'skip': 'invalid YouTube URL',
  52. }]
  53. def _real_extract(self, url):
  54. video_id = self._match_id(url)
  55. page = self._download_webpage(url, video_id)
  56. # Check for errors
  57. system_msg = self._html_search_regex(
  58. r'系統訊息(?:<br>|\n|\r)*([^<>]+)<br>', page, 'system message',
  59. default=None)
  60. if system_msg:
  61. raise ExtractorError(system_msg, expected=True)
  62. # Is it hosted externally on YouTube?
  63. youtube_url = self._html_search_regex(
  64. r'<embed src="(http://www.youtube.com/[^"]+)"',
  65. page, 'YouTube url', default=None)
  66. if youtube_url:
  67. return self.url_result(youtube_url, 'Youtube')
  68. api_page = self._download_webpage(
  69. 'http://mymedia.yam.com/api/a/?pID=' + video_id, video_id,
  70. note='Downloading API page')
  71. api_result_obj = compat_urlparse.parse_qs(api_page)
  72. uploader_id = self._html_search_regex(
  73. r'<!-- 發表作者 -->:[\n ]+<a href="/([a-z]+)"',
  74. page, 'uploader id', fatal=False)
  75. mobj = re.search(r'<!-- 發表於 -->(?P<mon>[A-Z][a-z]{2}) ' +
  76. r'(?P<day>\d{1,2}), (?P<year>\d{4})', page)
  77. if mobj:
  78. upload_date = '%s%02d%02d' % (
  79. mobj.group('year'),
  80. month_by_abbreviation(mobj.group('mon')),
  81. int(mobj.group('day')))
  82. else:
  83. upload_date = None
  84. duration = float_or_none(api_result_obj['totaltime'][0], scale=1000)
  85. return {
  86. 'id': video_id,
  87. 'url': api_result_obj['mp3file'][0],
  88. 'title': self._html_search_meta('description', page),
  89. 'duration': duration,
  90. 'uploader_id': uploader_id,
  91. 'upload_date': upload_date,
  92. }