Skip to content

Commit

Permalink
Merge pull request #1476 from Ghlerrix/pushUrl
Browse files Browse the repository at this point in the history
[fix] 修复未配置Action Secrets每日定时推送报错
  • Loading branch information
tangly1024 committed Sep 2, 2023
2 parents 2a63057 + 79225e1 commit 4b17208
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 19 deletions.
9 changes: 3 additions & 6 deletions .github/workflows/pushUrl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@ jobs:
with:
python-version: 3.8

- name: install requests
- name: Install requests
run: pip install requests

- 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 }}
- name: Push
run: python pushUrl.py --url ${{ secrets.URL }} --baidu_token ${{ secrets.BAIDU_TOKEN }} --bing_api_key ${{ secrets.BING_API_KEY }}
65 changes: 52 additions & 13 deletions pushUrl.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
import random
import re
import ssl
import time

import requests
import argparse

ssl._create_default_https_context = ssl._create_unverified_context


# 每日推送限额,可根据实际情况修改
QUOTA = 100


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)
print('解析站点地图中,请稍后……')
try:
result = requests.get(site)
big = re.findall('<loc>(.*?)</loc>', result.content.decode('utf-8'), re.S)
print('当前已有url:')
print(list(big))
return list(big)
except:
print('请检查你的url是否有误。')
print('正确的应是完整的域名,包含https://,且不包含‘sitemap.xml’, 如下所示:')
print('正确的示例: https://ghlcode.cn')
print('详情参见: https://ghlcode.cn/fe032806-5362-4d82-b746-a0b26ce8b9d9')



def push_to_bing(site, urls, api_key):
Expand All @@ -25,9 +42,9 @@ def push_to_bing(site, urls, api_key):
response = requests.post(endpoint, json=payload)
result = response.json()
if response.status_code == 200:
print("successfully submitted to Bing.")
print("成功推送到Bing.")
elif "ErrorCode" in result:
print("Error pushing URLs to Bing:", result["Message"])
print("推送到Bing出现错误,错误信息为:", result["Message"])
except Exception as e:
print("An error occurred:", e)

Expand All @@ -42,9 +59,9 @@ def push_to_baidu(site, urls, token):
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.")
print("成功推送到百度.")
elif "error" in result:
print("Error pushing URLs to Baidu:", result["message"])
print("推送到百度出现错误,错误信息为:", result["message"])
else:
print("Unknown response from Baidu:", result)
except Exception as e:
Expand All @@ -53,12 +70,34 @@ def push_to_baidu(site, urls, token):

if __name__ == '__main__':
parser = argparse.ArgumentParser(description='parse sitemap')
parser.add_argument('url', type=str, help='The url of your website')
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)

# 获取当前的时间戳作为随机种子
current_timestamp = int(time.time())
random.seed(current_timestamp)

if args.url:
# 解析urls
urls = parse_stiemap(args.url)
if urls is not None:
# 判断当前urls数量是否超过额度,若超过则取当日最大值,默认为100,可根据实际情况修改
if len(urls) > QUOTA:
urls = random.sample(urls, QUOTA)
# 推送bing
if args.bing_api_key:
push_to_bing(args.url, urls, args.bing_api_key)
else:
print('未配置 Bing API Key')
print('详情参见: https://ghlcode.cn/fe032806-5362-4d82-b746-a0b26ce8b9d9')
# 推送百度
if args.baidu_token:
push_to_baidu(args.url, urls, args.baidu_token)
else:
print('未配置 Baidu Token')
print('详情参见: https://ghlcode.cn/fe032806-5362-4d82-b746-a0b26ce8b9d9')
else:
print('请前往 Github Action Secrets 配置 URL')
print('详情参见: https://ghlcode.cn/fe032806-5362-4d82-b746-a0b26ce8b9d9')

0 comments on commit 4b17208

Please sign in to comment.