Merge pull request #1461 from Ghlerrix/pushUrl

优化百度推送和新增必应推送
This commit is contained in:
tangly1024
2023-08-23 22:29:54 +08:00
committed by GitHub
5 changed files with 73 additions and 37 deletions

View File

@@ -1,16 +1,16 @@
## 利用GitHub Actions每天定时给百度推送链接提高收录率 ##
name: baidupush
name: pushUrl
# 两种触发方式一、push代码二、每天国际标准时间23点北京时间+8即早上7点运行
on:
# push:
push:
schedule:
- cron: '0 23 * * *' # https://help.github.com/en/actions/automating-your-workflow-with-github-actions/events-that-trigger-workflows#scheduled-events-schedule
workflow_dispatch:
inputs:
unconditional-invoking:
description: 'baidupush unconditionally'
description: 'push url unconditionally'
type: boolean
required: true
default: true
@@ -35,5 +35,8 @@ jobs:
- name: install requests
run: pip install requests
- name: baidupush
run: npm run baidupush
- name: baiduPush
run: python pushUrl.py ${{ secrets.URL }} --baidu_token ${{ secrets.BAIDU_TOKEN }}
- name: bingPush
run: python pushUrl.py ${{ secrets.URL }} --bing_api_key ${{ secrets.BING_API_KEY }}

View File

@@ -1,18 +0,0 @@
import re
import ssl
import requests
import argparse
if __name__ == '__main__':
ssl._create_default_https_context = ssl._create_unverified_context
parser = argparse.ArgumentParser(description='parse sitemap')
parser.add_argument('url', help='The url of your website')
args = parser.parse_args()
url = f'https://{args.url}/sitemap.xml'
result = requests.get(url)
big = re.findall('<loc>(.*?)</loc>', result.content.decode('utf-8'), re.S)
for i in big:
# print(i)
op_xml_txt = open('urls.txt', 'a')
op_xml_txt.write('%s\n' % i)

View File

@@ -1,12 +0,0 @@
#!/usr/bin/env sh
# 确保脚本抛出遇到的错误
set -e
# 解析sitemap.xml 记得换成你自己的域名注意检查是否包含www
python baidupush.py 'www.ghlerrix.cn'
# 百度链接推送换成自己的token和域名
curl -H 'Content-Type:text/plain' --data-binary @urls.txt "http://data.zz.baidu.com/urls?site=https://www.ghlerrix.cn&token=oUldnU4HZvSTlh0e"
rm -rf urls.txt # 删除文件

View File

@@ -18,8 +18,7 @@
"start": "next start",
"post-build": "next-sitemap --config next-sitemap.config.js",
"export": "next build && next-sitemap --config next-sitemap.config.js && next export",
"bundle-report": "ANALYZE=true yarn build",
"baidupush": "bash baidupush.sh"
"bundle-report": "ANALYZE=true yarn build"
},
"dependencies": {
"@giscus/react": "^2.2.6",

64
pushUrl.py Normal file
View File

@@ -0,0 +1,64 @@
import re
import ssl
import requests
import argparse
ssl._create_default_https_context = ssl._create_unverified_context
def parse_stiemap(site):
site = f'{site}/sitemap.xml'
result = requests.get(site)
big = re.findall('<loc>(.*?)</loc>', result.content.decode('utf-8'), re.S)
return list(big)
def push_to_bing(site, urls, api_key):
endpoint = f"https://ssl.bing.com/webmaster/api.svc/json/SubmitUrlbatch?apikey={api_key}"
payload = {
"siteUrl": site,
"urlList": urls
}
try:
response = requests.post(endpoint, json=payload)
result = response.json()
if response.status_code == 200:
print("successfully submitted to Bing.")
elif "ErrorCode" in result:
print("Error pushing URLs to Bing:", result["Message"])
except Exception as e:
print("An error occurred:", e)
def push_to_baidu(site, urls, token):
api_url = f"http://data.zz.baidu.com/urls?site={site}&token={token}"
payload = "\n".join(urls)
headers = {"Content-Type": "text/plain"}
try:
response = requests.post(api_url, data=payload, headers=headers)
result = response.json()
if "success" in result and result["success"]:
print("URLs successfully pushed to Baidu.")
elif "error" in result:
print("Error pushing URLs to Baidu:", result["message"])
else:
print("Unknown response from Baidu:", result)
except Exception as e:
print("An error occurred:", e)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='parse sitemap')
parser.add_argument('url', type=str, help='The url of your website')
parser.add_argument('--bing_api_key', type=str, default=None, help='your bing api key')
parser.add_argument('--baidu_token', type=str, default=None, help='Your baidu push token')
args = parser.parse_args()
urls = parse_stiemap(args.url)
if args.bing_api_key:
push_to_bing(args.url, urls, args.bing_api_key)
if args.baidu_token:
push_to_baidu(args.url, urls, args.baidu_token)