This repository has been archived on 2024-12-31. You can view files and clone it, but cannot push or open issues or pull requests.

113 lines
4.7 KiB
Python

import os
import sys
import pyperclip
import requests
from PyQt5.QtCore import QCoreApplication, QRect, QMetaObject
from PyQt5.QtWidgets import QPushButton, QApplication, QDialog, QFileDialog, QLineEdit, QMessageBox, QInputDialog
sheepstar_post = "https://api.sheepstar.xyz/media/"
def sendError(title, text):
QMessageBox().critical(None, title, text)
def sendSuccess(title, text):
QMessageBox().information(None, title, text)
def deleteFile():
assetID, _ = QInputDialog().getText(None, "Datei von CDN löschen",
"Gib hier die 16-stellige assetID ein, um es löschen zu können.")
headers = {'Authorization': "Bearer " + open('api-key.txt', 'r').read().replace("\n", "")}
post = requests.delete(sheepstar_post + "/delete", headers=headers, data={'assetID': assetID})
try:
if post.status_code == 200:
sendSuccess("Löschen erfolgreich", "Die Datei wurde erfolgreich aus dem CDN gelöscht.")
elif post.status_code == 401:
sendError("Löschen fehlgeschlagen", "Bitte ersetze den API-Key in der api-key.txt")
elif post.status_code == 404:
sendError("Löschen fehlgeschlagen", "Die Datei existiert nicht auf dem server.")
elif post.status_code == 500:
sendError("Löschen fehlgeschlagen", "Ein interner Fehler ist aufgetreten.")
except Exception as e:
sendError("Löschen fehlgeschlagen", "Ein unbekannter Fehler ist aufgetreten: " + str(e))
class Ui_Dialog(object):
def setupUi(self, Dialog):
if not Dialog.objectName():
Dialog.setObjectName(u"Dialog")
Dialog.resize(385, 88)
Dialog.setMaximumWidth(385)
Dialog.setMaximumHeight(88)
Dialog.setMinimumWidth(385)
Dialog.setMinimumHeight(88)
self.lineEdit = QLineEdit(Dialog)
self.lineEdit.setObjectName(u"lineEdit")
self.lineEdit.setEnabled(False)
self.lineEdit.setGeometry(QRect(10, 10, 271, 31))
self.pushButton = QPushButton(Dialog)
self.pushButton.setObjectName(u"pushButton")
self.pushButton.setGeometry(QRect(290, 10, 95, 30))
self.pushButton.clicked.connect(self.select)
self.pushButton_2 = QPushButton(Dialog)
self.pushButton_2.setObjectName(u"pushButton_2")
self.pushButton_2.setGeometry(QRect(10, 50, 200, 35))
self.pushButton_2.clicked.connect(self.upload)
self.pushButton_3 = QPushButton(Dialog)
self.pushButton_3.setObjectName(u"pushButton_3")
self.pushButton_3.setGeometry(QRect(215, 50, 170, 35))
self.pushButton_3.clicked.connect(deleteFile)
self.retranslateUi(Dialog)
QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QCoreApplication.translate("Dialog", u"Sheepstar Asset Uploader", None))
self.pushButton.setText(QCoreApplication.translate("Dialog", u"Auswählen", None))
self.pushButton_2.setText(QCoreApplication.translate("Dialog", u"Hochladen", None))
self.pushButton_3.setText(QCoreApplication.translate("Dialog", u"Datei löschen", None))
def select(self):
file_name, _ = QFileDialog.getOpenFileName(None, "Open File")
self.lineEdit.setText(file_name)
def upload(self):
upload_file = self.lineEdit.text()
if os.path.isfile(upload_file):
try:
files = {'asset': open(upload_file, 'rb')}
headers = {'Authorization': "Bearer " + open('api-key.txt', 'r').read().replace("\n", "")}
post = requests.put(sheepstar_post + "/upload", files=files, headers=headers)
if post.status_code == 400:
sendError("Upload fehlgeschlagen", "Die Datei konnte nicht hochgeladen werden.")
elif post.status_code == 401:
sendError("Upload fehlgeschlagen", "Bitte ersetze den API-Key in der api-key.txt")
elif post.status_code == 200:
pyperclip.copy(post.json()['url'])
sendSuccess("Upload erfolgreich", "Der Link der Datei wurde in deine Zwischenablage kopiert.")
except Exception as e:
sendError("Upload fehlgeschlagen", "Es ist ein Fehler während des Uploads aufgetreten: " + str(e))
else:
sendError("Upload fehlgeschlagen", "Die Datei existiert nicht mehr auf deinem Computer")
if __name__ == "__main__":
if not os.path.exists('api-key.txt'):
f = open('api-key.txt', 'w')
f.write('replace this text with your api key')
f.close()
app = QApplication(sys.argv)
window = QDialog()
ui = Ui_Dialog()
ui.setupUi(window)
window.show()
sys.exit(app.exec_())