Skip to content

Commit

Permalink
fix: badge automation
Browse files Browse the repository at this point in the history
  • Loading branch information
Antvirf committed Jun 22, 2024
1 parent 2b266d9 commit 98f7bf8
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions badges/badgecreator.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
"""
Python script to get ratings and downloads from Garmin Marketplace, and create repo badges for both.
"""
import re

import json
import requests


def find_rating(text):
"""Given Garmin marketplace page for the app, extract rating value"""
return re.search(r"class=\"rating\" title=\"(.*)\"", text).group(1)


def find_downloads(text):
"""Given Garmin marketplace page for the app, extract downloads value"""
return re.search(r"glyphicon glyphicon-circle-arrow-down\"><\/span> <span>(\d*)<\/span><\/span>", text).group(1)
GARMIN_APP_IDENTIFIER = "38b1b25e-3cf7-4993-9fd9-7ced64eb3564"


def create_badge(text, value):
"""Given a text and a number, generate an .svg badge"""
url = f"https://img.shields.io/badge/{text}-{value}-brightgreen"
svg_text = requests.get(url).text
response = requests.get(f"https://img.shields.io/badge/{text}-{value}-brightgreen")
response.raise_for_status()
svg_text = response.text

with open(text+".svg", "w") as writer:
with open(text + ".svg", "w") as writer:
writer.write(svg_text)


if __name__ == "__main__":
text = requests.get(
r"https://apps.garmin.com/en-US/apps/38b1b25e-3cf7-4993-9fd9-7ced64eb3564"
).text

try:
downloads = find_downloads(text)
create_badge("downloads", downloads)
except:
print("Downloads not found, badge not created")
response = requests.get(
f"https://apps.garmin.com/api/appsLibraryExternalServices/api/asw/apps/{GARMIN_APP_IDENTIFIER}?"
)
response.raise_for_status()
response_json = json.loads(response.text)

try:
rating = find_rating(text)
create_badge("rating", rating)
except:
print("Downloads not found, badge not created")
downloads = response_json["downloadCount"]
rating = response_json["averageRating"]
except KeyError:
print(
"Something went wrong parsing the JSON response, check its contents below:"
)
print(
json.dumps(
response_json,
indent=4,
sort_keys=True,
)
)

create_badge("downloads", downloads)
create_badge("rating", rating)

0 comments on commit 98f7bf8

Please sign in to comment.