Skip to content

Commit c694010

Browse files
committed
fix: github action bug
1 parent 9dac025 commit c694010

File tree

6 files changed

+192
-142
lines changed

6 files changed

+192
-142
lines changed

.github/workflows/GitHub520.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ jobs:
2424
python-version: ${{ matrix.python-version }}
2525
- name: Install dependencies
2626
run: |
27-
if [ -f requirements.txt ]; then sudo pip install -r requirements.txt; fi
27+
if [ -f actions_requirements.txt ]; then pip install -r actions_requirements.txt; fi
2828
- name: run script
2929
run: |
3030
# fetch new ip content and update readme file
31-
sudo python fetch_ips.py
31+
python update_ips.py
3232
- name: commit
3333
id: commit
3434
run: |

README.md

+14-14
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,32 @@
3737
```bash
3838
# GitHub520 Host Start
3939
140.82.112.26 alive.github.com
40-
140.82.112.6 api.github.com
41-
185.199.108.153 assets-cdn.github.com
40+
140.82.114.6 api.github.com
41+
185.199.109.153 assets-cdn.github.com
4242
185.199.111.133 avatars.githubusercontent.com
4343
185.199.111.133 avatars0.githubusercontent.com
4444
185.199.111.133 avatars1.githubusercontent.com
4545
185.199.111.133 avatars2.githubusercontent.com
4646
185.199.111.133 avatars3.githubusercontent.com
4747
185.199.111.133 avatars4.githubusercontent.com
4848
185.199.111.133 avatars5.githubusercontent.com
49-
185.199.110.133 camo.githubusercontent.com
50-
140.82.112.21 central.github.com
49+
185.199.111.133 camo.githubusercontent.com
50+
140.82.114.22 central.github.com
5151
185.199.111.133 cloud.githubusercontent.com
52-
140.82.113.10 codeload.github.com
52+
140.82.114.9 codeload.github.com
5353
140.82.114.21 collector.github.com
5454
185.199.111.133 desktop.githubusercontent.com
5555
185.199.111.133 favicons.githubusercontent.com
56-
140.82.112.3 gist.github.com
56+
140.82.114.4 gist.github.com
5757
54.231.230.65 github-cloud.s3.amazonaws.com
5858
52.216.141.212 github-com.s3.amazonaws.com
59-
16.182.108.113 github-production-release-asset-2e65be.s3.amazonaws.com
60-
52.216.51.225 github-production-repository-file-5c1aeb.s3.amazonaws.com
61-
52.217.192.249 github-production-user-asset-6210df.s3.amazonaws.com
59+
3.5.28.20 github-production-release-asset-2e65be.s3.amazonaws.com
60+
3.5.2.232 github-production-repository-file-5c1aeb.s3.amazonaws.com
61+
52.217.167.97 github-production-user-asset-6210df.s3.amazonaws.com
6262
192.0.66.2 github.blog
6363
140.82.114.3 github.com
6464
140.82.113.17 github.community
65-
185.199.110.154 github.githubassets.com
65+
185.199.108.154 github.githubassets.com
6666
151.101.193.194 github.global.ssl.fastly.net
6767
185.199.108.153 github.io
6868
185.199.111.133 github.map.fastly.net
@@ -72,19 +72,19 @@
7272
185.199.110.133 objects.githubusercontent.com
7373
13.107.42.16 pipelines.actions.githubusercontent.com
7474
185.199.111.133 raw.githubusercontent.com
75-
185.199.110.133 user-images.githubusercontent.com
76-
140.82.113.21 education.github.com
75+
185.199.111.133 user-images.githubusercontent.com
76+
127.0.0.105 education.github.com
7777
185.199.111.133 private-user-images.githubusercontent.com
7878

7979

80-
# Update time: 2025-01-10T14:08:05+08:00
80+
# Update time: 2025-01-16T21:13:43+08:00
8181
# Update url: https://raw.hellogithub.com/hosts
8282
# Star me: https://github.com/521xueweihan/GitHub520
8383
# GitHub520 Host End
8484

8585
```
8686

87-
该内容会自动定时更新, 数据更新时间:2025-01-10T14:08:05+08:00
87+
该内容会自动定时更新, 数据更新时间:2025-01-16T21:13:43+08:00
8888

8989
#### 2.1.2 修改 hosts 文件
9090

actions_requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
requests-html==0.10.0
2+
retry==0.9.2
3+
lxml_html_clean

