# %%
import os
import time
import datetime
import requests
from io import BytesIO
from PIL import Image
def gen_anigif(url_list, output_name) :
url_list.sort()
images = [Image.open(BytesIO(requests.get(url).content)) for url in url_list]
im = images[0]
im.save(output_name, save_all=True, append_images=images[1:], loop=0x03, duration=160)
def put_anigif_name(stn_name) :
return 'get_radar_' + stn_name + '.gif'
def countdown_timer(wait_sec) :
for i in range(wait_sec) :
time.sleep(1)
print(" >>> Countdown : ",
str((wait_sec - i - 1) // 60), 'min',
str((wait_sec - i - 1) % 60), 'sec', end = '\r', flush = True)
def get_kma_radar_pohang() :
html_head = """
"""
html_body = ""
nowtime_utc = datetime.datetime.utcnow()
nowtime_utc -= datetime.timedelta(minutes = nowtime_utc.minute % 10)
nowtime_utc = nowtime_utc.replace(second = 0, microsecond = 0)
nowtime = nowtime_utc + datetime.timedelta(hours = 9)
str_1 = ""
str_1 += "
" + "-" * 35 + "\n"
str_1 += "
Run : " + nowtime.strftime("%Y-%m-%d %a %H:%M:%S") + " KST\n"
str_1 += "
Now : KST\n"
str_1 += "
\n"
str_1 += "
KMA_Forecast_Short-Term\n"
str_1 += "
JTWC\n"
str_1 += "
earth.null\n"
str_1 += "
zoom.earth\n"
str_1 += "
" + "-" * 35 + "\n"
time_step = 10
stn_names = ['MYN', 'PSN', 'KWK', 'GDK', 'SSP', 'GSN']
anigif_seq = {}
for stn_name in stn_names :
anigif_seq[stn_name] = []
str_1 += "
" + nowtime.strftime('%Y%m%d_') + "0000" + " "
for stn_name in stn_names :
str_1 += "" + stn_name + " "
str_1 += '\n'
str_1 += '
\n'
for i in range(15) :
nowtime_before = nowtime - datetime.timedelta(minutes = time_step * i)
url_1 = nowtime_before.strftime('%Y%m%d%H%M')
url_9 = nowtime_before.strftime('%Y%m%d_%H%M')
str_1 += "
" + url_9 + " "
for idx, stn_name in enumerate(stn_names) :
url_0 = 'https://www.weather.go.kr/w/cgi-bin/rdr_new/nph-rdr_stn1_img?tm='
url_2 = '&cmp=HSR&stn=' + stn_name + '&obs=RN&cpi=PPI&size='
url_3 = '1280'
url_4 = '480'
url_A = url_0 + url_1 + url_2 + url_3
url_B = url_0 + url_1 + url_2 + url_4
anigif_seq[stn_name].append(url_B)
str_1 += "" + stn_name + " "
str_1 += '\n'
str_1 += "
" + "-" * 35 + "\n"
for stn in anigif_seq :
try :
gen_anigif(anigif_seq[stn], put_anigif_name(stn))
except :
pass
for i in range(1, 15) :
url_0 = 'https://www.weather.go.kr/w/repositary/image/sat/gk2a/EA/gk2a_ami_le1b_rgb-s-true_ea020lc_'
url_2 = '.png'
nowtime_before = nowtime_utc - datetime.timedelta(minutes = time_step * i)
url_1 = nowtime_before.strftime('%Y%m%d%H%M')
url_9 = nowtime_before.strftime('%Y%m%d_%H%M')
url = url_0 + url_1 + url_2
str_1 = str_1 + "
SAT_East_RGB_" + url_9 + " \n"
html_body += str_1
html_full = html_head + html_body + html_tail
output_name = os.path.join(os.getcwd(), 'get_kma_radar_pohang.htm')
with open(output_name, 'w', encoding = 'utf-8-sig') as f :
f.write(html_full)
#print("Webcast Page generated : " + output_name)
# %%
get_kma_radar_pohang()