Skip to content

Commit 6a7b75f

Browse files
xirazzeee
authored andcommitted
wip: implement content-rating backend helper function
1 parent a0856b1 commit 6a7b75f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

backend/app/utils.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import base64
2+
import ctypes
23
import gzip
34
import hashlib
45
import json
@@ -21,6 +22,8 @@
2122
clean_id_re = re.compile("[^a-zA-Z0-9_-]+")
2223
remove_desktop_re = re.compile(r"\.desktop$")
2324

25+
MAXUINT = ctypes.c_uint(-1).value
26+
2427

2528
class Hasher:
2629
"""
@@ -395,6 +398,33 @@ def appstream2dict(appstream_url=None) -> dict[str, dict]:
395398
return apps
396399

397400

401+
def get_content_rating_details(content_rating: dict, locale: str) -> dict:
402+
system = AppStream.ContentRatingSystem.from_locale(locale)
403+
rating = AppStream.ContentRating()
404+
rating.set_kind(content_rating["kind"])
405+
details = {"attrs": []}
406+
407+
for attr, level in content_rating.items():
408+
if attr == 'kind':
409+
continue
410+
c_level = AppStream.ContentRatingValue.from_string(level)
411+
rating.add_attribute(attr, c_level)
412+
description = AppStream.ContentRating.attribute_get_description(attr, c_level)
413+
details["attrs"] = {
414+
"attr": attr,
415+
"level": level,
416+
"description": description,
417+
}
418+
419+
min_age = AppStream.ContentRating.get_minimum_age(rating)
420+
if min_age == MAXUINT:
421+
details["minimumAge"] = None
422+
else:
423+
details["minimumAge"] = AppStream.ContentRatingSystem.format_age(system, min_age)
424+
425+
return details
426+
427+
398428
def get_clean_app_id(app_id: str):
399429
return re.sub(clean_id_re, "_", app_id)
400430

0 commit comments

Comments
 (0)