1 Star 0 Fork 0

Endless95/github-host

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
github.py 2.61 KB
一键复制 编辑 原始数据 按行查看 历史
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@Contact : liuyuqi.gov@msn.cn
@Time : 2019/08/03 16:16:59
@License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
@Desc : refeash github host everyday
'''
import os
import datetime
from github_host import get_ip_utils
from github_host.libs.json_conf import JsonConf
class Github(object):
def __init__(self):
self.jsonConf = JsonConf()
self.conf = self.jsonConf.load()
self.sites = self.conf.get('sites')
self.addr2ip = {}
self.hostLocation = r"hosts"
def dropDuplication(self, line):
flag = False
if "#*******" in line:
return True
for site in self.sites:
if site in line:
flag = flag or True
else:
flag = flag or False
return flag
def saveRouterosFile(self):
''' 应网友需求,导出一份 routeros 格式的hosts文件 '''
today = datetime.date.today()
with open("hosts-routeros.txt", "w") as f:
f.write("#*********************github " +
str(today) + " update********************\n")
f.write(
"#******* get latest hosts: http://blog.yoqi.me/lyq/16489.html\n")
for key in self.addr2ip:
f.write("add address=" + self.addr2ip[key] + " name="+ key + "\n")
# 更新host, 并刷新本地DNS
def updateHost(self):
today = datetime.date.today()
for site in self.sites:
trueip = get_ip_utils.getIpFromipapi(site)
if trueip != None:
self.addr2ip[site] = trueip
print(site + "\t" + trueip)
with open(self.hostLocation, "r") as f1:
f1_lines = f1.readlines()
with open("temphost", "w") as f2:
for line in f1_lines: # 为了防止 host 越写用越长,需要删除之前更新的含有github相关内容
if self.dropDuplication(line) == False:
f2.write(line)
f2.write("#*********************github " +
str(today) + " update********************\n")
f2.write(
"#******* get latest hosts: http://blog.yoqi.me/lyq/16489.html\n")
for key in self.addr2ip:
f2.write(self.addr2ip[key] + "\t" + key + "\n")
os.remove(self.hostLocation)
os.rename("temphost", self.hostLocation)
# os.system("ipconfig /flushdns")
self.saveRouterosFile()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Endless5F/github-host.git
git@gitee.com:Endless5F/github-host.git
Endless5F
github-host
github-host
master

搜索帮助