1
1
"""Other Scratch API-related functions"""
2
2
3
+ import json
4
+ import warnings
5
+
3
6
from ..utils import commons
7
+ from ..utils .exceptions import BadRequest
4
8
from ..utils .requests import Requests as requests
5
- import json
9
+ from ..utils .supportedlangs import SUPPORTED_CODES , SUPPORTED_NAMES
10
+
6
11
7
12
# --- Front page ---
8
13
9
14
def get_news (* , limit = 10 , offset = 0 ):
10
- return commons .api_iterative ("https://api.scratch.mit.edu/news" , limit = limit , offset = offset )
15
+ return commons .api_iterative ("https://api.scratch.mit.edu/news" , limit = limit , offset = offset )
16
+
11
17
12
18
def featured_data ():
13
19
return requests .get ("https://api.scratch.mit.edu/proxy/featured" ).json ()
14
20
21
+
15
22
def featured_projects ():
16
23
return featured_data ()["community_featured_projects" ]
17
24
25
+
18
26
def featured_studios ():
19
27
return featured_data ()["community_featured_studios" ]
20
28
29
+
21
30
def top_loved ():
22
31
return featured_data ()["community_most_loved_projects" ]
23
32
33
+
24
34
def top_remixed ():
25
35
return featured_data ()["community_most_remixed_projects" ]
26
36
37
+
27
38
def newest_projects ():
28
39
return featured_data ()["community_newest_projects" ]
29
40
41
+
30
42
def curated_projects ():
31
43
return featured_data ()["curator_top_projects" ]
32
44
45
+
33
46
def design_studio_projects ():
34
47
return featured_data ()["scratch_design_studio" ]
35
48
49
+
36
50
# --- Statistics ---
37
51
38
52
def total_site_stats ():
39
53
data = requests .get ("https://scratch.mit.edu/statistics/data/daily/" ).json ()
40
54
data .pop ("_TS" )
41
55
return data
42
56
57
+
43
58
def monthly_site_traffic ():
44
59
data = requests .get ("https://scratch.mit.edu/statistics/data/monthly-ga/" ).json ()
45
60
data .pop ("_TS" )
46
61
return data
47
62
63
+
48
64
def country_counts ():
49
65
return requests .get ("https://scratch.mit.edu/statistics/data/monthly/" ).json ()["country_distribution" ]
50
66
67
+
51
68
def age_distribution ():
52
69
data = requests .get ("https://scratch.mit.edu/statistics/data/monthly/" ).json ()["age_distribution_data" ][0 ]["values" ]
53
70
return_data = {}
54
71
for value in data :
55
72
return_data [value ["x" ]] = value ["y" ]
56
73
return return_data
57
74
75
+
58
76
def monthly_comment_activity ():
59
77
return requests .get ("https://scratch.mit.edu/statistics/data/monthly/" ).json ()["comment_data" ]
60
78
79
+
61
80
def monthly_project_shares ():
62
81
return requests .get ("https://scratch.mit.edu/statistics/data/monthly/" ).json ()["project_data" ]
63
82
83
+
64
84
def monthly_active_users ():
65
85
return requests .get ("https://scratch.mit.edu/statistics/data/monthly/" ).json ()["active_user_data" ]
66
86
87
+
67
88
def monthly_activity_trends ():
68
89
return requests .get ("https://scratch.mit.edu/statistics/data/monthly/" ).json ()["activity_data" ]
69
90
91
+
70
92
# --- CSRF Token Generation API ---
71
93
72
94
def get_csrf_token ():
@@ -80,32 +102,41 @@ def get_csrf_token():
80
102
"https://scratch.mit.edu/csrf_token/"
81
103
).headers ["set-cookie" ].split (";" )[3 ][len (" Path=/, scratchcsrftoken=" ):]
82
104
105
+
83
106
# --- Various other api.scratch.mit.edu API endpoints ---
84
107
85
108
def get_health ():
86
109
return requests .get ("https://api.scratch.mit.edu/health" ).json ()
87
110
111
+
88
112
def get_total_project_count () -> int :
89
113
return requests .get ("https://api.scratch.mit.edu/projects/count/all" ).json ()["count" ]
90
114
115
+
91
116
def check_username (username ):
92
117
return requests .get (f"https://api.scratch.mit.edu/accounts/checkusername/{ username } " ).json ()["msg" ]
93
118
119
+
94
120
def check_password (password ):
95
- return requests .post ("https://api.scratch.mit.edu/accounts/checkpassword/" , json = {"password" :password }).json ()["msg" ]
121
+ return requests .post ("https://api.scratch.mit.edu/accounts/checkpassword/" , json = {"password" : password }).json ()[
122
+ "msg" ]
123
+
96
124
97
125
# --- April fools endpoints ---
98
126
99
127
def aprilfools_get_counter () -> int :
100
128
return requests .get ("https://api.scratch.mit.edu/surprise" ).json ()["surprise" ]
101
129
130
+
102
131
def aprilfools_increment_counter () -> int :
103
132
return requests .post ("https://api.scratch.mit.edu/surprise" ).json ()["surprise" ]
104
133
134
+
105
135
# --- Resources ---
106
136
def get_resource_urls ():
107
137
return requests .get ("https://resources.scratch.mit.edu/localized-urls.json" ).json ()
108
138
139
+
109
140
# --- Misc ---
110
141
# I'm not sure what to label this as
111
142
def scratch_team_members () -> dict :
@@ -115,3 +146,20 @@ def scratch_team_members() -> dict:
115
146
text = text .split ("\" }]')" )[0 ] + "\" }]"
116
147
117
148
return json .loads (text )
149
+
150
+
151
+ def translate (language : str , text : str = "hello" ):
152
+ if language not in SUPPORTED_CODES :
153
+ if language .lower () in SUPPORTED_CODES :
154
+ language = language .lower ()
155
+ elif language .title () in SUPPORTED_NAMES :
156
+ language = SUPPORTED_CODES [SUPPORTED_NAMES .index (language .title ())]
157
+ else :
158
+ warnings .warn (f"'{ language } ' is probably not a supported language" )
159
+ response_json = requests .get (
160
+ f"https://translate-service.scratch.mit.edu/translate?language={ language } &text={ text } " ).json ()
161
+
162
+ if "result" in response_json :
163
+ return response_json ["result" ]
164
+ else :
165
+ raise BadRequest (f"Language '{ language } ' does not seem to be valid.\n Response: { response_json } " )
0 commit comments