weibo.py 5.8 KB

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