代码拉取完成,页面将自动刷新
同步操作将从 安木鸡/jump-jump 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
# -*- 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()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。