common.py

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env python
2+
# -*- coding:utf-8 -*-
3+
#
4+
# Author : XueWeiHan
5+
6+
# Date : 2025-01-16 15:27
7+
# Desc : 公共函数
8+
import os
9+
import json
10+
from typing import Any, Optional
11+
from datetime import datetime, timezone, timedelta
12+
13+
from retry import retry
14+
15+
GITHUB_URLS = [
16+
'alive.github.com', 'api.github.com', 'assets-cdn.github.com',
17+
'avatars.githubusercontent.com', 'avatars0.githubusercontent.com',
18+
'avatars1.githubusercontent.com', 'avatars2.githubusercontent.com',
19+
'avatars3.githubusercontent.com', 'avatars4.githubusercontent.com',
20+
'avatars5.githubusercontent.com', 'camo.githubusercontent.com',
21+
'central.github.com', 'cloud.githubusercontent.com', 'codeload.github.com',
22+
'collector.github.com', 'desktop.githubusercontent.com',
23+
'favicons.githubusercontent.com', 'gist.github.com',
24+
'github-cloud.s3.amazonaws.com', 'github-com.s3.amazonaws.com',
25+
'github-production-release-asset-2e65be.s3.amazonaws.com',
26+
'github-production-repository-file-5c1aeb.s3.amazonaws.com',
27+
'github-production-user-asset-6210df.s3.amazonaws.com', 'github.blog',
28+
'github.com', 'github.community', 'github.githubassets.com',
29+
'github.global.ssl.fastly.net', 'github.io', 'github.map.fastly.net',
30+
'githubstatus.com', 'live.github.com', 'media.githubusercontent.com',
31+
'objects.githubusercontent.com', 'pipelines.actions.githubusercontent.com',
32+
'raw.githubusercontent.com', 'user-images.githubusercontent.com',
33+
'vscode.dev', 'education.github.com', 'private-user-images.githubusercontent.com'
34+
]
35+
36+
HOSTS_TEMPLATE = """# GitHub520 Host Start
37+
{content}
38+
39+
# Update time: {update_time}
40+
# Update url: https://raw.hellogithub.com/hosts
41+
# Star me: https://github.com/521xueweihan/GitHub520
42+
# GitHub520 Host End\n"""
43+
44+
45+
@retry(tries=3)
46+
def get_json(session: Any) -> Optional[list]:
47+
url = 'https://raw.hellogithub.com/hosts.json'
48+
try:
49+
rs = session.get(url)
50+
data = json.loads(rs.text)
51+
return data
52+
except Exception as ex:
53+
print(f"get: {url}, error: {ex}")
54+
raise Exception
55+
56+
57+
def write_file(hosts_content: str, update_time: str) -> bool:
58+
output_doc_file_path = os.path.join(os.path.dirname(__file__), "README.md")
59+
template_path = os.path.join(os.path.dirname(__file__),
60+
"README_template.md")
61+
write_host_file(hosts_content)
62+
if os.path.exists(output_doc_file_path):
63+
with open(output_doc_file_path, "r") as old_readme_fb:
64+
old_content = old_readme_fb.read()
65+
if old_content:
66+
old_hosts = old_content.split("```bash")[1].split("```")[0].strip()
67+
old_hosts = old_hosts.split("# Update time:")[0].strip()
68+
hosts_content_hosts = hosts_content.split("# Update time:")[
69+
0].strip()
70+
if old_hosts == hosts_content_hosts:
71+
print("host not change")
72+
return False
73+
74+
with open(template_path, "r") as temp_fb:
75+
template_str = temp_fb.read()
76+
hosts_content = template_str.format(hosts_str=hosts_content,
77+
update_time=update_time)
78+
with open(output_doc_file_path, "w") as output_fb:
79+
output_fb.write(hosts_content)
80+
return True
81+
82+
83+
def write_host_file(hosts_content: str) -> None:
84+
output_file_path = os.path.join(os.path.dirname(__file__), 'hosts')
85+
with open(output_file_path, "w") as output_fb:
86+
output_fb.write(hosts_content)
87+
88+
89+
def write_json_file(hosts_list: list) -> None:
90+
output_file_path = os.path.join(os.path.dirname(__file__), 'hosts.json')
91+
with open(output_file_path, "w") as output_fb:
92+
json.dump(hosts_list, output_fb)
93+
94+
95+
def write_hosts_content(content: str, content_list: list) -> str:
96+
if not content:
97+
return ""
98+
update_time = datetime.now(timezone.utc).astimezone(
99+
timezone(timedelta(hours=8))).replace(microsecond=0).isoformat()
100+
hosts_content = HOSTS_TEMPLATE.format(content=content,
101+
update_time=update_time)
102+
has_change = write_file(hosts_content, update_time)
103+
if has_change:
104+
write_json_file(content_list)
105+
return hosts_content

fetch_ips.py

+26-126
Original file line numberDiff line numberDiff line change
@@ -5,95 +5,26 @@
55
66
# Date : 2020-05-19 15:27
77
# Desc : 获取最新的 GitHub 相关域名对应 IP
8-
import os
98
import re
10-
import json
11-
import argparse
129
from typing import Any, Optional
13-
14-
from datetime import datetime, timezone, timedelta
10+
from datetime import datetime
1511

1612
from pythonping import ping
1713
from requests_html import HTMLSession
1814
from retry import retry
1915

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
8617

8718

8819
def get_best_ip(ip_list: list) -> str:
89-
ping_timeout = 2
20+
ping_timeout = 1
9021
best_ip = ''
91-
min_ms = ping_timeout * 500
22+
min_ms = ping_timeout * 1000
9223
ip_set = set(ip_list)
9324
for ip in ip_set:
9425
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:
9728
# 超时认为 IP 失效
9829
continue
9930
else:
@@ -103,18 +34,6 @@ def get_best_ip(ip_list: list) -> str:
10334
return best_ip
10435

10536

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-
11837
@retry(tries=3)
11938
def get_ip(session: Any, github_url: str) -> Optional[str]:
12039
url = f'https://sites.ipaddress.com/{github_url}'
@@ -130,52 +49,33 @@ def get_ip(session: Any, github_url: str) -> Optional[str]:
13049
if best_ip:
13150
return best_ip
13251
else:
133-
raise Exception(f"url: {github_url}, ipaddress empty")
52+
raise Exception("url: {github_url}, ipaddress empty")
13453
except Exception as ex:
135-
print(f"get: {url}, error: {ex}")
54+
print("get: {url}, error: {ex}")
13655
raise Exception
13756

13857

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.')
14261
session = HTMLSession()
14362
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}')
16073

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.')
17378

17479

17580
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

Comments
 (0)