Here is the code strongly simplified. You can use any url to get a reasonable number of files.
import sys
from PyQt6.QtCore import Qt, QSize
from PyQt6.QtGui import QAction, QPixmap
from PyQt6.QtWidgets import (
QMainWindow, QLabel
)
from mepMicroObservatory import viewMicroObservatoryExoplanetFilesWindow
class MainWindow(QMainWindow):
def init(self):
super().init()
self.setWindowTitle("app")
self.setMinimumSize(QSize(500, 300))
label = QLabel(self)
pixmap = QPixmap('img/exoplanet.jpg')
label.setPixmap(pixmap)
self.resize(pixmap.width(), pixmap.height())
label.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.setCentralWidget(label)
fileAction = QAction("file",self)
fileAction.triggered.connect(self.fileActionClick)
microObservatoryAction = QAction("Website", self)
microObservatoryAction.triggered.connect(self.microObservatoryActionClick)
viewMicroObservatoryExoplanetAction = QAction("view Observations", self)
viewMicroObservatoryExoplanetAction.triggered.connect(self.viewMicroObservatoryExoplanetActionClick)
menu = self.menuBar()
file_menu = menu.addMenu("file")
file_menu.addAction(fileAction)
file_menu.addSeparator()
mo_menu = menu.addMenu("download")
mo_menu.addAction(viewMicroObservatoryExoplanetAction)
def fileActionClick(self):
print("file action clicked")
def microObservatoryActionClick(self):
print("file action clicked")
def viewMicroObservatoryExoplanetActionClick(self):
self.w = viewMicroObservatoryExoplanetFilesWindow()
self.w.setWindowTitle("view Observations")
self.w.show()
from PyQt6.QtWidgets import QWidget, QFormLayout, QComboBox, QListWidget,
QDateTimeEdit, QPushButton, QMessageBox, QApplication
from mepDA import getUniqueStars, getFileNamesFromImageDirectory, createDirectory
from PyQt6.QtCore import QDate
from PyQt6.QtGui import QCursor
unique_stars = getUniqueStars()
class viewMicroObservatoryExoplanetFilesWindow(QWidget):
def init(self):
super().init()
layout = QFormLayout()
self.starLabel = QLabel("star")
self.starLabel.setStyleSheet("color:blue;")
self.starCombo = QComboBox()
self.starCombo.addItem("*")
self.starCombo.addItems(unique_stars)
self.starCombo.currentTextChanged.connect(self.starComboViewTxtChanged)
self.fitsFilesLabel = QLabel("fits Files")
self.fitsFilesLabel.setStyleSheet("color:blue;")
self.dateLabel = QLabel("date")
self.dateLabel.setStyleSheet("color:blue;")
self.date = QDateTimeEdit(self)
self.date.setDate(QDate.currentDate())
self.date.setDisplayFormat("dd.MM.yyyy")
self.fileList = QListWidget()
self.fileList.itemDoubleClicked.connect(self.fileListItemDoubleClicked)
self.refreshButton = QPushButton("refresh File List")
self.refreshButton.clicked.connect(self.refreshButtonClicked)
self.observationLabel = QLabel("number of Observations")
self.observationLabel.setStyleSheet("color:blue;")
self.observations = QLabel(str(self.fileList.count()))
self.downLoadButton = QPushButton("download Files")
self.downLoadButton.clicked.connect(self.fitsDownloadButtonClicked)
self.downLoadButton.setDisabled(True)
self.statusLabel = QLabel(" ")
layout.addRow(self.starLabel, self.starCombo)
layout.addRow(self.dateLabel, self.date)
layout.addRow(self.fitsFilesLabel, self.fileList)
layout.addRow("", self.refreshButton)
layout.addRow(self.observationLabel, self.observations)
layout.addRow("", self.downLoadButton)
layout.addRow("downloaded files: ", self.statusLabel)
self.setLayout(layout)
def fileListItemDoubleClicked(self):
selectedIndex = self.fileList.currentItem().text()
print(selectedIndex)
def starComboViewTxtChanged(self, selectedStar):
self.downLoadButton.setDisabled(True)
self.refreshButton.setEnabled(True)
def refreshButtonClicked(self, selectedStar):
cursor = QCursor(Qt.CursorShape.WaitCursor)
QApplication.setOverrideCursor(cursor)
date = self.date.text()
day = date[0:2]
month = date[3:5]
year = date[8:10]
selectedStar = self.starCombo.currentText()
star = selectedStar
if selectedStar.startswith("HAT-P"):
star = selectedStar.replace("HAT-P", "HATP")
myDate = str(year) + str(month) + str(day)
print("selected star: ", star, " date: " , myDate)
fList = getFileNamesFromImageDirectory(star)
self.fileList.clear()
i = 0
for f in fList:
if myDate in f:
i += 1
if star == "*":
self.fileList.addItem(f)
elif f.startswith(star):
self.fileList.addItem(f)
self.observations.setText(str(i))
self.downLoadButton.setEnabled(True)
self.refreshButton.setDisabled(True)
cursor = QCursor(Qt.CursorShape.ArrowCursor)
QApplication.changeOverrideCursor(cursor)
def fitsDownloadButtonClicked(self):
print("fits download running")
day = self.date.text()[0:2]
month = self.date.text()[3:5]
year = self.date.text()[8:10]
dsDirectory = "microDirectory" + "/" + self.starCombo.currentText() + year + month + day
createDirectory(dsDirectory)
url = "https://mo-www.cfa.harvard.edu/ImageDirectory"
files = [self.fileList.item(x).text() for x in range(self.fileList.count())]
i = 0
for f in files:
downloadFile(url, f, dsDirectory)
i += 1
message = "file " + str(i) + "/" + str(len(files)) + " downloaded : "
self.statusLabel.setText(message + f)
print("file ", "{:02d}".format(i), "/", len(files), " downloaded : ", f)
#print("{:02d}".format(i))
dlg = QMessageBox(self)
dlg.setWindowTitle("download ")
dlg.setText("download Ended")
button = dlg.exec()
button = QMessageBox.StandardButton(button)
print(i, " files downloded")
def downloadFile(url, file, directory):
import requests
file_path = url + '/' + file
download_path = directory + '/' + file
print(file_path, " ---> ", download_path)
response = requests.get(file_path)
if response.status_code == 200:
with open(download_path, 'wb') as f:
f.write(response.content)
#print('File downloaded successfully: ', file)
else:
print('Failed to download file: ', download_path)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()