11import 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.' )
889def 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 \t open_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 \t hash={ hash256 } ' )
29- print ('manifest_file generaeted.' )
114+ print ('[success] manifest_file generaeted.' )
30115if __name__ == '__main__' :
31116 main ()
0 commit comments