Browse Source

update project struct

jianboy 1 year ago
parent
commit
3ebc9251c4

+ 1 - 1
.github/workflows/main.yml

@@ -37,7 +37,7 @@ jobs:
       - name: Update Deny Host
         run: |
           pwd
-          python tools/main.py
+          python main.py
 
       - name: Push to built branch
         uses: Automattic/action-commit-to-branch@master

+ 6 - 0
bin/github_host

@@ -0,0 +1,6 @@
+#!/usr/bin/env python
+
+import github_host
+
+if __name__ == '__main__':
+    github_host.main()

+ 46 - 0
conf/config.json

@@ -0,0 +1,46 @@
+{
+    "sites": [
+        "github.global.ssl.fastly.net",
+        "assets-cdn.github.com",
+        "documentcloud.github.com",
+        "gist.github.com",
+        "gist.githubusercontent.com",
+        "github.githubassets.com",
+        "help.github.com",
+        "nodeload.github.com",
+        "raw.github.com",
+        "status.github.com",
+        "training.github.com",
+        "avatars.githubusercontent.com",
+        "avatars0.githubusercontent.com",
+        "avatars1.githubusercontent.com",
+        "avatars2.githubusercontent.com",
+        "avatars3.githubusercontent.com",
+        "avatars4.githubusercontent.com",
+        "avatars5.githubusercontent.com",
+        "avatars6.githubusercontent.com",
+        "avatars7.githubusercontent.com",
+        "avatars8.githubusercontent.com",
+        "favicons.githubusercontent.com",
+        "codeload.github.com",
+        "github-cloud.s3.amazonaws.com",
+        "github-com.s3.amazonaws.com",
+        "github-production-release-asset-2e65be.s3.amazonaws.com",
+        "github-production-user-asset-6210df.s3.amazonaws.com",
+        "github-production-repository-file-5c1aeb.s3.amazonaws.com",
+        "githubstatus.com",
+        "github.community",
+        "media.githubusercontent.com",
+        "camo.githubusercontent.com",
+        "raw.githubusercontent.com",
+        "cloud.githubusercontent.com",
+        "user-images.githubusercontent.com",
+        "customer-stories-feed.github.com",
+        "pages.github.com",
+        "api.github.com",
+        "live.github.com",
+        "githubapp.com",
+        "github.dev",
+        "github.com"
+    ]
+}

+ 5 - 0
github_host/__init__.py

@@ -0,0 +1,5 @@
+from github_host.github import Github
+
+def main():
+    github=Github()
+    github.updateHost()

+ 0 - 0
github_host/api.py


+ 11 - 1
tools/get_ip_utils.py → github_host/get_ip_utils.py

@@ -1,3 +1,13 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+'''
+@Contact :   liuyuqi.gov@msn.cn
+@Time    :   2019/08/03 17:02:15
+@License :   Copyright © 2017-2022 liuyuqi. All Rights Reserved.
+@Desc    :   get ip from ip address
+'''
+
+from email import header
 import requests
 from bs4 import BeautifulSoup
 import re,json
@@ -75,4 +85,4 @@ def getIpFromipapi(site):
             trueip=res["query"]
     except Exception as e:
         print("查询" + site + " 时出现错误: " + str(e))
-    return trueip
+    return trueip

+ 56 - 0
github_host/github.py

@@ -0,0 +1,56 @@
+#!/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
+
+    # 更新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)

+ 66 - 0
github_host/libs/json_conf.py

@@ -0,0 +1,66 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+'''
+@Contact :   liuyuqi.gov@msn.cn
+@Time    :   2022/05/24 15:07:14
+@License :   Copyright © 2017-2022 liuyuqi. All Rights Reserved.
+@Desc    :   yaml util
+'''
+import os
+import json
+
+
+class JsonConf:
+    def __init__(self, config_path="conf/config.json"):
+        self.config_path = config_path
+
+    def save(self, data):
+        with open(self.config_path, 'w') as json_file:
+            json_file.write(json.dumps(data, indent=4))
+
+    def load(self):
+        if not os.path.exists(self.config_path):
+            with open(self.config_path, 'w') as json_file:
+                pass
+        with open(self.config_path, encoding="utf-8") as json_file:
+            try:
+                data = json.load(json_file)
+            except Exception as e:
+                if(str(e).index("utf-8-sig") > 0):
+                    with open(self.config_path, encoding="utf-8-sig") as json_file:
+                        data = json.load(json_file)
+                        return data
+                else:
+                    print(e)
+            return data
+
+    def set(self, data_dict):
+        json_obj = self.load()
+        for key in data_dict:
+            json_obj[key] = data_dict[key]
+        self.save(json_obj)
+        print(json.dumps(json_obj, indent=4))
+
+    def get(self, key, default_val=""):
+        '''
+        配置文件获取key对象的值,如果没有设置就返回默认值
+        '''
+        try:
+            result = self.load()[key]
+            return result
+        except Exception as e:
+            print(e)
+            return default_val
+
+    def get(self, jsonData, key, default_val=""):
+        try:
+            return jsonData[key]
+        except Exception as e:
+            return default_val
+
+    @staticmethod
+    def get(jsonData, key, default_val=""):
+        try:
+            return jsonData[key]
+        except Exception as e:
+            return default_val

+ 12 - 0
main.py

@@ -0,0 +1,12 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+'''
+@Contact :   liuyuqi.gov@msn.cn
+@Time    :   2019/08/03 16:15:54
+@License :   Copyright © 2017-2022 liuyuqi. All Rights Reserved.
+@Desc    :   main function
+'''
+import github_host
+
+if __name__ == '__main__':
+    github_host.main()

+ 0 - 102
tools/main.py

@@ -1,102 +0,0 @@
-
-#!/usr/bin/env python
-# -*- encoding: utf-8 -*-
-'''
-@Author  :   liuyuqi
-@Contact :   liuyuqi.gov@msn.cn
-@Time    :   2020/04/26 23:01:40
-@Version :   1.0
-@License :   Copyright © 2017-2020 liuyuqi. All Rights Reserved.
-@Desc    :   github.com
-'''
-
-import shutil
-import os,sys,ctypes
-import datetime
-import get_ip_utils
-import platform
-
-# 需要获取ip的网址
-sites = [
-    "github.global.ssl.fastly.net",
-    "assets-cdn.github.com",
-    "documentcloud.github.com",
-    "gist.github.com",
-    "gist.githubusercontent.com",
-    "github.githubassets.com",# 很卡
-    "help.github.com",
-    "nodeload.github.com",
-    "raw.github.com",
-    "status.github.com",
-    "training.github.com",
-    "avatars.githubusercontent.com",
-    "avatars0.githubusercontent.com",
-    "avatars1.githubusercontent.com",
-    "avatars2.githubusercontent.com",
-    "avatars3.githubusercontent.com",
-    "avatars4.githubusercontent.com",
-    "avatars5.githubusercontent.com",
-    "avatars6.githubusercontent.com",
-    "avatars7.githubusercontent.com",
-    "avatars8.githubusercontent.com",
-    "favicons.githubusercontent.com",
-    "codeload.github.com",
-    "github-cloud.s3.amazonaws.com",
-    "github-com.s3.amazonaws.com",
-    "github-production-release-asset-2e65be.s3.amazonaws.com",
-    "github-production-user-asset-6210df.s3.amazonaws.com",
-    "github-production-repository-file-5c1aeb.s3.amazonaws.com",
-    "githubstatus.com",
-    "github.community",
-    "media.githubusercontent.com",
-    "camo.githubusercontent.com",
-    "raw.githubusercontent.com", 
-    "cloud.githubusercontent.com",
-    "user-images.githubusercontent.com",
-    "customer-stories-feed.github.com",
-    "pages.github.com",
-    "api.github.com",
-    "live.github.com",
-    "githubapp.com",
-    "github.dev",
-    "github.com"
-]
-
-addr2ip = {}
-hostLocation = r"hosts"
-
-def dropDuplication(line):
-    flag = False
-    if "#*******" in line:
-        return True
-    for site in sites:
-        if site in line:
-            flag = flag or True
-        else:
-            flag = flag or False
-    return flag
-
-# 更新host, 并刷新本地DNS
-def updateHost():
-    today = datetime.date.today()
-    for site in sites:
-        trueip=get_ip_utils.getIpFromipapi(site)
-        if trueip != None:
-            addr2ip[site] = trueip
-            print(site + "\t" + trueip)
-    with open(hostLocation, "r") as f1:
-        f1_lines = f1.readlines()
-        with open("temphost", "w") as f2:
-            for line in f1_lines:                       # 为了防止 host 越写用越长,需要删除之前更新的含有github相关内容
-                if 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 addr2ip:
-                f2.write(addr2ip[key] + "\t" + key + "\n")
-    os.remove(hostLocation)
-    os.rename("temphost",hostLocation)
-
-if __name__ == "__main__":
-    updateHost()