weibo.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. import json
  5. import random as rnd
  6. import re
  7. from ..compat import (
  8. compat_urllib_parse_urlencode as urlencode,
  9. compat_urllib_request as Request,
  10. compat_urlparse as parse,
  11. )
  12. from ..utils import (
  13. js_to_json,
  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. headers = {
  28. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
  29. 'Accept-Encoding': 'gzip, deflate, br',
  30. 'Accept-Language': 'en,zh-CN;q=0.9,zh;q=0.8',
  31. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36',
  32. 'Upgrade-Insecure-Requests': '1',
  33. }
  34. # to get Referer url for genvisitor
  35. webpage, urlh = self._download_webpage_handle(url, video_id, headers=headers, note="first visit the page")
  36. visitor_url = urlh.geturl()
  37. data = urlencode({
  38. "cb": "gen_callback",
  39. "fp": '{"os":"2","browser":"Gecko57,0,0,0","fonts":"undefined","screenInfo":"1440*900*24","plugins":""}',
  40. }).encode()
  41. headers = {
  42. 'Accept-Encoding': 'gzip, deflate, br',
  43. 'Accept': '*/*',
  44. 'Referer': visitor_url,
  45. }
  46. r_genvisitor = Request(
  47. 'https://passport.weibo.com/visitor/genvisitor',
  48. data=data,
  49. headers=headers,
  50. method='POST'
  51. )
  52. webpage, urlh = self._download_webpage_handle(r_genvisitor, video_id, note="gen visitor")
  53. p = webpage.split("&&")[1] # split "gen_callback && gen_callback(...)"
  54. i1 = p.find('{')
  55. i2 = p.rfind('}')
  56. j = p[i1:i2 + 1] # get JSON object
  57. d = json.loads(j)
  58. tid = d["data"]["tid"]
  59. cnfd = "%03d" % d["data"]["confidence"]
  60. param = urlencode({
  61. 'a': 'incarnate',
  62. 't': tid,
  63. 'w': 2,
  64. 'c': cnfd,
  65. 'cb': 'cross_domain',
  66. 'from': 'weibo',
  67. '_rand': rnd.random()
  68. })
  69. gencallback_url = "https://passport.weibo.com/visitor/visitor?" + param
  70. webpage, urlh = self._download_webpage_handle(gencallback_url, video_id, note="gen callback")
  71. webpage, urlh = self._download_webpage_handle(url, video_id, headers=headers, note="retry to visit the page")
  72. # TODO more code goes here, for example ...
  73. title = self._html_search_regex(r'<title>(.+?)</title>', webpage, 'title')
  74. video_sources_text = self._search_regex("video-sources=\\\\\"(.+?)\"", webpage, 'video_sources')
  75. video_formats = parse.parse_qs(video_sources_text)
  76. formats = []
  77. supported_resolutions = ['720', '480']
  78. for res in supported_resolutions:
  79. f = video_formats.get(res)
  80. if isinstance(f, list):
  81. if len(f) > 0:
  82. vid_url = f[0]
  83. formats.append({
  84. 'url': vid_url,
  85. 'format': 'mp4',
  86. 'height': int(res),
  87. })
  88. self._sort_formats(formats)
  89. uploader = self._og_search_property('nick-name', webpage, 'uploader', default=None)
  90. return {
  91. 'id': video_id,
  92. 'title': title,
  93. 'uploader': uploader,
  94. 'formats': formats
  95. # TODO more properties (see youtube_dl/extractor/common.py)
  96. }
  97. class WeiboMobileIE(InfoExtractor):
  98. _VALID_URL = r'https?://m.weibo.cn/status/(?P<id>[0-9]+)(\?.+)?'
  99. _TEST = {
  100. 'url': 'https://m.weibo.cn/status/4189191225395228?wm=3333_2001&sourcetype=weixin&featurecode=newtitle&from=singlemessage&isappinstalled=0',
  101. 'info_dict': {
  102. 'id': '4189191225395228',
  103. 'ext': 'mp4',
  104. 'title': '午睡当然是要甜甜蜜蜜的啦',
  105. 'uploader': '柴犬柴犬'
  106. }
  107. }
  108. def _real_extract(self, url):
  109. video_id = self._match_id(url)
  110. headers = {
  111. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
  112. 'Accept-Encoding': 'gzip, deflate, br',
  113. 'Accept-Language': 'en,zh-CN;q=0.9,zh;q=0.8',
  114. 'Upgrade-Insecure-Requests': '1',
  115. }
  116. # to get Referer url for genvisitor
  117. webpage, urlh = self._download_webpage_handle(url, video_id, headers=headers, note="visit the page")
  118. js_code = self._search_regex(r'var\s+\$render_data\s*=\s*\[({.*})\]\[0\] \|\| {};', webpage, 'js_code', flags=re.DOTALL)
  119. weibo_info = self._parse_json(js_code, video_id, transform_source=js_to_json)
  120. page_info = weibo_info['status']['page_info']
  121. title = weibo_info['status']['status_title']
  122. format = {
  123. 'url': page_info['media_info']['stream_url'],
  124. 'format': 'mp4',
  125. }
  126. formats = [format]
  127. uploader = weibo_info['status']['user']['screen_name']
  128. return {
  129. 'id': video_id,
  130. 'title': title,
  131. 'uploader': uploader,
  132. 'formats': formats
  133. # TODO more properties (see youtube_dl/extractor/common.py)
  134. }