breadtube-bot/google_api_test.py
2025-10-04 16:51:08 +09:00

27 lines
1 KiB
Python

from pathlib import Path
import urllib.error
import urllib.request
def main():
api_key = Path('data/google_api_key.txt').read_text(encoding='utf-8').strip()
timeout = 3
http_code_ok = 200
request = urllib.request.Request(
'https://www.googleapis.com/youtube/v3/search?part=snippet'
f'&channelId=UC-kM5kL9CgjN9s9pim089gg&maxResults=10&order=date&key={api_key}')
request.add_header('Accept', 'application/json')
try:
with urllib.request.urlopen(request, timeout=timeout) as response:
if response.status != http_code_ok:
raise RuntimeError(
f'Non-OK response (status: {response.status}) : {response.read()}')
print(response.read().decode())
except urllib.error.HTTPError as error:
raise RuntimeError(f'Cannot call API: {error}: {error.read()}') from error
except urllib.error.URLError as error:
raise RuntimeError(f'Cannot call API: {error}') from error
if __name__ == '__main__':
main()