diff options
author | gimpei <gim.kobayashi@gmail.com> | 2017-08-13 18:33:21 +0900 |
---|---|---|
committer | gimpei <gim.kobayashi@gmail.com> | 2017-08-13 18:33:21 +0900 |
commit | f7fd72566766b44099d95face242da1427e5e758 (patch) | |
tree | 9223bf8320a07241b18c55805d276c2b33df2b3e | |
parent | a7dd2368c76e52028fa9d11b98a242d75ead4a75 (diff) | |
download | kawaii-term-f7fd72566766b44099d95face242da1427e5e758.tar.gz |
a
-rw-r--r-- | async/weather.txt | 9 | ||||
-rw-r--r-- | weather.py | 44 |
2 files changed, 53 insertions, 0 deletions
diff --git a/async/weather.txt b/async/weather.txt new file mode 100644 index 0000000..6c85f18 --- /dev/null +++ b/async/weather.txt @@ -0,0 +1,9 @@ + + +1502616245___shower rain +1502616409___shower rain +1502616447___shower rain +1502616501___shower rain +1502616676___shower rain +1502616719___shower rain +1502616773___shower rain diff --git a/weather.py b/weather.py new file mode 100644 index 0000000..52ca691 --- /dev/null +++ b/weather.py @@ -0,0 +1,44 @@ + + +""" +OPENWEATHERというインターネットサービスを利用したものです。 +私個人のAPIキーですが、個人で利用したものを利用してアクセスしてください +d5098ce4e32e6d1ffc703cd671d8f985 +http://api.openweathermap.org/data/2.5/forecast?id=1850147&APPID={APIKEY} +""" +import os +import json +import time +import sys +from multiprocessing import Process + +ok = None +# check the exists of recent prediction +with open('async/weather.txt','r') as f: + line = None + for line in f: + ... + last = line.strip() + if last == '': + ok = False + print('please wait serveral seconds...' ) + else: + #print('last', last ) + lasttime = int( last.split('___').pop(0) ) + if int( time.time() ) - lasttime > 30: + #print('refresh') + ok = False + # 無条件に出力 + print(last.split('___').pop() ) + +def async_update(): + raw = os.popen('curl -s "http://api.openweathermap.org/data/2.5/weather?q=Tokoy,jp&appid=1e240e732347c23472274dc188cd39d6"').read() + obj = json.loads( raw ) + desc = obj['weather'][0]['description'] + save = '___'.join( map(str,[int(time.time()), desc] ) ) + os.system('echo {save} >> async/weather.txt'.format( save=save ) ) + +if ok is not None: + p = Process(target=async_update, args=()) + p.start() + p.join() |