| 35 | |
| 36 | @app.route('/api/community_projects/') |
| 37 | def community_projects(): |
| 38 | # global community_projects_cache |
| 39 | # global last_cache_time |
| 40 | community_projects_cache_data = cache.get("community_projects") |
| 41 | community_projects_cache = community_projects_cache_data and community_projects_cache_data.get("value") |
| 42 | if not community_projects_cache: |
| 43 | projects = sa.Studio(id=31478892).projects(limit=40) |
| 44 | if isinstance(projects[0], dict): #atm the server this is running on still uses scratchattach 1.7.4 that returns a list of dicts here |
| 45 | community_projects_cache = [ |
| 46 | {"project_id":p["id"], "title":p["title"], "author":p["username"], "thumbnail_url":f"https://uploads.scratch.mit.edu/get_image/project/{p['id']}_480x360.png"} for p in projects |
| 47 | ] |
| 48 | else: |
| 49 | community_projects_cache = [ |
| 50 | {"project_id":p.id, "title":p.title, "author":p.author_name, "thumbnail_url":f"https://uploads.scratch.mit.edu/get_image/project/{p.id}_480x360.png"} for p in projects |
| 51 | ] |
| 52 | community_projects_cache_data = {"value": community_projects_cache, "data_id": secrets.token_urlsafe(32)} |
| 53 | cache.set("community_projects", community_projects_cache_data, {"ttl": 300, "tags": ["website"]}) |
| 54 | response = jsonify(random.choices(community_projects_cache, k=5)) |
| 55 | response.headers.add("Fetched-Data-Id", community_projects_cache_data and community_projects_cache_data.get("data_id")) |
| 56 | return response |