<filename>load_download_button.py
import base64
import urllib.request
from pathlib import Path

from qgis.PyQt.QtWidgets import QAction, QToolBar
from qgis.PyQt.QtGui import (QPixmap, QIcon)
from qgis.utils import iface
from qgis.core import (QgsProject, QgsRasterLayer, Qgis)
import processing


def download_from_selection():

    project = QgsProject.instance()
    projet_file_name = project.fileName()
    root = project.layerTreeRoot()
    mygroup = root.findGroup("Raster Etat Major")
    
    for feat in iface.activeLayer().selectedFeatures():
        raster_path = Path(projet_file_name).parent / 'data' / feat['location']
        file_name = raster_path.stem
        if not raster_path.exists():
            file_size = urllib.request.urlopen(f'https://labs.webgeodatavore.com/partage/SCANEM40K/{raster_path.name}').info().get('Content-Length')
            iface.messageBar().pushMessage(f"Downloading {file_name} before adding", level=Qgis.Info, duration=3)
            urllib.request.urlretrieve(f'https://labs.webgeodatavore.com/partage/SCANEM40K/{raster_path.name}', raster_path)
        existing_layers_in_group = [l.name() for l in mygroup.children()]
        if file_name not in existing_layers_in_group:
            raster_layer = QgsRasterLayer(f"{raster_path}", file_name)
            project.addMapLayer(raster_layer, False)
            mygroup.addLayer(raster_layer)
        else:
            iface.messageBar().pushMessage(f"The layer with name {file_name} already present. No need to add", level=Qgis.Info, duration=1)

title = 'Download from selection'

if not iface.mainWindow().findChild(QToolBar,'Download from selection'):
    base64_data = "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAOxAAADsQ<KEY>"

    pm1 = QPixmap()
    pm1.loadFromData(base64.b64decode(base64_data))

    selection_download_action = QAction(
        QIcon(pm1),
        title,
    )
    selection_download_action.triggered.connect(download_from_selection)
    toolbar1 = iface.addToolBar(title)
    toolbar1.setObjectName(title)
    toolbar1.addAction(selection_download_action)
