|
| 1 | +# SPDX-FileCopyrightText: 2023 Alec Delaney |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +"""Download the images from the GraphQL query to the static folder.""" |
| 5 | + |
| 6 | +import json |
| 7 | +import pathlib |
| 8 | + |
| 9 | +import requests |
| 10 | + |
| 11 | +URL = "https://api.github.com/graphql" |
| 12 | + |
| 13 | +with open("/etc/config.json", encoding="utf-8") as jsonfile: |
| 14 | + config = json.load(jsonfile) |
| 15 | + |
| 16 | +with open("assets/graphql_query.txt", mode="r", encoding="utf-8") as queryfile: |
| 17 | + query_param = {"query": queryfile.read()} |
| 18 | + |
| 19 | +resp = requests.post( |
| 20 | + URL, |
| 21 | + json=query_param, |
| 22 | + headers={ |
| 23 | + "Authorization": f'Bearer {config["GH_TOKEN"]}', |
| 24 | + }, |
| 25 | + timeout=5, |
| 26 | +) |
| 27 | + |
| 28 | +json_resp = json.loads(resp.content)["data"]["user"] |
| 29 | +resp_dir = pathlib.Path("assets/contrib/") |
| 30 | +card_dir = pathlib.Path("flask_app/static/img/gh_cards/") |
| 31 | +resp_dir.mkdir(exist_ok=True) |
| 32 | +card_dir.mkdir(exist_ok=True) |
| 33 | + |
| 34 | +with open(str(resp_dir / "recent.json"), mode="w", encoding="utf-8") as contribfile: |
| 35 | + json.dump(json_resp, contribfile) |
| 36 | + |
| 37 | + |
| 38 | +for index, node in enumerate(json_resp["repositories"]["nodes"]): |
| 39 | + for _ in range(5): |
| 40 | + img_resp = requests.get(node["openGraphImageUrl"], timeout=5) |
| 41 | + if img_resp.status_code == 200: |
| 42 | + with open(str(card_dir / f"card{index}.png"), mode="wb") as imgfile: |
| 43 | + for data_chunk in img_resp: |
| 44 | + imgfile.write(data_chunk) |
0 commit comments