Skip to content

Commit f61c150

Browse files
authored
Merge pull request #1084 from openzim/phet_recipes
Add automatic creation of phet recipes
2 parents cd4d672 + db68130 commit f61c150

File tree

3 files changed

+141
-1
lines changed

3 files changed

+141
-1
lines changed

recipesauto/src/recipesauto/entrypoint.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ def _parse_context_values(value: str) -> dict[str, str]:
5959
help=f"Path to the overrides.yaml file. Defaults to {Context.overrides}",
6060
)
6161

62-
parser.add_argument("kind", choices=["ted", "devdocs", "freecodecamp", "shamela"])
62+
parser.add_argument(
63+
"kind", choices=["ted", "devdocs", "freecodecamp", "shamela", "phet"]
64+
)
6365

6466
args = parser.parse_args(raw_args)
6567

recipesauto/src/recipesauto/phet.py

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
from dataclasses import dataclass
2+
from enum import Enum
3+
import json
4+
from pathlib import Path
5+
import re
6+
import shutil
7+
from typing import Any
8+
import zipfile
9+
10+
import requests
11+
12+
from recipesauto.context import Context
13+
from recipesauto.constants import logger
14+
from recipesauto.utils import check_zim_name
15+
16+
context = Context.get()
17+
18+
19+
def get_recipe_tag() -> str:
20+
return "phet"
21+
22+
23+
def _ignore_locale(locale: str) -> bool:
24+
return locale in [
25+
"ba",
26+
"fu",
27+
] # both do not contain any translated simulation for now
28+
29+
30+
def get_expected_recipes() -> list[dict[str, Any]]:
31+
32+
resp = requests.get(
33+
"https://phet.colorado.edu/services/metadata/1.3/simulations?format=json&summary",
34+
allow_redirects=True,
35+
timeout=context.http_timeout,
36+
)
37+
resp.raise_for_status()
38+
data = resp.json()
39+
locales = sorted(
40+
set(
41+
filter(
42+
lambda locale: locale == "zh_CN" or "_" not in locale,
43+
[
44+
locale
45+
for project in data["projects"]
46+
for sim in project["simulations"]
47+
for locale in sim["localizedSimulations"].keys()
48+
],
49+
)
50+
)
51+
)
52+
del resp
53+
del data
54+
55+
return [
56+
{
57+
"category": "phet",
58+
"config": {
59+
"flags": {
60+
"includeLanguages": locale,
61+
"withoutLanguageVariants": True,
62+
},
63+
"image": {
64+
"name": "ghcr.io/openzim/phet",
65+
"tag": "3.0.2",
66+
},
67+
"monitor": False,
68+
"platform": None,
69+
"resources": {
70+
"cpu": 3,
71+
"disk": 10737418240,
72+
"memory": 7516192768,
73+
},
74+
"task_name": "phet",
75+
"platform": "phet",
76+
"warehouse_path": "/phet",
77+
},
78+
"enabled": True,
79+
# TODO: use proper language, but this is a pain to do because of name_en + name_native + already existing values in ZF DB
80+
"language": {
81+
"code": "mul",
82+
"name_en": "Multiple Languages",
83+
"name_native": "Multiple Languages",
84+
},
85+
"name": check_zim_name(_get_name(locale)),
86+
"periodicity": "quarterly",
87+
"tags": [
88+
"phet",
89+
],
90+
}
91+
for locale in locales
92+
if not _ignore_locale(locale)
93+
] + [
94+
{
95+
"category": "phet",
96+
"config": {
97+
"flags": {
98+
"mulOnly": True,
99+
},
100+
"image": {
101+
"name": "ghcr.io/openzim/phet",
102+
"tag": "3.0.2",
103+
},
104+
"monitor": False,
105+
"platform": None,
106+
"resources": {
107+
"cpu": 3,
108+
"disk": 10737418240,
109+
"memory": 7516192768,
110+
},
111+
"task_name": "phet",
112+
"platform": "phet",
113+
"warehouse_path": "/phet",
114+
},
115+
"enabled": True,
116+
"language": {
117+
"code": "mul",
118+
"name_en": "Multiple Languages",
119+
"name_native": "Multiple Languages",
120+
},
121+
"name": check_zim_name(_get_name("mul")),
122+
"periodicity": "quarterly",
123+
"tags": [
124+
"multi",
125+
"customapp",
126+
"phet",
127+
],
128+
}
129+
]
130+
131+
132+
def _get_name(locale: str) -> str:
133+
if locale == "zh_CN":
134+
return "phet_zh_all"
135+
else:
136+
return f"phet_{locale}_all"

recipesauto/src/recipesauto/processor.py

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ def run(self):
5353
import recipesauto.freecodecamp as setmodule
5454
elif context.kind == "shamela":
5555
import recipesauto.shamela as setmodule
56+
elif context.kind == "phet":
57+
import recipesauto.phet as setmodule
5658
else:
5759
raise Exception(f"Unsupported kind: {context.kind}")
5860

0 commit comments

Comments
 (0)