From dc84074a096f9e8d0a678a15120dffa9027d8993 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Gl=C3=BCpker?= Date: Thu, 7 Apr 2016 14:26:20 +0200 Subject: Add config file for user settings (api key, sound, soundfile) --- 9kwpyqt.py | 51 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/9kwpyqt.py b/9kwpyqt.py index d68a1b9..68960ca 100755 --- a/9kwpyqt.py +++ b/9kwpyqt.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 +import configparser import sys import time from PyQt5.QtCore import Qt, QTimer, QUrl, pyqtSignal @@ -10,10 +11,6 @@ from PyQt5.QtGui import QPixmap, QImage from PyQt5.QtMultimedia import QSound from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest, QNetworkReply -API_URL = "http://www.9kw.eu/index.cgi" -API_KEY = "" -soundfile = "notify.wav" - class ExtendedQLabel(QLabel): clicked = pyqtSignal(int, str) image = False # QPixmap() @@ -40,9 +37,8 @@ class ExtendedQLabel(QLabel): Qt.KeepAspectRatio) self.setPixmap(self.imageR) - - class CaptchaGUI(QWidget): + config = None # Configuration (9kwpyqt.ini) sound = False # QSound() timer = False # QTimer() for 30sec timing @@ -54,12 +50,10 @@ class CaptchaGUI(QWidget): currentSkipped = 0 currentQueued = 0 currentWorker = 0 - apiurl = API_URL+"?source=pythonapi&apikey="+API_KEY+"&" offlinemessage = "Click \"Start\" to fetch next captcha." waitingoncaptcha = 0 - def __init__(self, parent=None): super(CaptchaGUI, self).__init__(parent) self.setMinimumWidth(400) @@ -98,6 +92,7 @@ class CaptchaGUI(QWidget): QSizePolicy.Fixed, QSizePolicy.Fixed ) + self.soundCheckbox.clicked.connect(self.toggleSound) self.startstopButton.clicked.connect(self.toggleRunning) self.LayoutSettings = QHBoxLayout() @@ -116,13 +111,14 @@ class CaptchaGUI(QWidget): self.setLayout(mainLayout) self.setWindowTitle("Captcha 9kw PyQt") - if not API_KEY: - self.captchaImage.setText("API_KEY is missing.\nPlease edit this file and enter your key at the top.") + if not self.readConfig(): + self.captchaImage.setText("Config file not found. New file created.\nPlease edit the configuration file to enter your api key.\nYou need to restart this programm afterwards.") + self.startstopButton.setEnabled(False) + return + if not self.config['DEFAULT']['API_KEY']: + self.captchaImage.setText("API Key not found.\nPlease edit the configuration file to enter your api key.\nYou need to restart this programm afterwards.") self.startstopButton.setEnabled(False) return - - # load soundfile - self.sound = QSound(soundfile) # Initialize network self.NetworkManager = QNetworkAccessManager() @@ -182,6 +178,10 @@ class CaptchaGUI(QWidget): self.startstopButton.setText(self.running and "Stop" or "Start") self.startstopButton.setChecked(self.running) + def toggleSound(self): + self.config['DEFAULT']['Sound'] = self.soundCheckbox.isChecked() and 'yes' or 'no' + self.writeConfig() + ################################################## # Handle network ################################################## @@ -314,6 +314,31 @@ class CaptchaGUI(QWidget): ################################################## # Handle logic ################################################## + def readConfig(self): + defaultconfig = {} + defaultconfig['API_URL'] = 'http://www.9kw.eu/index.cgi' + defaultconfig['API_KEY'] = '' + defaultconfig['Sound'] = 'yes' + defaultconfig['Soundfile'] = 'notify.wav' + self.config = configparser.ConfigParser(defaultconfig) + try: + self.config.read_file(open('9kwpyqt.ini')) + except FileNotFoundError: + self.writeConfig() + return False + + # Overwrite file to add potential new options + self.writeConfig() + + self.apiurl = self.config['DEFAULT']['API_URL']+"?source=pythonapi&apikey="+self.config['DEFAULT']['API_KEY']+"&" + self.sound = QSound(self.config['DEFAULT']['Soundfile']) + self.soundCheckbox.setChecked( self.config.getboolean('DEFAULT', 'Sound') ) + return True + + def writeConfig(self): + with open('9kwpyqt.ini', 'w') as configfile: + self.config.write(configfile) + def skipCaptcha(self): self.submitCaptcha(skip=True) -- cgit v1.2.3