-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_json.py
executable file
·140 lines (113 loc) · 5.05 KB
/
update_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import json
import re
import requests
# Fetch all release information from GitHub
def fetch_all_releases(repo_url):
api_url = f"https://api.github.com/repos/{repo_url}/releases"
headers = {"Accept": "application/vnd.github+json"}
response = requests.get(api_url, headers=headers)
releases = response.json()
sorted_releases = sorted(
releases, key=lambda x: x["published_at"], reverse=False)
return sorted_releases
# Fetch the latest release information from GitHub
def fetch_latest_release(repo_url):
api_url = f"https://api.github.com/repos/{repo_url}/releases"
headers = {"Accept": "application/vnd.github+json"}
response = requests.get(api_url, headers=headers)
releases = response.json()
sorted_releases = sorted(
releases, key=lambda x: x["published_at"], reverse=True)
if sorted_releases:
return sorted_releases[0]
raise ValueError("No release found.")
# Update the JSON file with the fetched data
def remove_tags(text):
text = re.sub('<[^<]+?>', '', text) # Remove HTML tags
text = re.sub(r'#{1,6}\s?', '', text) # Remove markdown header tags
return text
def get_ipa_url(assets):
for asset in assets:
return asset['browser_download_url']
return None
def update_json_file(json_file, fetched_data_all, fetched_data_latest):
with open(json_file, "r") as file:
data = json.load(file)
app = data["apps"][0]
# Ensure 'versions' key exists in app
if "versions" not in app:
app["versions"] = []
for release in fetched_data_all:
full_version = fetched_data_latest["tag_name"].lstrip('v')
tag = release["tag_name"]
version = re.search(r"^(\d+\.\d+\.\d+)", full_version).group(1)
versionDate = release["published_at"]
description = release["body"]
description = remove_tags(description)
description = re.sub(r'\*{2}', '', description)
description = re.sub(r'-', '•', description)
description = re.sub(r'`', '"', description)
downloadURL = get_ipa_url(release["assets"])
size = next((asset["size"] for asset in release["assets"] if asset['browser_download_url'] == downloadURL), None)
version_entry = {
"version": version,
"date": versionDate,
"localizedDescription": description,
"downloadURL": downloadURL,
"size": size
}
# Check if the version entry already exists based on version
version_entry_exists = [item for item in app["versions"] if item["version"] == version]
# If the version entry exists, remove it
if version_entry_exists:
app["versions"].remove(version_entry_exists[0])
# Add the new version entry (whether or not it existed before) only if downloadURL is not None
if downloadURL is not None:
# Insert the version entry at the first position
app["versions"].insert(0, version_entry)
# Now handle the latest release data (from the second script)
full_version = fetched_data_latest["tag_name"].lstrip('v')
tag = fetched_data_latest["tag_name"]
version = re.search(r"^(\d+\.\d+\.\d+)", full_version).group(1)
app["version"] = version
app["versionDate"] = fetched_data_latest["published_at"]
description = fetched_data_latest["body"]
description = remove_tags(description)
description = re.sub(r'\*{2}', '', description)
description = re.sub(r'-', '•', description)
description = re.sub(r'`', '"', description)
app["versionDescription"] = description
app["downloadURL"] = get_ipa_url(fetched_data_latest["assets"])
app["size"] = next((asset["size"] for asset in fetched_data_latest["assets"] if asset['browser_download_url'] == app["downloadURL"]), None)
# Ensure 'news' key exists in data
if "news" not in data:
data["news"] = []
# Add news entry if there's a new release
news_identifier = f"release-{full_version}"
news_entry = {
"title": f"{full_version} - Winston for Reddit",
"identifier": news_identifier,
"caption": f"Update of Winston just got released!",
"date": fetched_data_latest["published_at"],
"tintColor": "#000000",
"imageURL": "https://raw.githubusercontent.com/Balackburn/Winston/main/images/news/news_2.png",
"notify": True,
"url": f"https://github.com/lo-cafe/winston/releases/tag/{tag}"
}
# Check if the news entry already exists
news_entry_exists = any(item["identifier"] ==
news_identifier for item in data["news"])
# Add the news entry if it doesn't exist
if not news_entry_exists:
data["news"].append(news_entry)
with open(json_file, "w") as file:
json.dump(data, file, indent=2)
# Main function
def main():
repo_url = "lo-cafe/winston"
json_file = "apps.json"
fetched_data_all = fetch_all_releases(repo_url)
fetched_data_latest = fetch_latest_release(repo_url)
update_json_file(json_file, fetched_data_all, fetched_data_latest)
if __name__ == "__main__":
main()