11"""Other Scratch API-related functions"""
22
3+ import json
4+ import warnings
5+
36from ..utils import commons
7+ from ..utils .exceptions import BadRequest
48from ..utils .requests import Requests as requests
5- import json
9+ from ..utils .supportedlangs import SUPPORTED_CODES , SUPPORTED_NAMES
10+
611
712# --- Front page ---
813
914def 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+
1117
1218def featured_data ():
1319 return requests .get ("https://api.scratch.mit.edu/proxy/featured" ).json ()
1420
21+
1522def featured_projects ():
1623 return featured_data ()["community_featured_projects" ]
1724
25+
1826def featured_studios ():
1927 return featured_data ()["community_featured_studios" ]
2028
29+
2130def top_loved ():
2231 return featured_data ()["community_most_loved_projects" ]
2332
33+
2434def top_remixed ():
2535 return featured_data ()["community_most_remixed_projects" ]
2636
37+
2738def newest_projects ():
2839 return featured_data ()["community_newest_projects" ]
2940
41+
3042def curated_projects ():
3143 return featured_data ()["curator_top_projects" ]
3244
45+
3346def design_studio_projects ():
3447 return featured_data ()["scratch_design_studio" ]
3548
49+
3650# --- Statistics ---
3751
3852def total_site_stats ():
3953 data = requests .get ("https://scratch.mit.edu/statistics/data/daily/" ).json ()
4054 data .pop ("_TS" )
4155 return data
4256
57+
4358def monthly_site_traffic ():
4459 data = requests .get ("https://scratch.mit.edu/statistics/data/monthly-ga/" ).json ()
4560 data .pop ("_TS" )
4661 return data
4762
63+
4864def country_counts ():
4965 return requests .get ("https://scratch.mit.edu/statistics/data/monthly/" ).json ()["country_distribution" ]
5066
67+
5168def age_distribution ():
5269 data = requests .get ("https://scratch.mit.edu/statistics/data/monthly/" ).json ()["age_distribution_data" ][0 ]["values" ]
5370 return_data = {}
5471 for value in data :
5572 return_data [value ["x" ]] = value ["y" ]
5673 return return_data
5774
75+
5876def monthly_comment_activity ():
5977 return requests .get ("https://scratch.mit.edu/statistics/data/monthly/" ).json ()["comment_data" ]
6078
79+
6180def monthly_project_shares ():
6281 return requests .get ("https://scratch.mit.edu/statistics/data/monthly/" ).json ()["project_data" ]
6382
83+
6484def monthly_active_users ():
6585 return requests .get ("https://scratch.mit.edu/statistics/data/monthly/" ).json ()["active_user_data" ]
6686
87+
6788def monthly_activity_trends ():
6889 return requests .get ("https://scratch.mit.edu/statistics/data/monthly/" ).json ()["activity_data" ]
6990
91+
7092# --- CSRF Token Generation API ---
7193
7294def get_csrf_token ():
@@ -80,32 +102,41 @@ def get_csrf_token():
80102 "https://scratch.mit.edu/csrf_token/"
81103 ).headers ["set-cookie" ].split (";" )[3 ][len (" Path=/, scratchcsrftoken=" ):]
82104
105+
83106# --- Various other api.scratch.mit.edu API endpoints ---
84107
85108def get_health ():
86109 return requests .get ("https://api.scratch.mit.edu/health" ).json ()
87110
111+
88112def get_total_project_count () -> int :
89113 return requests .get ("https://api.scratch.mit.edu/projects/count/all" ).json ()["count" ]
90114
115+
91116def check_username (username ):
92117 return requests .get (f"https://api.scratch.mit.edu/accounts/checkusername/{ username } " ).json ()["msg" ]
93118
119+
94120def 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+
96124
97125# --- April fools endpoints ---
98126
99127def aprilfools_get_counter () -> int :
100128 return requests .get ("https://api.scratch.mit.edu/surprise" ).json ()["surprise" ]
101129
130+
102131def aprilfools_increment_counter () -> int :
103132 return requests .post ("https://api.scratch.mit.edu/surprise" ).json ()["surprise" ]
104133
134+
105135# --- Resources ---
106136def get_resource_urls ():
107137 return requests .get ("https://resources.scratch.mit.edu/localized-urls.json" ).json ()
108138
139+
109140# --- Misc ---
110141# I'm not sure what to label this as
111142def scratch_team_members () -> dict :
@@ -115,3 +146,20 @@ def scratch_team_members() -> dict:
115146 text = text .split ("\" }]')" )[0 ] + "\" }]"
116147
117148 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