|
4 | 4 | import os |
5 | 5 | import shutil |
6 | 6 | import tempfile |
7 | | -import urllib |
8 | 7 | import uuid |
| 8 | +from typing import Optional |
9 | 9 |
|
| 10 | +import requests |
10 | 11 | from qgis.core import ( |
11 | 12 | QgsCoordinateReferenceSystem, |
12 | 13 | QgsProject, |
@@ -142,8 +143,15 @@ def make_combobox_text(self, data): |
142 | 143 | """ |
143 | 144 | return "[" + data["country"] + "]" + "[" + data["region"] + "]" + data["name"] |
144 | 145 |
|
145 | | - def download_zip(self, url: str) -> str: |
146 | | - data = urllib.request.urlopen(url).read() |
| 146 | + def download_zip(self, url: str) -> Optional[str]: |
| 147 | + response = requests.get(url) |
| 148 | + if response.status_code != 200: |
| 149 | + self.iface.messageBar().pushCritical( |
| 150 | + self.tr("Error"), |
| 151 | + self.tr("Failed to download GTFS data from the URL: ") + url, |
| 152 | + ) |
| 153 | + return None |
| 154 | + data = response.content |
147 | 155 | download_path = os.path.join(TEMP_DIR, str(uuid.uuid4()) + ".zip") |
148 | 156 | with open(download_path, mode="wb") as f: |
149 | 157 | f.write(data) |
@@ -201,6 +209,8 @@ def execution(self): |
201 | 209 | for feed_info in self.get_target_feed_infos(): |
202 | 210 | if feed_info["path"].startswith("http"): |
203 | 211 | feed_info["path"] = self.download_zip(feed_info["path"]) |
| 212 | + if feed_info["path"] is None: |
| 213 | + continue |
204 | 214 |
|
205 | 215 | output_dir = os.path.join( |
206 | 216 | self.outputDirFileWidget.filePath(), feed_info["dir"] |
|
0 commit comments