|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
""" |
|
3
|
|
|
cron: 32 7 * * * |
|
4
|
|
|
new Env('百度搜索资源平台'); |
|
5
|
|
|
""" |
|
6
|
|
|
|
|
7
|
|
|
from urllib.parse import parse_qs, urlsplit |
|
8
|
|
|
|
|
9
|
|
|
import requests |
|
10
|
|
|
|
|
11
|
|
|
from notify_mtr import send |
|
12
|
|
|
from utils import get_data |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class BaiduUrlSubmit: |
|
16
|
|
|
def __init__(self, check_items): |
|
17
|
|
|
self.check_items = check_items |
|
18
|
|
|
|
|
19
|
|
|
@staticmethod |
|
20
|
|
|
def url_submit(data_url: str, submit_url: str, times: int = 100) -> str: |
|
21
|
|
|
site = parse_qs(urlsplit(submit_url).query).get("site", [])[0] |
|
22
|
|
|
urls_data = requests.get(data_url) |
|
23
|
|
|
remain = 100000 |
|
24
|
|
|
success_count = 0 |
|
25
|
|
|
error_count = 0 |
|
26
|
|
|
for _ in range(times): |
|
27
|
|
|
try: |
|
28
|
|
|
res = requests.post(submit_url, data=urls_data).json() |
|
29
|
|
|
if res.get("success"): |
|
30
|
|
|
remain = res.get("remain") |
|
31
|
|
|
success_count += res.get("success") |
|
32
|
|
|
else: |
|
33
|
|
|
error_count += 1 |
|
34
|
|
|
except Exception as e: |
|
35
|
|
|
print(e) |
|
36
|
|
|
error_count += 1 |
|
37
|
|
|
return ( |
|
38
|
|
|
f"站点地址: {site}\n" |
|
39
|
|
|
f"当天剩余的可推送 url 条数: {remain}\n" |
|
40
|
|
|
f"成功推送的 url 条数: {success_count}\n" |
|
41
|
|
|
f"成功推送的 url 次数: {times - error_count}\n" |
|
42
|
|
|
f"失败推送的 url 次数: {error_count}" |
|
43
|
|
|
) |
|
44
|
|
|
|
|
45
|
|
|
def main(self): |
|
46
|
|
|
msg_all = "" |
|
47
|
|
|
for check_item in self.check_items: |
|
48
|
|
|
data_url = check_item.get("data_url") |
|
49
|
|
|
submit_url = check_item.get("submit_url") |
|
50
|
|
|
times = int(check_item.get("times", 100)) |
|
51
|
|
|
if data_url and submit_url: |
|
52
|
|
|
msg = self.url_submit(data_url, submit_url, times) |
|
53
|
|
|
else: |
|
54
|
|
|
msg = "配置错误" |
|
55
|
|
|
msg_all += msg + "\n\n" |
|
56
|
|
|
return msg_all |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
if __name__ == "__main__": |
|
60
|
|
|
_data = get_data() |
|
61
|
|
|
_check_items = _data.get("BAIDU", []) |
|
62
|
|
|
result = BaiduUrlSubmit(check_items=_check_items).main() |
|
63
|
|
|
send("百度搜索资源平台", result) |
|
64
|
|
|
|