Skip to content

Instantly share code, notes, and snippets.

@Konano
Created January 8, 2024 06:37
Show Gist options
  • Save Konano/4320444783aa0cbf873b01505b0ea39a to your computer and use it in GitHub Desktop.
Save Konano/4320444783aa0cbf873b01505b0ea39a to your computer and use it in GitHub Desktop.
通过 m.weibo.cn 发布微博
import requests
import json
import time
import random
import mimetypes
import os
cookie = json.load(open("cookie.json", "r"))
cookie["MLOGIN"] = 1
def get_xsrf() -> (int, str):
time_watermark = int(time.time() / 3600) * 100001
print(f"Time watermark: {time_watermark}")
cookie["_T_WM"] = time_watermark
headers = {
"authority": "m.weibo.cn",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
"cache-control": "no-cache",
"cookie": "; ".join([f"{k}={v}" for k, v in cookie.items()]),
"pragma": "no-cache",
"sec-ch-ua": '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
"sec-ch-ua-mobile": "?1",
"sec-ch-ua-platform": '"Android"',
"sec-fetch-dest": "empty",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "same-origin",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Linux; Android 13; Pixel 7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Mobile Safari/537.36",
}
resp = requests.get("https://m.weibo.cn/api/config", headers=headers).json()
time.sleep(random.random() * 0.3 + 0.7)
xsfr_token = resp["data"]["st"]
uid = resp["data"]["uid"]
print(f"XSRF token: {xsfr_token}")
return time_watermark, xsfr_token
from enum import Enum
class VisibleType(Enum):
ALL = 0
FOLLOW = 10
FRIEND = 6
PRIVATE = 1
def post(
content: str, visible: VisibleType = VisibleType.PRIVATE, pic_ids: [str] = None
):
t_wm, xsrf = get_xsrf()
cookie["_T_WM"] = t_wm
cookie["XSRF-TOKEN"] = xsrf
headers = {
"authority": "m.weibo.cn",
"accept": "application/json, text/plain, */*",
"accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
"cache-control": "no-cache",
"content-type": "application/x-www-form-urlencoded",
"cookie": "; ".join([f"{k}={v}" for k, v in cookie.items()]),
"mweibo-pwa": "1",
"origin": "https://m.weibo.cn",
"pragma": "no-cache",
"referer": "https://m.weibo.cn/compose/",
"sec-ch-ua": '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
"sec-ch-ua-mobile": "?1",
"sec-ch-ua-platform": '"Android"',
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"user-agent": "Mozilla/5.0 (Linux; Android 13; Pixel 7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Mobile Safari/537.36",
"x-requested-with": "XMLHttpRequest",
"x-xsrf-token": xsrf,
}
data = {
"content": content,
"visible": visible.value,
"st": xsrf,
"_spr": "screen:412x915",
}
if pic_ids is not None:
data["picId"] = ",".join(pic_ids)
resp = requests.post(
"https://m.weibo.cn/api/statuses/update", headers=headers, data=data
).json()
time.sleep(random.random() * 0.3 + 0.7)
print(resp)
def upload_image(filename: str):
t_wm, xsrf = get_xsrf()
cookie["_T_WM"] = t_wm
cookie["XSRF-TOKEN"] = xsrf
headers = {
"authority": "m.weibo.cn",
"accept": "application/json, text/plain, */*",
"accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
"cache-control": "no-cache",
"cookie": "; ".join([f"{k}={v}" for k, v in cookie.items()]),
"mweibo-pwa": "1",
"origin": "https://m.weibo.cn",
"pragma": "no-cache",
"referer": "https://m.weibo.cn/compose/",
"sec-ch-ua": '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
"sec-ch-ua-mobile": "?1",
"sec-ch-ua-platform": '"Android"',
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"user-agent": "Mozilla/5.0 (Linux; Android 13; Pixel 7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Mobile Safari/537.36",
"x-requested-with": "XMLHttpRequest",
"x-xsrf-token": xsrf,
}
data = {
"type": "json",
"_spr": "screen:412x915",
"st": xsrf,
}
mimetype, _ = mimetypes.guess_type(filename)
extension = filename.rsplit(".", 1)[-1]
new_filename = os.urandom(4).hex() + "." + extension
files = [("pic", (new_filename, open(filename, "rb").read(), mimetype))]
resp = requests.post(
"https://m.weibo.cn/api/statuses/uploadPic",
headers=headers,
data=data,
files=files,
).json()
print(resp)
time.sleep(random.random() * 0.3 + 0.7)
pic_id = resp["pic_id"]
return pic_id
def post_image(content: str, images: [str], visible: VisibleType = VisibleType.PRIVATE):
pic_ids = [upload_image(x) for x in images]
post(content, visible, pic_ids)
post_image("test", ["1.jpg", "2.jpg"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment