Created
June 13, 2025 00:37
-
-
Save autch/89dac2286fc8c0134e60b7d3ebd62e7d to your computer and use it in GitHub Desktop.
mirakc-arib collect-logos の出力からロゴ画像を書き出す
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
#!/usr/bin/env python | |
# | |
# Usage: | |
# curl -s http://localhost:40772/api/channels/BS/BS15_0/stream | mirakc-arib collect-logos | ./collect-logo.py | |
import json | |
import sys | |
from urllib.request import urlopen | |
written_files = set() | |
while True: | |
line = sys.stdin.readline() | |
if not line: | |
break | |
try: | |
data = json.loads(line) | |
except json.JSONDecodeError: | |
print(f"Error decoding JSON: {line.strip()}", file=sys.stderr) | |
continue | |
filename = ( | |
f"logo_n{data['nid']}_id{data['id']}_t{data['type']}_v{data['version']}.png" | |
) | |
try: | |
if filename in written_files: | |
print(f"Skipping already processed file: {filename}", file=sys.stderr) | |
continue | |
if not data["data"].startswith("data:"): | |
print( | |
f"Invalid data URL for {data['nid']}-{data['id']}: {data['data']}", | |
file=sys.stderr, | |
) | |
continue | |
with open(filename, "wb") as f: | |
with urlopen(data["data"]) as lf: | |
f.write(lf.read()) | |
print(f"Logo saved as {filename}", file=sys.stderr) | |
written_files.add(filename) | |
except Exception as e: | |
print(f"Error processing {data['nid']}-{data['id']}: {e}", file=sys.stderr) | |
continue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment