5
5
6
6
# Date : 2020-05-19 15:27
7
7
# Desc : 获取最新的 GitHub 相关域名对应 IP
8
- import os
9
8
import re
10
- import json
11
- import argparse
12
9
from typing import Any , Optional
13
-
14
- from datetime import datetime , timezone , timedelta
10
+ from datetime import datetime
15
11
16
12
from pythonping import ping
17
13
from requests_html import HTMLSession
18
14
from retry import retry
19
15
20
- GITHUB_URLS = [
21
- 'alive.github.com' , 'api.github.com' , 'assets-cdn.github.com' ,
22
- 'avatars.githubusercontent.com' , 'avatars0.githubusercontent.com' ,
23
- 'avatars1.githubusercontent.com' , 'avatars2.githubusercontent.com' ,
24
- 'avatars3.githubusercontent.com' , 'avatars4.githubusercontent.com' ,
25
- 'avatars5.githubusercontent.com' , 'camo.githubusercontent.com' ,
26
- 'central.github.com' , 'cloud.githubusercontent.com' , 'codeload.github.com' ,
27
- 'collector.github.com' , 'desktop.githubusercontent.com' ,
28
- 'favicons.githubusercontent.com' , 'gist.github.com' ,
29
- 'github-cloud.s3.amazonaws.com' , 'github-com.s3.amazonaws.com' ,
30
- 'github-production-release-asset-2e65be.s3.amazonaws.com' ,
31
- 'github-production-repository-file-5c1aeb.s3.amazonaws.com' ,
32
- 'github-production-user-asset-6210df.s3.amazonaws.com' , 'github.blog' ,
33
- 'github.com' , 'github.community' , 'github.githubassets.com' ,
34
- 'github.global.ssl.fastly.net' , 'github.io' , 'github.map.fastly.net' ,
35
- 'githubstatus.com' , 'live.github.com' , 'media.githubusercontent.com' ,
36
- 'objects.githubusercontent.com' , 'pipelines.actions.githubusercontent.com' ,
37
- 'raw.githubusercontent.com' , 'user-images.githubusercontent.com' ,
38
- 'vscode.dev' , 'education.github.com' , 'private-user-images.githubusercontent.com'
39
- ]
40
-
41
- HOSTS_TEMPLATE = """# GitHub520 Host Start
42
- {content}
43
-
44
- # Update time: {update_time}
45
- # Update url: https://raw.hellogithub.com/hosts
46
- # Star me: https://github.com/521xueweihan/GitHub520
47
- # GitHub520 Host End\n """
48
-
49
-
50
- def write_file (hosts_content : str , update_time : str ) -> bool :
51
- output_doc_file_path = os .path .join (os .path .dirname (__file__ ), "README.md" )
52
- template_path = os .path .join (os .path .dirname (__file__ ),
53
- "README_template.md" )
54
- write_host_file (hosts_content )
55
- if os .path .exists (output_doc_file_path ):
56
- with open (output_doc_file_path , "r" ) as old_readme_fb :
57
- old_content = old_readme_fb .read ()
58
- if old_content :
59
- old_hosts = old_content .split ("```bash" )[1 ].split ("```" )[0 ].strip ()
60
- old_hosts = old_hosts .split ("# Update time:" )[0 ].strip ()
61
- hosts_content_hosts = hosts_content .split ("# Update time:" )[
62
- 0 ].strip ()
63
- if old_hosts == hosts_content_hosts :
64
- print ("host not change" )
65
- return False
66
-
67
- with open (template_path , "r" ) as temp_fb :
68
- template_str = temp_fb .read ()
69
- hosts_content = template_str .format (hosts_str = hosts_content ,
70
- update_time = update_time )
71
- with open (output_doc_file_path , "w" ) as output_fb :
72
- output_fb .write (hosts_content )
73
- return True
74
-
75
-
76
- def write_host_file (hosts_content : str ) -> None :
77
- output_file_path = os .path .join (os .path .dirname (__file__ ), 'hosts' )
78
- with open (output_file_path , "w" ) as output_fb :
79
- output_fb .write (hosts_content )
80
-
81
-
82
- def write_json_file (hosts_list : list ) -> None :
83
- output_file_path = os .path .join (os .path .dirname (__file__ ), 'hosts.json' )
84
- with open (output_file_path , "w" ) as output_fb :
85
- json .dump (hosts_list , output_fb )
16
+ from common import GITHUB_URLS , write_hosts_content
86
17
87
18
88
19
def get_best_ip (ip_list : list ) -> str :
89
- ping_timeout = 2
20
+ ping_timeout = 1
90
21
best_ip = ''
91
- min_ms = ping_timeout * 500
22
+ min_ms = ping_timeout * 1000
92
23
ip_set = set (ip_list )
93
24
for ip in ip_set :
94
25
ping_result = ping (ip , timeout = ping_timeout )
95
- print (ping_result .rtt_avg_ms )
96
- if ping_result .rtt_avg_ms == ping_timeout * 500 :
26
+ print (f'ping { ip } { ping_result .rtt_avg_ms } ms' )
27
+ if ping_result .rtt_avg_ms == ping_timeout * 1000 :
97
28
# 超时认为 IP 失效
98
29
continue
99
30
else :
@@ -103,18 +34,6 @@ def get_best_ip(ip_list: list) -> str:
103
34
return best_ip
104
35
105
36
106
- @retry (tries = 3 )
107
- def get_json (session : Any ) -> Optional [list ]:
108
- url = 'https://raw.hellogithub.com/hosts.json'
109
- try :
110
- rs = session .get (url )
111
- data = json .loads (rs .text )
112
- return data
113
- except Exception as ex :
114
- print (f"get: { url } , error: { ex } " )
115
- raise Exception
116
-
117
-
118
37
@retry (tries = 3 )
119
38
def get_ip (session : Any , github_url : str ) -> Optional [str ]:
120
39
url = f'https://sites.ipaddress.com/{ github_url } '
@@ -130,52 +49,33 @@ def get_ip(session: Any, github_url: str) -> Optional[str]:
130
49
if best_ip :
131
50
return best_ip
132
51
else :
133
- raise Exception (f "url: { github_url } , ipaddress empty" )
52
+ raise Exception ("url: {github_url}, ipaddress empty" )
134
53
except Exception as ex :
135
- print (f "get: { url } , error: { ex } " )
54
+ print ("get: {url}, error: {ex}" )
136
55
raise Exception
137
56
138
57
139
- def main (verbose = False , model = 'server' ) -> None :
140
- if verbose :
141
- print (' Start script.' )
58
+ def main () -> None :
59
+ current_time = datetime . now (). strftime ( '%Y-%m-%d %H:%M:%S' )
60
+ print (f' { current_time } - Start script.' )
142
61
session = HTMLSession ()
143
62
content = ""
144
- if model == 'server' :
145
- content_list = []
146
- for index , github_url in enumerate (GITHUB_URLS ):
147
- try :
148
- ip = get_ip (session , github_url )
149
-
150
- content += ip .ljust (30 ) + github_url + "\n "
151
- content_list .append ((ip , github_url ,))
152
- except Exception :
153
- continue
154
- if verbose :
155
- print (f'process url: { index + 1 } /{ len (GITHUB_URLS )} ' )
156
- else :
157
- content_list = get_json (session )
158
- for item in content_list :
159
- content += item [0 ].ljust (30 ) + item [1 ] + "\n "
63
+ content_list = []
64
+ for index , github_url in enumerate (GITHUB_URLS ):
65
+ try :
66
+ ip = get_ip (session , github_url )
67
+
68
+ content += ip .ljust (30 ) + github_url + "\n "
69
+ content_list .append ((ip , github_url ,))
70
+ except Exception :
71
+ continue
72
+ print (f'Process url: { index + 1 } /{ len (GITHUB_URLS )} , { github_url } ' )
160
73
161
- if not content :
162
- return
163
- update_time = datetime .utcnow ().astimezone (
164
- timezone (timedelta (hours = 8 ))).replace (microsecond = 0 ).isoformat ()
165
- hosts_content = HOSTS_TEMPLATE .format (content = content ,
166
- update_time = update_time )
167
- has_change = write_file (hosts_content , update_time )
168
- if has_change :
169
- write_json_file (content_list )
170
- if verbose :
171
- print (hosts_content )
172
- print ('End script.' )
74
+ write_hosts_content (content , content_list )
75
+ # print(hosts_content)
76
+ current_time = datetime .now ().strftime ('%Y-%m-%d %H:%M:%S' )
77
+ print (f'{ current_time } - End script.' )
173
78
174
79
175
80
if __name__ == '__main__' :
176
- parser = argparse .ArgumentParser (description = "Run fetch ips script" )
177
- # 2. 定义参数
178
- parser .add_argument ('model' , metavar = 'model' , type = str , nargs = '?' )
179
- # 3. 解析命令行
180
- args = parser .parse_args ()
181
- main (True , args .model )
81
+ main ()
0 commit comments