Skip to content
This repository was archived by the owner on Mar 17, 2026. It is now read-only.

Commit 0e31924

Browse files
committed
update
1 parent 237a7ef commit 0e31924

4 files changed

Lines changed: 119 additions & 12 deletions

File tree

README.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,25 @@ freecdn-js能提高网站稳定性,如果其中一个cdn链接不可用则启
7474

7575
在`.py`文件头部可以设置是否使用代理(v2ray代理,默认开启),需要自行设置。
7676

77-
`generate_external_manifest_file.py`用于生成`freecdn-manifest.txt`,`.txt`储存用于加速`manifest-full.txt`的cdn链接。详见[EtherDream/freecdn](https://github.com/EtherDream/freecdn/tree/master/examples/ext-manifest)
77+
在上传hexo博客后使用`refresh_cdn_cache.py`进行刷新。通过访问`purge.jsdelivr.net/resource`来刷新`cdn.jsdelivr.net/resource`缓存。
7878

79-
> P.S. cdn有缓存,如果freecdn失效请检查`freecdn-manifest.txt`或`manifest-full.txt`是否和对应原文件一致
79+
> P.S. 官方进行了限制,需要用邮箱申请权限才能生效,比较麻烦(可以设置`generate_external_manifest_file.py`中的`is_refresh_tag`为 `True`,这样可以刷新博客的release`tag`从而达到即时更新cdn缓存
8080

81-
在上传hexo博客后使用`refresh_cdn_cache.py`进行刷新
81+
`generate_external_manifest_file.py`用于生成`freecdn-manifest.txt`,`.txt`储存用于加速`manifest-full.txt`的cdn链接。需要在文件中填写`user`、`repo`等信息
8282

83-
> P.S. `jsd.cdn.zzko.cn`缓存时间长,而且没有刷新缓存的方法,若出现网页加载失败的情况请不要使用`refresh_cdn_cache.py`,这是因为`jsd.cdn.zzko.cn`的加载速度最快,但是加载的文件不是最新版本,因而hash对不上网页就加载失败。
83+
`generate_external_manifest_file.py`中的`is_refresh_tag`为 `True`时能刷新博客的release`tag`从而达到即时更新cdn缓存的目的。需要填写`user`、`token`等信息。
84+
85+
<font color=#808080 >*注:若在没有更新repo资源的时候重复刷新tag可能会生成多个`draft` release*</font>
86+
87+
> Github账户中添加Token:
88+
>1. Github任意页面中,依次点击:右上角头像 -> Settings -> Developer Settings -> Personal access tokens
89+
>1. 点击Generate new token
90+
>1. Notes中随便输入个名字,Select scopes中,确保repo及其子项目全部选中,然后点击Generate Token
91+
>1. 把产生的token,一个40位的16进制字符串记住。重要:此token只显示这一次,如果没记住只能删除重建
92+
93+
外部`manifest`详见[EtherDream/freecdn](https://github.com/EtherDream/freecdn/tree/master/examples/ext-manifest)
94+
95+
> P.S. cdn有缓存,如果freecdn失效请通过访问cdn的`freecdn-manifest.txt`或`manifest-full.txt`来检验是否和在`repo`中对应原文件一致。
8496

8597
生成的`freecdn-manifest.txt`[示例](https://github.com/xingpingcn/picture-bed-using-freecdn/blob/main/freecdn-manifest.txt)如下
8698

@@ -96,6 +108,7 @@ freecdn-js能提高网站稳定性,如果其中一个cdn链接不可用则启
96108
https://cdn.jsdelivr.us/gh/xingpingcn/xingpingcn.github.io@main/manifest-full.txt
97109
https://cdn.jsdelivr.ren/gh/xingpingcn/xingpingcn.github.io@main/manifest-full.txt
98110
https://cdn.jsdelivr.net/gh/xingpingcn/xingpingcn.github.io@main/manifest-full.txt
111+
https://raw.githubusercontent.com/xingpingcn/xingpingcn.github.io/main/manifest-full.txt
99112
hash=izgWMFIdMtd29Zy7kWt3rWohTm7WQsZ9003qUATHdFo=
100113
```
101114

@@ -111,6 +124,12 @@ freecdn-js能提高网站稳定性,如果其中一个cdn链接不可用则启
111124

112125
内置了几个`类cdn.jsdelivr.net`的cdn。其中jsd.cdn.zzko.cn的GitHub地址是[这里](https://github.com/54ayao/Chinajsdelivr)
113126

127+
`generate_external_manifest_file.py`中的`is_refresh_tag`为 `True`时,会查询当前的branch是否有release `tag`,如果没有则创建一个新的`tag`([github API: create-a-release](https://docs.github.com/zh/rest/releases/releases?apiVersion=2022-11-28#create-a-release)),这个`tag`由当前head_commit的`sha_id`的前10位组成。如果有则删除,然后创建一个新的`tag`,freecdn-manifest.txt中的url替换成以下样式:
128+
129+
```
130+
https://cdn.jsdelivr.us/gh/xingpingcn/xingpingcn.github.io@tag/manifest-full.txt
131+
```
132+
114133
## 和hexo配合使用
115134

116135
我的博客用的hexo,因而可以使用以下命令行生成对应文件。
5.96 KB
Binary file not shown.

generate_external_manifest_file.py

Lines changed: 92 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,91 @@
11
import os
2-
import sys,base64,hashlib
3-
from import_to_db_with_urls_txt import cdn_list
2+
import sys,base64,hashlib,requests
3+
from import_to_db_with_urls_txt import cdn_list,proxies_dict
44

55
'''
6-
从freecdn-manifest.txt中生成manifest-full.txt和用于引入外部manifest的freecdn-manifest.txt
6+
从freecdn-manifest.txt中生成manifest-full.txt和用于引入外部manifest的freecdn-manifest.txt。需要填写user、token等信息。
7+
8+
is_refresh_tag = True 会刷新tag,此tag用于即时更新cdn缓存(间接)。需要填写user、token(personal access token)等信息。
79
'''
10+
11+
user = ''
12+
repo = ''
13+
branch = ''
14+
15+
is_refresh_tag = True
16+
17+
token = ''
18+
19+
headers = {
20+
"Accept" : "application/vnd.github+json",
21+
"Authorization": f"Bearer {token}",
22+
"X-GitHub-Api-Version": "2022-11-28"
23+
}
24+
25+
def try_func(func):
26+
def wrapper():
27+
try:
28+
return func()
29+
except Exception :
30+
print('[error] check your network or uesr and repo')
31+
raise
32+
return wrapper
33+
@try_func
34+
def get_release_id():
35+
36+
r = requests.get(f'https://api.github.com/repos/{user}/{repo}/releases/latest',headers=headers,proxies=proxies_dict)
37+
json = r.json()
38+
if r.status_code == 200:
39+
id = json["id"]
40+
print(f'[info] latest release id: {id}.')
41+
return id
42+
else:
43+
if json["message"] == "Not Found":
44+
print("[warning] status_code: "+str(r.status_code))
45+
print('[info] would get 404 status_code if there were no release. or check your network.')
46+
return None
47+
@try_func
48+
def get_branch_sha():
49+
50+
r = requests.get(f'https://api.github.com/repos/{user}/{repo}/branches/{branch}',headers=headers,proxies=proxies_dict)
51+
json = r.json()
52+
return json['commit']['sha'][0:10]
53+
54+
# 取当前head commit的sha-id的前10位作为tag和release的名称
55+
branch_sha = get_branch_sha()
56+
data_of_new_release= {
57+
"tag_name":f"{branch_sha}",
58+
"target_commitish":f"{branch}",
59+
"name":f"{branch_sha}",
60+
"body":"update blog",
61+
"draft": False
62+
}
63+
def post_new_release():
64+
release_id = get_release_id()
65+
if not release_id == None:
66+
#delete release
67+
r1 = requests.delete(f'https://api.github.com/repos/{user}/{repo}/releases/{release_id}',headers=headers,proxies=proxies_dict)
68+
if r1.status_code == 204 :
69+
print('[success] old release deleted.')
70+
else:
71+
print('[error] '+r1.content)
72+
r2 = requests.delete(f'https://api.github.com/repos/{user}/{repo}/git/refs/tags/{branch_sha}',headers=headers,proxies=proxies_dict)
73+
if r1.status_code == 204 :
74+
print('[success] old tag deleted.')
75+
else:
76+
print('[error] '+r2.content)
77+
#create a new one
78+
r = requests.post(f'https://api.github.com/repos/{user}/{repo}/releases',headers=headers,json=data_of_new_release,proxies=proxies_dict)
79+
if r.status_code == 201:
80+
print('[success] new release created.')
81+
elif r.status_code == 404:
82+
print('[error] Not Found if the discussion category name is invalid.')
83+
print(r.json())
84+
elif r.status_code == 422:
85+
print('[error] Validation failed, or the endpoint has been spammed.')
86+
print(r.json())
87+
else:
88+
print('[error] status_code: '+str(r.status_code)+'. when post a new release.')
889
def CalcFileSha256_with_base64(filname):
990
''' calculate file sha256 '''
1091
with open(filname, "rb") as f:
@@ -19,13 +100,17 @@ def main():
19100
hash256 = CalcFileSha256_with_base64(
20101
os.path.join('./public', 'manifest-full.txt'))
21102
f.write('@include\n\t/manifest-full.txt\n@global\n\topen_timeout=0\n/manifest-full.txt')
103+
if is_refresh_tag:
104+
post_new_release()
22105
for cdn in cdn_list:
23106
if not cdn == 'https://raw.githubusercontent.com/':
24-
f.write(f'\n\t{cdn}xingpingcn/xingpingcn.github.io@main/manifest-full.txt')
107+
if is_refresh_tag:
108+
f.write(f'\n\t{cdn}{user}/{repo}@{branch_sha}/manifest-full.txt')
109+
else:
110+
f.write(f'\n\t{cdn}{user}/{repo}@{branch}/manifest-full.txt')
25111
else:
26-
f.write(f'\n\t{cdn}xingpingcn/xingpingcn.github.io/main/manifest-full.txt')
27-
# f.write(f'''@include\n\t/manifest-full.txt\n@global\n\topen_timeout=0\n/manifest-full.txt\n\thttps://jsd.cdn.zzko.cn/gh/xingpingcn/xingpingcn.github.io@main/manifest-full.txt\n\thttps://cdn.jsdelivr.us/gh/xingpingcn/xingpingcn.github.io@main/manifest-full.txt\n\thttps://cdn.jsdelivr.ren/gh/xingpingcn/xingpingcn.github.io@main/manifest-full.txt\n\thttps://cdn.jsdelivr.net/gh/xingpingcn/xingpingcn.github.io@main/manifest-full.txt\n\thash={hash256}''')
112+
f.write(f'\n\t{cdn}{user}/{repo}/{branch}/manifest-full.txt')
28113
f.write(f'\n\thash={hash256}')
29-
print('manifest_file generaeted.')
114+
print('[success] manifest_file generaeted.')
30115
if __name__ == '__main__':
31116
main()

import_to_db_with_urls_txt.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
os.chdir(sys.path[0]) # os.chdir(sys.path[0])把当前py文件所在路径设置为当前运行路径.
1717

1818
is_use_proxy = True
19-
proxies_dict = {'http': 'socks5://127.0.0.1:10808',
19+
if is_use_proxy:
20+
proxies_dict = {'http': 'socks5://127.0.0.1:10808',
2021
'https': 'socks5://127.0.0.1:10808'}
22+
else:
23+
proxies_dict ={}
2124
dir_for_custom_conf = 'dir_for_custom_conf' # 储存文件的文件夹名称
2225
headers = {
2326
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36',

0 commit comments

Comments
 (0)