Created
March 21, 2026 13:20
-
-
Save kraftwerk28/a01fde6381d1c708dc991a04cd091895 to your computer and use it in GitHub Desktop.
Linux Spotlight
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from argparse import ArgumentParser | |
| from datetime import datetime | |
| from pathlib import Path | |
| from urllib.parse import urlparse | |
| from urllib3.util import SKIP_HEADER | |
| import json | |
| import logging | |
| import requests | |
| logging.basicConfig(level=logging.INFO) | |
| log = logging.getLogger() | |
| argp = ArgumentParser() | |
| argp.add_argument("--width", type=int, default=9999) | |
| argp.add_argument("--height", type=int, default=9999) | |
| args = argp.parse_args() | |
| # BASE_URL = "https://arc.msn.com/v3/Delivery/Placement" | |
| BASE_URL = "https://fd.api.iris.microsoft.com/v3/Delivery/Placement" | |
| USER_AGENT = "WindowsShellClient/0" | |
| data = { | |
| "pid": 209567, | |
| "fmt": "json", | |
| "rafb": 0, | |
| "ua": USER_AGENT, | |
| "cdm": 1, | |
| "disphorzres": args.width, | |
| "dispvertres": args.height, | |
| "lo": 80217, | |
| "pl": "en-US", | |
| "lc": "en-US", | |
| "ctry": "us", | |
| "time": datetime.now().isoformat(), | |
| } | |
| resp = requests.get( | |
| BASE_URL, | |
| params=data, | |
| headers={ | |
| "Accept": "*/*", | |
| "User-Agent": SKIP_HEADER, | |
| }, | |
| ) | |
| resp_data = resp.json() | |
| img_dir = Path("output_" + datetime.now().isoformat()) | |
| img_dir.mkdir() | |
| with (img_dir / "raw.json").open("w") as f: | |
| json.dump(resp_data, f) | |
| img_count = 0 | |
| for index, i in enumerate(resp_data["batchrsp"]["items"]): | |
| item_data = json.loads(i["item"]) | |
| with (img_dir / f"item_{index}.json").open("w") as f: | |
| json.dump(item_data, f) | |
| for k, v in item_data["ad"].items(): | |
| if k.startswith("image_"): | |
| img_url = urlparse(v["u"]) | |
| img_resp = requests.get(img_url.geturl()) | |
| if img_resp.ok: | |
| img_size = int(img_resp.headers.get("Content-Length") or "") | |
| if img_size > 1024 * 4: | |
| img_ext = Path(img_url.path).suffix | |
| img_path: Path = img_dir / (f"item_{index}_" + k + img_ext) | |
| log.info("Downloading %s...", img_path.name) | |
| with img_path.open("wb") as f: | |
| f.write(img_resp.content) | |
| img_count += 1 | |
| log.info("Retrieved %d images into %s", img_count, img_dir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment