weibo.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. import json
  5. import random
  6. import re
  7. from ..compat import (
  8. compat_urlparse,
  9. )
  10. from ..utils import (
  11. js_to_json,
  12. strip_jsonp,
  13. urlencode_postdata,
  14. )
  15. class WeiboIE(InfoExtractor):
  16. _VALID_URL = r'https?://weibo\.com/[0-9]+/(?P<id>[a-zA-Z0-9]+)'
  17. _TEST = {
  18. 'url': 'https://weibo.com/6275294458/Fp6RGfbff?type=comment',
  19. 'info_dict': {
  20. 'id': 'Fp6RGfbff',
  21. 'ext': 'mp4',
  22. 'title': 'You should have servants to massage you,... 来自Hosico_猫 - 微博',
  23. }
  24. }
  25. def _real_extract(self, url):
  26. video_id = self._match_id(url)
  27. # to get Referer url for genvisitor
  28. webpage, urlh = self._download_webpage_handle(url, video_id, note="first visit the page")
  29. visitor_url = urlh.geturl()
  30. headers = {
  31. 'Referer': visitor_url
  32. }
  33. fp = {
  34. "os": "2",
  35. "browser": "Gecko57,0,0,0",
  36. "fonts": "undefined",
  37. "screenInfo": "1440*900*24",
  38. "plugins": ""
  39. }
  40. data = urlencode_postdata({
  41. "cb": "gen_callback",
  42. "fp": json.dumps(fp),
  43. })
  44. genvisitor_url = 'https://passport.weibo.com/visitor/genvisitor'
  45. webpage = self._download_webpage(genvisitor_url, video_id, data=data, headers=headers, note="gen visitor")
  46. p = strip_jsonp(webpage)
  47. i1 = p.find('{')
  48. i2 = p.rfind('}')
  49. j = p[i1:i2 + 1] # get JSON object
  50. d = json.loads(j)
  51. tid = d["data"]["tid"]
  52. cnfd = "%03d" % d["data"]["confidence"]
  53. query = {
  54. 'a': 'incarnate',
  55. 't': tid,
  56. 'w': 2,
  57. 'c': cnfd,
  58. 'cb': 'cross_domain',
  59. 'from': 'weibo',
  60. '_rand': random.random()
  61. }
  62. gencallback_url = "https://passport.weibo.com/visitor/visitor"
  63. self._download_webpage(gencallback_url, video_id, note="gen callback", query=query)
  64. webpage = self._download_webpage(url, video_id, note="retry to visit the page")
  65. title = self._html_search_regex(r'<title>(.+?)</title>', webpage, 'title')
  66. video_sources_text = self._search_regex(r'video-sources=\\\"(.+?)\"', webpage, 'video_sources')
  67. video_formats = compat_urlparse.parse_qs(video_sources_text)
  68. formats = []
  69. supported_resolutions = ('720', '480')
  70. for res in supported_resolutions:
  71. f = video_formats.get(res)
  72. if isinstance(f, list):
  73. if len(f) > 0:
  74. vid_url = f[0]
  75. formats.append({
  76. 'url': vid_url,
  77. 'format': 'mp4',
  78. 'height': int(res),
  79. })
  80. self._sort_formats(formats)
  81. uploader = self._og_search_property('nick-name', webpage, 'uploader', default=None)
  82. return {
  83. 'id': video_id,
  84. 'title': title,
  85. 'uploader': uploader,
  86. 'formats': formats
  87. }
  88. class WeiboMobileIE(InfoExtractor):
  89. _VALID_URL = r'https?://m\.weibo\.cn/status/(?P<id>[0-9]+)(\?.+)?'
  90. _TEST = {
  91. 'url': 'https://m.weibo.cn/status/4189191225395228?wm=3333_2001&sourcetype=weixin&featurecode=newtitle&from=singlemessage&isappinstalled=0',
  92. 'info_dict': {
  93. 'id': '4189191225395228',
  94. 'ext': 'mp4',
  95. 'title': '午睡当然是要甜甜蜜蜜的啦',
  96. 'uploader': '柴犬柴犬'
  97. }
  98. }
  99. def _real_extract(self, url):
  100. video_id = self._match_id(url)
  101. # to get Referer url for genvisitor
  102. webpage = self._download_webpage(url, video_id, note="visit the page")
  103. js_code = self._search_regex(r'var\s+\$render_data\s*=\s*\[({.*})\]\[0\] \|\| {};', webpage, 'js_code', flags=re.DOTALL)
  104. weibo_info = self._parse_json(js_code, video_id, transform_source=js_to_json)
  105. page_info = weibo_info.get('status').get('page_info')
  106. title = weibo_info.get('status').get('status_title')
  107. uploader = weibo_info.get('status').get('user').get('screen_name')
  108. return {
  109. 'id': video_id,
  110. 'title': title,
  111. 'uploader': uploader,
  112. 'url': page_info['media_info']['stream_url']
  113. }