Skip to content

Commit

Permalink
Use QgsBlockingNetworkRequest for data fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
MiraGeowerkstatt authored Jun 1, 2022
2 parents 79df410 + e219b7f commit 4660cd8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 405 deletions.
54 changes: 36 additions & 18 deletions custom_news_feed/custom_news_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@
"""
import os.path
import json
import requests
from .network import networkaccessmanager

from qgis.PyQt.QtCore import QSettings, QTranslator, QCoreApplication, Qt, QTimer
from qgis.PyQt.QtCore import QSettings, QTranslator, QCoreApplication, Qt, QTimer, QUrl
from qgis.PyQt.QtGui import QIcon, QPixmap, QImage
from qgis.PyQt.QtWidgets import QAction
from qgis.core import Qgis, QgsMessageLog, QgsSettings
from qgis.PyQt.QtNetwork import QNetworkReply, QNetworkRequest
from qgis.core import Qgis, QgsMessageLog, QgsBlockingNetworkRequest, QgsSettings
from qgis.PyQt.QtWidgets import QAction, QApplication, QWidget, \
QVBoxLayout, QHBoxLayout,\
QLabel, QFileDialog
Expand All @@ -51,7 +49,6 @@ def __init__(self, iface):
# Save reference to the QGIS interface
self.iface = iface
self.settings = QgsSettings()
self.nam = networkaccessmanager.NetworkAccessManager()

# Run plugin when project is opened or created
self.iface.projectRead.connect(self.run)
Expand Down Expand Up @@ -245,7 +242,7 @@ def display_news_content(self, news_json_file_path):
self.tr(u'JSON-file konnte nicht geladen werden. ') +
self.tr(u'Mehr Informationen im QGis message log.'),
level = Qgis.Critical)
QgsMessageLog.logMessage(u'Error Initializing Config file ' + str(e),'Custom News Feed Plugin')
QgsMessageLog.logMessage(u'Error Initializing Config file ' + str(e),'Custom News Feed')
try:
self.timer.start(news['NewsRefreshInterval'] * 60000 ) # convert minutes in miliseconds
self.dockwidget.setWindowTitle(news['PanelTitle'])
Expand All @@ -258,7 +255,7 @@ def display_news_content(self, news_json_file_path):
self.iface.messageBar().pushMessage("Fehler im Custom News Feed Plugin",
self.tr(u'Das Feld ' + str(e) + ' ist im angegebenen JSON-file nicht vorhanden.'),
level = Qgis.Critical)
QgsMessageLog.logMessage(u'Error Reading Config file, missing field ' + str(e),'Custom News Feed Plugin')
QgsMessageLog.logMessage(u'Error Reading Config file, missing field ' + str(e),'Custom News Feed')


def configure_pinned_message(self, pinnedMessageJson):
Expand Down Expand Up @@ -323,7 +320,19 @@ def addNews(self, newsArticles):
imageUrl = newsArticle["ImageUrl"]
try:
if imageUrl[0:4].lower() == 'http':
image.loadFromData(requests.get(imageUrl).content)
request = QNetworkRequest(QUrl(imageUrl))
blockingRequest = QgsBlockingNetworkRequest()
result = blockingRequest.get(request)
if result == QgsBlockingNetworkRequest.NoError:
reply = blockingRequest.reply()
if reply.error() == QNetworkReply.NoError:
image.loadFromData(reply.content())
else:
image = None;
QgsMessageLog.logMessage(u'Error reading image ' + reply.errorString(),'Custom News Feed')
else:
image = None;
QgsMessageLog.logMessage(u'Error reading image ' + blockingRequest.errorMessage(),'Custom News Feed')
else :
with open(imageUrl, 'rb') as file:
image.loadFromData(file.read())
Expand All @@ -333,13 +342,13 @@ def addNews(self, newsArticles):
self.tr(u' konnte nicht geladen werden. '),
level = Qgis.Critical)
QgsMessageLog.logMessage(u'Error reading image ' + str(e),'Custom News Feed')

image_label = QLabel()
image_label.setFixedSize(150, 150)
image_label.setPixmap(QPixmap(image).scaled(150, 150, Qt.KeepAspectRatio, Qt.SmoothTransformation))
right_inner_vbox.setContentsMargins(0,15,0,0)
right_inner_vbox.addWidget(image_label)
right_inner_vbox.addStretch(1)
if image is not None:
image_label = QLabel()
image_label.setFixedSize(150, 150)
image_label.setPixmap(QPixmap(image).scaled(150, 150, Qt.KeepAspectRatio, Qt.SmoothTransformation))
right_inner_vbox.setContentsMargins(0,15,0,0)
right_inner_vbox.addWidget(image_label)
right_inner_vbox.addStretch(1)

title = QLabel(newsArticle['Title'])
title.setStyleSheet("font-weight: bold")
Expand Down Expand Up @@ -395,8 +404,17 @@ def get_text_content_from_path(self, path):
QApplication.setOverrideCursor(Qt.WaitCursor)
try:
if path[0:4].lower() == 'http':
(response, content) = self.nam.request(path)
txt = content.decode("utf-8")
request = QNetworkRequest(QUrl(path))
blockingRequest = QgsBlockingNetworkRequest()
result = blockingRequest.get(request)
if result == QgsBlockingNetworkRequest.NoError:
reply = blockingRequest.reply()
if reply.error() == QNetworkReply.NoError:
txt = str(reply.content(), 'utf-8')
else:
QgsMessageLog.logMessage(u'Error reading file ' + reply.errorString(),'Custom News Feed')
else:
QgsMessageLog.logMessage(u'Error reading file ' + blockingRequest.errorMessage(),'Custom News Feed')
else:
if (not os.path.exists(path)) \
and os.path.exists(os.path.join(self.plugin_dir, path)):
Expand Down
Loading

0 comments on commit 4660cd8

Please sign in to comment.