weibo.py 4.3 KB

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