| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- import sys
- from PyQt5.QtWidgets import QMessageBox
- from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout, QHBoxLayout, QWidgetItem
- from PyQt5.QtCore import Qt
- import sqlite3
- import threading
- import time
- from extract_and_save import run
- import logging
- logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(message)s')
- logger = logging.getLogger(__name__)
- class MailSenderApp(QWidget):
- def __init__(self):
- super().__init__()
- self.initUI()
- def initUI(self):
- self.setWindowTitle('Mail Sender')
- self.setGeometry(100, 100, 200, 200)
- main_layout = QVBoxLayout()
- main_layout.setAlignment(Qt.AlignmentFlag.AlignLeft) # 设置垂直布局居中
- # 创建标签和输入框,并将它们放在一个水平布局中
- def create_row(label_text):
- layout = QHBoxLayout()
- label = QLabel(label_text, self)
- edit = QLineEdit(self)
- edit.setFixedWidth(300) # 设置输入框的固定宽度为500
- layout.addWidget(label)
- layout.addWidget(edit)
- return layout, edit
- self.phone_layout, self.phone_edit = create_row('手机号码:')
- self.session_layout, self.session_edit = create_row('Session:')
- self.sender_email_layout, self.sender_email_edit = create_row('发送人邮箱号:')
- self.receiver_email_layout, self.receiver_email_edit = create_row('接受人邮箱号:')
- self.smtp_proxy_layout, self.smtp_proxy_edit = create_row('邮箱SMTP代理:')
- self.smtp_account_layout, self.smtp_account_edit = create_row('SMTP账号:')
- self.smtp_password_layout, self.smtp_password_edit = create_row('SMTP密码:')
- self.email_alias_layout, self.email_alias_edit = create_row('邮箱特殊代号:')
- self.scan_interval_layout, self.scan_interval_edit = create_row('扫描间隔时间:')
- self.error_count_layout, self.error_count_edit = create_row('异常次数\n(超出则退出扫描):')
- self.success_scan_interval_layout, self.success_scan_interval_edit = create_row('没有短信验证\n中间间隔时间(秒):')
- # 将水平布局添加到垂直布局中
- for layout in [
- self.phone_layout,
- self.session_layout,
- self.sender_email_layout,
- self.receiver_email_layout,
- self.smtp_proxy_layout,
- self.smtp_account_layout,
- self.smtp_password_layout,
- self.email_alias_layout,
- self.scan_interval_layout,
- self.error_count_layout,
- self.success_scan_interval_layout
- ]:
- main_layout.addLayout(layout)
- # 创建按钮
- self.save_button = QPushButton('保存', self)
- self.save_button.clicked.connect(self.save_settings)
- self.start_scan_button = QPushButton('开启扫描', self)
- self.start_scan_button.clicked.connect(self.start_scan)
- self.stop_scan_button = QPushButton('关闭扫描', self)
- self.stop_scan_button.clicked.connect(self.stop_scan)
- # 创建一个水平布局来放置按钮
- button_layout = QHBoxLayout()
- button_layout.addWidget(self.save_button)
- button_layout.addWidget(self.start_scan_button)
- button_layout.addWidget(self.stop_scan_button)
- # 将按钮布局添加到主布局中
- main_layout.addLayout(button_layout)
- # 设置主窗口的布局
- container = QWidget()
- container.setLayout(main_layout)
- container.setMaximumWidth(300) # 设置容器的最大宽度为1000
- main_window_layout = QVBoxLayout()
- main_window_layout.addWidget(container, alignment=Qt.AlignmentFlag.AlignLeft)
- self.setLayout(main_window_layout)
- # 从数据库中读取数据并填充到输入框中
- self.load_settings()
- def save_settings(self):
- phone = self.phone_edit.text()
- session = self.session_edit.text()
- sender_email = self.sender_email_edit.text()
- receiver_email = self.receiver_email_edit.text()
- smtp_proxy = self.smtp_proxy_edit.text()
- smtp_account = self.smtp_account_edit.text()
- smtp_password = self.smtp_password_edit.text()
- email_alias = self.email_alias_edit.text()
- scan_interval = self.scan_interval_edit.text()
- error_count = self.error_count_edit.text()
- success_scan_interval = self.success_scan_interval_edit.text()
- # 保存到SQLite数据库
- conn = sqlite3.connect('settings.db')
- cursor = conn.cursor()
- cursor.execute('''CREATE TABLE IF NOT EXISTS settings (
- phone TEXT,
- session TEXT,
- sender_email TEXT,
- receiver_email TEXT,
- smtp_proxy TEXT,
- smtp_account TEXT,
- smtp_password TEXT,
- email_alias TEXT,
- scan_interval TEXT,
- error_count TEXT,
- success_scan_interval TEXT
- )''')
- cursor.execute('''DELETE FROM settings''') # 删除现有数据
- cursor.execute('''INSERT INTO settings (
- phone,
- session,
- sender_email,
- receiver_email,
- smtp_proxy,
- smtp_account,
- smtp_password,
- email_alias,
- scan_interval,
- error_count,
- success_scan_interval
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?,?,?)''',
- (phone, session, sender_email, receiver_email, smtp_proxy, smtp_account, smtp_password, email_alias, scan_interval,error_count,success_scan_interval))
- conn.commit()
- conn.close()
- # 提示保存成功
- QMessageBox.information(self, '保存成功', '设置已保存到数据库。')
- def load_settings(self):
- # 从SQLite数据库中读取数据
- conn = sqlite3.connect('settings.db')
- cursor = conn.cursor()
- cursor.execute('''CREATE TABLE IF NOT EXISTS settings (
- phone TEXT,
- session TEXT,
- sender_email TEXT,
- receiver_email TEXT,
- smtp_proxy TEXT,
- smtp_account TEXT,
- smtp_password TEXT,
- email_alias TEXT,
- scan_interval TEXT,
- error_count TEXT,
- success_scan_interval TEXT
- )''')
- cursor.execute('''SELECT * FROM settings''')
- row = cursor.fetchone()
- conn.close()
- if row:
- self.phone_edit.setText(row[0])
- self.session_edit.setText(row[1])
- self.sender_email_edit.setText(row[2])
- self.receiver_email_edit.setText(row[3])
- self.smtp_proxy_edit.setText(row[4])
- self.smtp_account_edit.setText(row[5])
- self.smtp_password_edit.setText(row[6])
- self.email_alias_edit.setText(row[7])
- self.scan_interval_edit.setText(row[8])
- self.error_count_edit.setText(row[9])
- self.success_scan_interval_edit.setText(row[10])
- def start_scan(self):
- # 实现开启扫描的逻辑
- # 获取session的值
- session = self.session_edit.text()
- # 获取发送人邮箱号
- sender_email = self.sender_email_edit.text()
- # 获取接受人邮箱号
- receiver_email = self.receiver_email_edit.text()
- smtp_proxy = self.smtp_proxy_edit.text()
- smtp_account = self.smtp_account_edit.text()
- smtp_password = self.smtp_password_edit.text()
- email_alias = self.email_alias_edit.text()
- # 获取间隔时间
- scan_interval = self.scan_interval_edit.text()
- # 异常次数
- error_count = self.error_count_edit.text()
- # 获取成功间隔时间
- scan_interval = self.scan_interval_edit.text()
- # 获取成功间隔时间
- success_scan_interval = self.success_scan_interval_edit.text()
- logger.info(f'session:{session}')
- logger.info(f'发送人邮箱号:sender_email:{sender_email}')
- logger.info(f'接收人邮箱号:receiver_email:{receiver_email}')
- logger.info(f'smtp代理:smtp_proxy:{smtp_proxy}')
- logger.info(f'smtp账号:smtp_account:{smtp_account}')
- logger.info(f'smtp密码:smtp_password:{smtp_password}')
- logger.info(f'邮箱特殊代号:email_alias:{email_alias}')
- logger.info(f'间隔时长(秒):scan_interval:{scan_interval}')
- logger.info(f'异常次数:error_count:{int(error_count)}')
- logger.info(f'没有短信验证中间间隔时长(秒):success_scan_interval:{success_scan_interval}')
- global stop_flag
- stop_flag = False
- # 清空表
- logger.info("清空表sms_codes")
- conn = sqlite3.connect('settings.db')
- cursor = conn.cursor()
- cursor.execute('''CREATE TABLE IF NOT EXISTS sms_codes (edit_content TEXT, send_content_short TEXT)''')
- cursor.execute('delete from sms_codes')
- conn.commit()
- conn.close()
- # 创建一个线程,并启动线程,线程每间隔10时间执行一下extract_and_save.py中的run方法
- thread = threading.Thread(target=thread_function, args=(session, sender_email, receiver_email, smtp_proxy, smtp_account, smtp_password, email_alias,scan_interval,error_count,success_scan_interval))
- thread.start()
- logger.info("扫描已开启")
- QMessageBox.information(self, '扫描已开启', '扫描已开启。')
- def stop_scan(self):
- # 获取全局的变量 stop_flag
- global stop_flag
- stop_flag = True
- logger.info("扫描已关闭")
- QMessageBox.information(self, '扫描已关闭', '扫描已关闭。')
- def thread_function(session, sender_email, receiver_email, smtp_proxy, smtp_account, smtp_password, email_alias,scan_interval,error_count,success_scan_interval):
- #获取 全局的变量 stop_flag
- global stop_flag
- while True:
- # logger.info(f'扫描停止标志:stop_flag = {stop_flag}')
- # stop_flag 为true 则跳出
- if stop_flag:
- return
- stop1_flag = run(
- session=session,
- sender_email=sender_email,
- receiver_email=receiver_email,
- smtp_proxy=smtp_proxy,
- smtp_account=smtp_account,
- smtp_password=smtp_password,
- email_alias=email_alias,
- error_count=error_count
- )
- logger.info(f'扫描停止标志:stop1_flag = {stop1_flag}')
- if stop1_flag == 'stop':
- return
- elif stop1_flag == 'success':
- logger.info(f"没有短信验证时,间隔{float(success_scan_interval)}秒")
- time.sleep(float(success_scan_interval))
- time.sleep(float(scan_interval))
- if __name__ == "__main__":
- app = QApplication(sys.argv)
- ex = MailSenderApp()
- ex.show()
- sys.exit(app.exec())
|