-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
27 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |