Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Jekus/himawaripy into Jek…
Browse files Browse the repository at this point in the history
…us-master
  • Loading branch information
boramalper committed Aug 16, 2016
2 parents 969eaf7 + 4cd90d2 commit ab8ea0a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
5 changes: 4 additions & 1 deletion himawaripy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import appdirs

# Increases the quality and the size. Possible values: 4, 8, 16, 20
level = 4
level = 16

# Define a hourly offset or let the script calculate it depending on your timezone
# If auto_offset is True, then script will calculate your hour offset automatically depending on your location.
Expand All @@ -17,3 +17,6 @@
output_file = os.path.join(appdirs.user_cache_dir(appname="himawaripy",
appauthor=False),
"latest.png")

# Deadline for the whole download process in minutes
dl_deadline = 1
14 changes: 10 additions & 4 deletions himawaripy/himawaripy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@
from os.path import dirname
from time import strptime, strftime, mktime
from urllib.request import urlopen
from socket import timeout as TimeoutException

from PIL import Image
from pytz import timezone
from dateutil.tz import tzlocal

from .config import level, output_file, auto_offset, hour_offset
from .utils import set_background, get_desktop_environment
from config import level, output_file, auto_offset, hour_offset , dl_deadline
from utils import set_background, get_desktop_environment

counter = None
height = 550
width = 550
dl_timeout = dl_deadline * 60 / (level ** 2)


def get_time_offset(latest_date):
Expand Down Expand Up @@ -49,8 +51,9 @@ def download_chunk(args):

x, y, latest = args
url_format = "http://himawari8.nict.go.jp/img/D531106/{}d/{}/{}_{}_{}.png"
url = url_format.format(level, width, strftime("%Y/%m/%d/%H%M%S", latest), x, y)

with urlopen(url_format.format(level, width, strftime("%Y/%m/%d/%H%M%S", latest), x, y)) as tile_w:
with urlopen(url , timeout=dl_timeout) as tile_w:
tiledata = tile_w.read()

with counter.get_lock():
Expand Down Expand Up @@ -85,7 +88,10 @@ def main():
counter = Value("i", 0)
p = Pool(cpu_count() * level)
print("Downloading tiles: 0/{} completed".format(level * level), end="", flush=True)
res = p.map(download_chunk, product(range(level), range(level), (requested_time,)))
try:
res = p.map(download_chunk, product(range(level), range(level), (requested_time,)))
except TimeoutException:
exit("\nTimeout while downloading tiles.")

for (x, y, tiledata) in res:
tile = Image.open(BytesIO(tiledata))
Expand Down

0 comments on commit ab8ea0a

Please sign in to comment.