Skip to content

Instantly share code, notes, and snippets.

@radixm46
Last active February 24, 2021 16:18
Show Gist options
  • Save radixm46/e00aeabaf3c4a46b80fe00e9a78a1032 to your computer and use it in GitHub Desktop.
Save radixm46/e00aeabaf3c4a46b80fe00e9a78a1032 to your computer and use it in GitHub Desktop.
acquire weather forecst overview as json from jma.go.jp and print on shell
#!/usr/bin/env python3
import json
import textwrap
from urllib import request, error
LOCATION_CODE = 130000
JMA_URI = (
f"https://www.jma.go.jp/bosai/forecast/data/overview_forecast/{LOCATION_CODE}.json"
)
def print_forecast(forecast_d: json, wrap_length: int = 45) -> None:
# format and print
wrapper = textwrap.TextWrapper(
width=wrap_length,
initial_indent="",
)
print(
f'{"-".join(["" for i in range(wrap_length*2)])}\n',
f'place: {forecast_d.get("targetArea")}\n',
f'{"-".join(["" for i in range(wrap_length*2)])}\n',
sep="",
)
for t in wrapper.wrap(forecast_d.get("text")):
print(t)
if __name__ == "__main__":
with request.urlopen(JMA_URI) as response:
stream_data = response.readlines()[0]
stream_json = json.loads(stream_data.decode("utf-8"))
print_forecast(stream_json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment