Initial commit
This commit is contained in:
commit
1d4de5e3d9
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
images/
|
||||||
|
./kv.db
|
||||||
|
.idea/
|
BIN
__pycache__/kv.cpython-310.pyc
Normal file
BIN
__pycache__/kv.cpython-310.pyc
Normal file
Binary file not shown.
BIN
__pycache__/wp.cpython-310.pyc
Normal file
BIN
__pycache__/wp.cpython-310.pyc
Normal file
Binary file not shown.
70
kv.py
Normal file
70
kv.py
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
import sqlite3
|
||||||
|
|
||||||
|
|
||||||
|
def create():
|
||||||
|
conn = sqlite3.connect('kv.db')
|
||||||
|
c = conn.cursor()
|
||||||
|
|
||||||
|
# create with index
|
||||||
|
c.execute('''CREATE TABLE IF NOT EXISTS kv (key text, value text, PRIMARY KEY(key))''')
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
|
def set(key, value):
|
||||||
|
conn = sqlite3.connect('kv.db')
|
||||||
|
c = conn.cursor()
|
||||||
|
|
||||||
|
# check table
|
||||||
|
c.execute('''SELECT name FROM sqlite_master WHERE type='table' AND name='kv' ''')
|
||||||
|
result = c.fetchone()
|
||||||
|
if not result:
|
||||||
|
create()
|
||||||
|
|
||||||
|
# create with index
|
||||||
|
c.execute('''INSERT OR REPLACE INTO kv (key, value) VALUES (?, ?)''', (key, value))
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
|
def get(key):
|
||||||
|
conn = sqlite3.connect('kv.db')
|
||||||
|
c = conn.cursor()
|
||||||
|
|
||||||
|
# check table
|
||||||
|
c.execute('''SELECT name FROM sqlite_master WHERE type='table' AND name='kv' ''')
|
||||||
|
result = c.fetchone()
|
||||||
|
if not result:
|
||||||
|
create()
|
||||||
|
|
||||||
|
# create with index
|
||||||
|
c.execute('''SELECT value FROM kv WHERE key=?''', (key,))
|
||||||
|
|
||||||
|
result = c.fetchone()
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
if result:
|
||||||
|
return result[0]
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def delete(key):
|
||||||
|
conn = sqlite3.connect('kv.db')
|
||||||
|
c = conn.cursor()
|
||||||
|
|
||||||
|
# check table
|
||||||
|
c.execute('''SELECT name FROM sqlite_master WHERE type='table' AND name='kv' ''')
|
||||||
|
result = c.fetchone()
|
||||||
|
if not result:
|
||||||
|
create()
|
||||||
|
|
||||||
|
# create with index
|
||||||
|
c.execute('''DELETE FROM kv WHERE key=?''', (key,))
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
92
wp.py
Normal file
92
wp.py
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
import base64
|
||||||
|
import os
|
||||||
|
|
||||||
|
import requests, json
|
||||||
|
|
||||||
|
wp_url = "http://192.168.81.231/wp-json/wp/v2/"
|
||||||
|
|
||||||
|
user = "api"
|
||||||
|
passwd = "JBu7 Q2oZ rDze okev nhHo jXHM"
|
||||||
|
|
||||||
|
get = "get"
|
||||||
|
post = "post"
|
||||||
|
|
||||||
|
|
||||||
|
def req(method="get", path="", params=None):
|
||||||
|
if params is None:
|
||||||
|
params = {}
|
||||||
|
url = wp_url + path
|
||||||
|
|
||||||
|
if method == "get":
|
||||||
|
response = requests.get(url, params=params, auth=(user, passwd))
|
||||||
|
|
||||||
|
elif method == "post":
|
||||||
|
response = requests.post(url, data=params, auth=(user, passwd))
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise Exception("method error")
|
||||||
|
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
|
||||||
|
def get_posts():
|
||||||
|
params = {
|
||||||
|
"per_page": 100
|
||||||
|
}
|
||||||
|
return req(get, "posts", params)
|
||||||
|
|
||||||
|
|
||||||
|
# 发布
|
||||||
|
def post_post(title, content, featured_media=None):
|
||||||
|
data = {
|
||||||
|
"title": title,
|
||||||
|
"content": content,
|
||||||
|
"status": "publish"
|
||||||
|
}
|
||||||
|
|
||||||
|
if featured_media:
|
||||||
|
data["featured_media"] = featured_media
|
||||||
|
|
||||||
|
return req(post, "posts", data)
|
||||||
|
|
||||||
|
|
||||||
|
def upload_media(filepath):
|
||||||
|
data = open(filepath, 'rb').read()
|
||||||
|
filename = os.path.basename(filepath)
|
||||||
|
|
||||||
|
# get content type
|
||||||
|
import mimetypes
|
||||||
|
mime_type = mimetypes.guess_type(filename)[0]
|
||||||
|
if mime_type is None:
|
||||||
|
mime_type = 'image/jpg'
|
||||||
|
|
||||||
|
res = requests.post(url=wp_url + 'media',
|
||||||
|
data=data,
|
||||||
|
headers={'Content-Type': mime_type,
|
||||||
|
'Content-Disposition': 'attachment; filename=%s' % filename},
|
||||||
|
auth=(user, passwd))
|
||||||
|
# pp = pprint.PrettyPrinter(indent=4) ## print it pretty.
|
||||||
|
# pp.pprint(res.json()) #this is nice when you need it
|
||||||
|
new_dict = res.json()
|
||||||
|
new_id = new_dict.get('id')
|
||||||
|
# link = new_dict.get('guid').get("rendered")
|
||||||
|
return new_id
|
||||||
|
|
||||||
|
|
||||||
|
def get_post_url(post_id):
|
||||||
|
# get rendered url
|
||||||
|
res = req(get, "posts/" + str(post_id))
|
||||||
|
|
||||||
|
print(res)
|
||||||
|
return res['link']
|
||||||
|
|
||||||
|
|
||||||
|
def get_media_url(media_id):
|
||||||
|
# get rendered url
|
||||||
|
res = req(get, "media/" + str(media_id))
|
||||||
|
|
||||||
|
# if exists
|
||||||
|
if res['guid']['rendered']:
|
||||||
|
return res['guid']['rendered']
|
||||||
|
|
||||||
|
return None
|
111
ys_1.py
Normal file
111
ys_1.py
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
# 原神公告
|
||||||
|
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("按回车键获取下一页公告")
|
Loading…
Reference in New Issue
Block a user