111 lines
3.5 KiB
Python
111 lines
3.5 KiB
Python
# 原神公告
|
|
import base64
|
|
|
|
import requests, json
|
|
|
|
import wp
|
|
|
|
|
|
def api_get(page=1):
|
|
api_url = "https://bbs-api.miyoushe.com/post/wapi/getNewsList?gids=" + str(page) + "&last_id=20&page_size=20&type=2"
|
|
|
|
headers = {
|
|
'Host': 'bbs-api.miyoushe.com',
|
|
'Connection': 'keep-alive',
|
|
'Accept': 'application/json, text/plain, */*',
|
|
'Origin': 'https://bbs.mihoyo.com',
|
|
'User-Agent': 'Mozilla/5.0 (Linux; Android 10; Redmi K30 Pro) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.210 Mobile Safari/537.36',
|
|
'Referer': 'https://bbs.mihoyo.com/ys/article/' + str(page),
|
|
'Accept-Encoding': 'gzip, deflate, br',
|
|
'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7'
|
|
}
|
|
|
|
response = requests.get(api_url, headers=headers)
|
|
|
|
return response.json()
|
|
|
|
|
|
def get_post(post_id):
|
|
api_url = "https://bbs-api.miyoushe.com/post/wapi/getPostFull?gids=2&post_id=" + str(post_id) + "&read=1"
|
|
|
|
headers = {
|
|
'Host': 'bbs-api.miyoushe.com',
|
|
'Connection': 'keep-alive',
|
|
'Accept': 'application/json, text/plain, */*',
|
|
'Origin': 'https://bbs.mihoyo.com',
|
|
'User-Agent': 'Mozilla/5.0 (Linux; Android 10; Redmi K30 Pro) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.210 Mobile Safari/537.36',
|
|
'Referer': 'https://bbs.mihoyo.com/ys/article/' + str(post_id),
|
|
'Accept-Encoding': 'gzip, deflate, br',
|
|
'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7'
|
|
}
|
|
|
|
response = requests.get(api_url, headers=headers)
|
|
|
|
return response.json()
|
|
|
|
def get(page=1):
|
|
print("正在获取第" + str(page) + "页公告")
|
|
|
|
data = api_get(page)
|
|
|
|
if data['data']['list']:
|
|
for i in data['data']['list']:
|
|
post = i['post']
|
|
post_id = post['post_id']
|
|
image_url = post['images'][0]
|
|
|
|
# 保存图片到目录, 遵循url命名
|
|
image = requests.get(image_url).content
|
|
path = "images/" + image_url.split("/")[-1]
|
|
|
|
with open(path, 'wb') as f:
|
|
f.write(image)
|
|
|
|
# 上传图片到wp
|
|
media_id = wp.upload_media(path)
|
|
|
|
post_data = get_post(post_id)
|
|
|
|
if post_data['data']['post']['post']['content']:
|
|
content = post_data['data']['post']['post']['content']
|
|
|
|
# 下载所有图片
|
|
for i2 in post_data['data']['post']['post']['images']:
|
|
image_url = i2
|
|
image = requests.get(image_url).content
|
|
path = "images/" + image_url.split("/")[-1]
|
|
|
|
with open(path, 'wb') as f:
|
|
f.write(image)
|
|
|
|
# 上传图片到wp
|
|
media_id = wp.upload_media(path)
|
|
|
|
# 获取 WP 图片地址
|
|
wp_image_url = wp.get_media_url(media_id)
|
|
|
|
# 替换图片url
|
|
content = content.replace(image_url, wp_image_url)
|
|
|
|
# 发布
|
|
print("正在发布" + post['subject'])
|
|
|
|
# 在 Content 里面加入原文链接
|
|
content = content + "<p>原文链接:<a href='https://bbs.mihoyo.com/ys/article/" + str(post_id) + "'>https://bbs.mihoyo.com/ys/article/" + str(post_id) + "</a></p>"
|
|
|
|
wp.post_post(post['subject'], content, media_id)
|
|
|
|
print("成功发布" + post['subject'])
|
|
|
|
else:
|
|
print("没有更多公告了")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
# print(get_post(43547002))
|
|
page = 1
|
|
while True:
|
|
get(page)
|
|
page += 1
|
|
input("按回车键获取下一页公告") |