|
| 1 | +import requests |
| 2 | +import time |
| 3 | +from datetime import datetime, timedelta, timezone |
| 4 | + |
| 5 | +datas = [] |
| 6 | +for url in ["https://api.github.com/repos/rime/weasel/releases/tags/latest", "https://api.github.com/repos/rime/weasel/releases/latest"]: |
| 7 | + response = requests.get(url) |
| 8 | + if response.ok: |
| 9 | + datas.append(response.json()) |
| 10 | +filenames = [] |
| 11 | +urls = [] |
| 12 | +update_time = [] |
| 13 | +tags_name = [] |
| 14 | +for data in datas: |
| 15 | + if data["prerelease"] == False: |
| 16 | + release_json = data |
| 17 | + for asset in data['assets']: |
| 18 | + if asset['name'].endswith('.exe'): |
| 19 | + filenames.append(asset['name']) |
| 20 | + urls.append(asset['browser_download_url']) |
| 21 | + update_time.append(data['published_at']) |
| 22 | + tags_name.append(data['tag_name']) |
| 23 | +# compare update_time and get the latest one |
| 24 | +latest_time = update_time[0] |
| 25 | +latest_index = 0 |
| 26 | +for i in range(1, len(update_time)): |
| 27 | + if update_time[i] > latest_time: |
| 28 | + latest_time = update_time[i] |
| 29 | + latest_index = i |
| 30 | +# get version number |
| 31 | +version = filenames[latest_index].replace("-installer.exe", "") |
| 32 | +version = version.replace("weasel-", "") |
| 33 | +# download url |
| 34 | +download_url = urls[latest_index] |
| 35 | +# get local time in format like "Thu, 01 Jan 1970 00:00:00 +0000" |
| 36 | +# define a function to format time |
| 37 | +def format_time(time_str): |
| 38 | + utc_offset_sec = -time.timezone if time.localtime().tm_isdst == 0 else -time.altzone |
| 39 | + utc_offset = timedelta(seconds=utc_offset_sec) |
| 40 | + current_tz = timezone(utc_offset) |
| 41 | + utc_time = datetime.strptime(time_str, "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=timezone.utc) |
| 42 | + local_time = utc_time.astimezone(current_tz) |
| 43 | + formatted_time = local_time.strftime("%a, %d %b %Y %H:%M:%S %z") |
| 44 | + return formatted_time |
| 45 | +formatted_time = format_time(update_time[latest_index]) |
| 46 | + |
| 47 | +# get release notes link |
| 48 | +if tags_name[latest_index] == "latest": |
| 49 | + releaseNotesLink = "https://github.com/rime/weasel/releases/tag/latest" |
| 50 | +else: |
| 51 | + releaseNotesLink = "http://rime.github.io/testing/weasel/" |
| 52 | +# format xml file content |
| 53 | +template = f"""<?xml version="1.0" encoding="utf-8"?> |
| 54 | +<rss version="2.0" xmlns:sparkle="http://www.andymatuschak.org/xml-namespaces/sparkle"> |
| 55 | +<channel> |
| 56 | + <title>【小狼毫】輸入法測試頻道</title> |
| 57 | + <link>http://rime.github.io/testing/weasel/appcast.xml</link> |
| 58 | + <description>小狼毫測試版 Appcast 更新頻道</description> |
| 59 | + <language>zh</language> |
| 60 | + <item> |
| 61 | + <title>小狼毫 {version}</title> |
| 62 | + <sparkle:releaseNotesLink>{releaseNotesLink}</sparkle:releaseNotesLink> |
| 63 | + <pubDate>{formatted_time}</pubDate> |
| 64 | + <enclosure url="{download_url}" |
| 65 | + sparkle:version="{version}" |
| 66 | + type="application/octet-stream"/> |
| 67 | + </item> |
| 68 | + </channel> |
| 69 | +</rss> |
| 70 | +""" |
| 71 | +print(f"./source/testing/weasel/appcast.xml:\n{template}") |
| 72 | +# write template to ./source/testing/weasel/appcast.xml |
| 73 | +with open("./source/testing/weasel/appcast.xml", "w", encoding='utf-8') as f: |
| 74 | + f.write(template) |
| 75 | + f.close() |
| 76 | + print("./source/testing/weasel/appcast.xml updated\n") |
| 77 | +# update release appcast.xml automatically, and update other files if needed |
| 78 | +if 'release_json' in locals() or 'release_json' in globals(): |
| 79 | + import re |
| 80 | + release_formatted_time = format_time(release_json['published_at']) |
| 81 | + # get release url |
| 82 | + for asset in release_json['assets']: |
| 83 | + if asset['name'].endswith('.exe'): |
| 84 | + release_url = asset['browser_download_url'] |
| 85 | + break |
| 86 | + if 'release_url' not in locals(): |
| 87 | + print("release_url not found, using default url") |
| 88 | + release_url = f"https://github.com/rime/weasel/releases/download/{release_json["tag_name"]}/weasel-{release_json["tag_name"]}.0-installer.exe" |
| 89 | + # update appcast.xml |
| 90 | + template_release = f"""<?xml version="1.0" encoding="utf-8"?> |
| 91 | + <rss version="2.0" xmlns:sparkle="http://www.andymatuschak.org/xml-namespaces/sparkle"> |
| 92 | + <channel> |
| 93 | + <title>【小狼毫】輸入法更新頻道</title> |
| 94 | + <link>http://rime.github.io/release/weasel/appcast.xml</link> |
| 95 | + <description>小狼毫 Appcast 更新頻道</description> |
| 96 | + <language>zh</language> |
| 97 | + <item> |
| 98 | + <title>小狼毫 {release_json["tag_name"]}</title> |
| 99 | + <sparkle:releaseNotesLink>http://rime.github.io/release/weasel/</sparkle:releaseNotesLink> |
| 100 | + <pubDate>{release_formatted_time}</pubDate> |
| 101 | + <enclosure url="{release_url}" |
| 102 | + sparkle:version="{release_json["tag_name"]}" |
| 103 | + type="application/octet-stream"/> |
| 104 | + </item> |
| 105 | + </channel> |
| 106 | + </rss>""" |
| 107 | + print(f"./source/release/weasel/appcast.xml:\n{template_release}") |
| 108 | + with open("./source/release/weasel/appcast.xml", "w", encoding='utf-8') as f: |
| 109 | + f.write(template_release) |
| 110 | + f.close() |
| 111 | + print("./source/release/weasel/appcast.xml updated\n") |
| 112 | + # get changelog.md |
| 113 | + changelog_url = f"https://raw.githubusercontent.com/rime/weasel/refs/tags/{release_json['tag_name']}/CHANGELOG.md" |
| 114 | + changelog_txt = requests.get(changelog_url).text |
| 115 | + match = re.search(r'(\d+\.\d+\.\d+)', changelog_txt) |
| 116 | + if not match: |
| 117 | + print("No version number found in CHANGELOG.md") |
| 118 | + exit(0) |
| 119 | + changelog_version = match.group(1) |
| 120 | + |
| 121 | + if changelog_version != release_json['tag_name']: |
| 122 | + print("latest version in CHANGELOG.md is not released yet") |
| 123 | + exit(0) |
| 124 | + # version tag in CHANGELOG.md has been released |
| 125 | + print("latest version in CHANGELOG.md has been released") |
| 126 | + index_md = f"""title: 【小狼毫】更新日誌\ncomments: false\ndate: {release_formatted_time}\n---\n\n{changelog_txt}""" |
| 127 | + # update ./source/release/weasel/index.md |
| 128 | + with open("./source/release/weasel/index.md", "w", encoding='utf-8') as f: |
| 129 | + f.write(index_md) |
| 130 | + f.close() |
| 131 | + print("./source/release/weasel/index.md updated") |
| 132 | + # update ./source/testing/weasel/index.md |
| 133 | + with open("./source/testing/weasel/index.md", "w", encoding='utf-8') as f: |
| 134 | + f.write(index_md) |
| 135 | + f.close() |
| 136 | + print("./source/testing/weasel/index.md updated") |
| 137 | + # modify ./source/_data/downloads.yaml, key weasel/version and weasel/url |
| 138 | + from ruamel.yaml import YAML |
| 139 | + yaml = YAML() |
| 140 | + yaml.preserve_quotes = True |
| 141 | + yaml.indent(mapping=2, sequence=2, offset=2) |
| 142 | + # this will be like this until there is some change in the yaml file not in this format/rule |
| 143 | + with open("./source/_data/downloads.yaml", "r", encoding='utf-8') as f: |
| 144 | + downloads_yaml = yaml.load(f) |
| 145 | + f.close() |
| 146 | + with open("./source/_data/downloads.yaml", "w", encoding='utf-8') as f: |
| 147 | + downloads_yaml['weasel']['version'] = release_json['tag_name'] |
| 148 | + downloads_yaml['weasel']['url'] = release_url |
| 149 | + yaml.dump(downloads_yaml, f) |
| 150 | + print("./source/_data/downloads.yaml updated") |
| 151 | + f.close() |
| 152 | + # update source/download/index.md |
| 153 | + pattern = r"\* \[小狼毫 \d+\.\d+\.\d+\]\(https://github\.com/rime/weasel/releases/latest\)〔\[下載\]\(https://github\.com/rime/weasel/releases/download/\d+\.\d+\.\d+/weasel-\d+\.\d+\.\d+\.0-installer\.exe\)〕〔\[更新日誌\]\(/release/weasel/\)〕〔\[歷史版本\]\(https://github\.com/rime/weasel/releases\)〕" |
| 154 | + new_str = f"* [小狼毫 {release_json['tag_name']}](https://github.com/rime/weasel/releases/latest)〔[下載](https://github.com/rime/weasel/releases/download/{release_json['tag_name']}/weasel-{release_json['tag_name']}.0-installer.exe)〕〔[更新日誌](/release/weasel/)〕〔[歷史版本](https://github.com/rime/weasel/releases)〕" |
| 155 | + # replace pattern with new_str in ./source/download/index.md |
| 156 | + with open("./source/download/index.md", "r", encoding='utf-8') as f: |
| 157 | + download_md = f.read() |
| 158 | + f.close() |
| 159 | + download_md = re.sub(pattern, new_str, download_md) |
| 160 | + with open("./source/download/index.md", "w", encoding='utf-8') as f: |
| 161 | + f.write(download_md) |
| 162 | + f.close() |
| 163 | + print("./source/download/index.md updated") |
0 commit comments