From 9c77ac8278fc85ddf04b581e118aa272d340feb5 Mon Sep 17 00:00:00 2001 From: alessandro90 Date: Fri, 26 Apr 2019 17:36:24 +0200 Subject: [PATCH] Forecast data are now downloaded asynchronously. This is much faster --- artemis.py | 3 ++- constants.py | 31 +++++++++++++++++++------------ download_window.py | 1 + threads.py | 45 +++++++++++++++++++++++++++++---------------- 4 files changed, 51 insertions(+), 29 deletions(-) diff --git a/artemis.py b/artemis.py index 43701ff..f03ecf9 100644 --- a/artemis.py +++ b/artemis.py @@ -42,7 +42,8 @@ from utilities import (checksum_ok, format_numbers, resource_path,) -# import icon_rc +import icon_rc + qt_creator_file = resource_path("artemis.ui") Ui_MainWindow, _ = uic.loadUiType(qt_creator_file) diff --git a/constants.py b/constants.py index e19038a..e372ac3 100644 --- a/constants.py +++ b/constants.py @@ -1,18 +1,22 @@ from collections import namedtuple from enum import Enum, auto + class Ftype(object): FREQ = "freq" BAND = "band" + class GfdType(Enum): FREQ = auto() LOC = auto() + class ChecksumWhat(Enum): FOLDER = auto() DB = auto() + class Messages(object): DB_UP_TO_DATE = "Already up to date" DB_UP_TO_DATE_MSG = "No newer version to download." @@ -27,6 +31,7 @@ class Messages(object): BAD_DOWNLOAD = "Something went wrong" BAD_DOWNLOAD_MSG = "Something went wrong with the downaload.\nCheck your internet connection and try again." + class Signal(object): NAME = "name" INF_FREQ = "inf_freq" @@ -42,6 +47,7 @@ class Signal(object): ACF = "acf" WIKI_CLICKED = "url_clicked" + class Database(object): LINK_LOC = "https://aresvalley.com/Storage/Artemis/Database/data.zip" LINK_REF = "https://aresvalley.com/Storage/Artemis/Database/data.zip.log" @@ -66,25 +72,26 @@ class Database(object): Signal.SUP_BAND, Signal.CATEGORY_CODE,) + class Constants(object): CLICK_TO_UPDATE_STR = "Click to update" UPDATING_STR = "Updating..." ACF_DOCS = "https://aresvalley.com/documentation/" FORECAST_XRAY = "https://services.swpc.noaa.gov/text/goes-xray-flux-primary.txt" - FORECAST_PROT = "https://services.swpc.noaa.gov/text/goes-particle-flux-primary.txt" - FORECAST_AK_IND = "https://services.swpc.noaa.gov/text/wwv.txt" + FORECAST_PROT_EL = "https://services.swpc.noaa.gov/text/goes-particle-flux-primary.txt" + FORECAST_AK_INDEX = "https://services.swpc.noaa.gov/text/wwv.txt" FORECAST_SGAS = "https://services.swpc.noaa.gov/text/sgas.txt" - FORECAST_G = "https://services.swpc.noaa.gov/text/3-day-forecast.txt" + FORECAST_GEO_STORM = "https://services.swpc.noaa.gov/text/3-day-forecast.txt" FORECAST_INFO = "https://www.swpc.noaa.gov/sites/default/files/images/NOAAscales.pdf" - FORECAST_IMG_0 = "http://www.mmmonvhf.de/eme/eme.png" - FORECAST_IMG_1 = "http://www.mmmonvhf.de/ms/ms.png" - FORECAST_IMG_2 = "http://www.mmmonvhf.de/es/es.png" - FORECAST_IMG_3 = "http://www.mmmonvhf.de/solar/solar.png" - FORECAST_IMG_4 = "http://amunters.home.xs4all.nl/eskipstatusNA.gif" - FORECAST_IMG_5 = "http://amunters.home.xs4all.nl/aurorastatus.gif" - FORECAST_IMG_6 = "http://amunters.home.xs4all.nl/eskipstatus.gif" - FORECAST_IMG_7 = "http://amunters.home.xs4all.nl/eskip50status.gif" - FORECAST_IMG_8 = "http://amunters.home.xs4all.nl/eskip70status.gif" + FORECAST_IMGS = ["http://www.mmmonvhf.de/eme/eme.png", + "http://www.mmmonvhf.de/ms/ms.png", + "http://www.mmmonvhf.de/es/es.png", + "http://www.mmmonvhf.de/solar/solar.png", + "http://amunters.home.xs4all.nl/eskipstatusNA.gif", + "http://amunters.home.xs4all.nl/aurorastatus.gif", + "http://amunters.home.xs4all.nl/eskipstatus.gif", + "http://amunters.home.xs4all.nl/eskip50status.gif", + "http://amunters.home.xs4all.nl/eskip70status.gif"] SEARCH_LABEL_IMG = "search_icon.png" VOLUME_LABEL_IMG = "volume.png" DATA_FOLDER = "Data" diff --git a/download_window.py b/download_window.py index 6957f63..9b8327a 100644 --- a/download_window.py +++ b/download_window.py @@ -7,6 +7,7 @@ from constants import Messages Ui_Download_window, _ = uic.loadUiType(resource_path("download_db_window.ui")) + class DownloadWindow(QWidget, Ui_Download_window): complete = pyqtSignal() diff --git a/threads.py b/threads.py index 92a8577..8dc40c7 100644 --- a/threads.py +++ b/threads.py @@ -1,3 +1,4 @@ +import asyncio from enum import Enum, auto from io import BytesIO import os.path @@ -7,6 +8,8 @@ from zipfile import ZipFile from PyQt5.QtCore import QThread from constants import Constants, Database, ChecksumWhat from utilities import checksum_ok +import aiohttp + class ThreadStatus(Enum): OK = auto() @@ -15,6 +18,7 @@ class ThreadStatus(Enum): BAD_DOWNLOAD_ERR = auto() UNDEFINED = auto() + class DownloadThread(QThread): def __init__(self): super().__init__() @@ -73,24 +77,33 @@ class UpadteSpaceWeatherThread(QThread): self.terminate() self.wait() - def run(self): - get_request_data = lambda link: urllib3.PoolManager().request('GET', link).data + async def __download_resource(self, session, link): + resp = await session.get(link) + return await resp.read() + + async def __download_property(self, session, property_name): + link = getattr(Constants, "FORECAST_" + property_name.upper()) + data = await self.__download_resource(session, link) + setattr(self.__space_weather_data, property_name, str(data, 'utf-8')) + + async def __download_image(self, session, n): + im = await self.__download_resource(session, Constants.FORECAST_IMGS[n]) + self.__space_weather_data.images[n].loadFromData(im) + + async def __download_resources(self, *links): + session = aiohttp.ClientSession() + properties = ("xray", "prot_el", "ak_index", "sgas", "geo_storm") try: - self.__space_weather_data.xray = str(get_request_data(Constants.FORECAST_XRAY), 'utf-8') - self.__space_weather_data.prot_el = str(get_request_data(Constants.FORECAST_PROT), 'utf-8') - self.__space_weather_data.ak_index = str(get_request_data(Constants.FORECAST_AK_IND), 'utf-8') - self.__space_weather_data.sgas = str(get_request_data(Constants.FORECAST_SGAS), 'utf-8') - self.__space_weather_data.geo_storm = str(get_request_data(Constants.FORECAST_G), 'utf-8') - self.__space_weather_data.images[0].loadFromData(get_request_data(Constants.FORECAST_IMG_0)) - self.__space_weather_data.images[1].loadFromData(get_request_data(Constants.FORECAST_IMG_1)) - self.__space_weather_data.images[2].loadFromData(get_request_data(Constants.FORECAST_IMG_2)) - self.__space_weather_data.images[3].loadFromData(get_request_data(Constants.FORECAST_IMG_3)) - self.__space_weather_data.images[4].loadFromData(get_request_data(Constants.FORECAST_IMG_4)) - self.__space_weather_data.images[5].loadFromData(get_request_data(Constants.FORECAST_IMG_5)) - self.__space_weather_data.images[6].loadFromData(get_request_data(Constants.FORECAST_IMG_6)) - self.__space_weather_data.images[7].loadFromData(get_request_data(Constants.FORECAST_IMG_7)) - self.__space_weather_data.images[8].loadFromData(get_request_data(Constants.FORECAST_IMG_8)) + t = [asyncio.create_task(self.__download_property(session, p)) for p in properties] + tot_images = range(len(Constants.FORECAST_IMGS)) + t1 = [asyncio.create_task(self.__download_image(session, im_number)) for im_number in tot_images] + await asyncio.gather(*t, *t1) except Exception: self.__status = ThreadStatus.UNKNOWN_ERR else: self.__status = ThreadStatus.OK + finally: + await session.close() + + def run(self): + asyncio.run(self.__download_resources())