2 Star 0 Fork 38

云启星辰/jump-jump

forked from 安木鸡/jump-jump 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
update-links.py 3.41 KB
一键复制 编辑 原始数据 按行查看 历史
huagui 提交于 2022-03-17 15:29 . 批量更新域名脚本 update-link.py
# -*- coding: utf-8 -*-
'''
填写相关配置后直接运行
python update-links.py
即可,如缺少request模块,运行
python -m pip install request
进行安装
'''
from cmath import log
import json
import requests
import sys
import logging
logging.basicConfig(level=logging.INFO, format='[%(levelname)s] %(filename)s %(lineno)d [%(asctime)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
logging.getLogger('')
#---------------------------------------------
# API请求域名、接口,账号,每次查询返回条数
HOST = "https://jj.c2star.com"
LINKAPI = "/v1/short-link/"
USER = "admin"
PASSWORD = "c2star@Yunqi"
PageSize=20
# 目标域名,替换域名
# 如下配置效果为
# https://www.baidu.com/s?wd=2 =》 https://www.google.com/s?wd=2
OLD_DOMAIN="https://www.baidu.com"
NEW_DOMAIN="https://www.google.com"
#---------------------------------------------
def fetch_links(url, page=1, pageSize=10):
r = requests.get(url,auth = requests.auth.HTTPBasicAuth(USER, PASSWORD))
if r.status_code == 200:
response_body=r.text
response_body=json.loads(response_body)
return response_body["data"]
else:
logging.info(" update link bad response")
logging.info("request url 【{}】".format(url))
logging.info("request body 【{}】".format(json.dumps(body)))
logging.info("status_code 【{}】".format(r.status_code))
logging.info("response text 【{}】".format(r.text))
sys.exit(1)
def fetch_all_links():
page = 1
url = HOST + LINKAPI + "?page={}&pageSize={}".format(page,PageSize)
response = fetch_links(url)
total = response["total"]
shortLinks=response["shortLinks"]
while len(shortLinks) < total:
logging.info(total)
logging.info(len(shortLinks))
page = page + 1
url = HOST + LINKAPI + "?page={}&pageSize={}".format(page,PageSize)
logging.info(url)
response = fetch_links(url)
shortLinks.extend(response["shortLinks"])
return shortLinks
#按域名过滤
def filter_links(links, domain):
target = []
for item in links:
if item['url'].find(domain) > -1 :
target.append(item)
return target
def update_link(id, body):
url = HOST + LINKAPI + id
r = requests.patch(url,auth = requests.auth.HTTPBasicAuth(USER, PASSWORD), data=json.dumps(body))
if r.status_code == 200:
response_body=r.text
response_body=json.loads(response_body)
return True
else:
logging.info(" update link bad response")
logging.info("request url 【{}】".format(url))
logging.info("request body 【{}】".format(json.dumps(body)))
logging.info("status_code 【{}】".format(r.status_code))
logging.info("response text 【{}】".format(r.text))
return False
def update_domain():
links=fetch_all_links()
target=filter_links(links,OLD_DOMAIN)
successNum=0
for item in target:
item["url"] = item["url"].replace(OLD_DOMAIN,NEW_DOMAIN)
ok = update_link(item["id"], item)
if not ok:
break
successNum+=1
logging.info("匹配url共:{} 条".format(len(target)))
logging.info("成功替换:{} 条".format(successNum))
if __name__ == "__main__":
# url = HOST + LINKAPI + "?page=1&pageSize=10"
# fetch_links(url)
update_domain()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/cloudtrek/jump-jump.git
git@gitee.com:cloudtrek/jump-jump.git
cloudtrek
jump-jump
jump-jump
master

搜索帮助