main.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import sys
  2. from PyQt5.QtWidgets import QMessageBox
  3. from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout, QHBoxLayout, QWidgetItem
  4. from PyQt5.QtCore import Qt
  5. import sqlite3
  6. import threading
  7. import time
  8. from extract_and_save import run
  9. import logging
  10. logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(message)s')
  11. logger = logging.getLogger(__name__)
  12. class MailSenderApp(QWidget):
  13. def __init__(self):
  14. super().__init__()
  15. self.initUI()
  16. def initUI(self):
  17. self.setWindowTitle('Mail Sender')
  18. self.setGeometry(100, 100, 200, 200)
  19. main_layout = QVBoxLayout()
  20. main_layout.setAlignment(Qt.AlignmentFlag.AlignLeft) # 设置垂直布局居中
  21. # 创建标签和输入框,并将它们放在一个水平布局中
  22. def create_row(label_text):
  23. layout = QHBoxLayout()
  24. label = QLabel(label_text, self)
  25. edit = QLineEdit(self)
  26. edit.setFixedWidth(300) # 设置输入框的固定宽度为500
  27. layout.addWidget(label)
  28. layout.addWidget(edit)
  29. return layout, edit
  30. self.phone_layout, self.phone_edit = create_row('手机号码:')
  31. self.session_layout, self.session_edit = create_row('Session:')
  32. self.sender_email_layout, self.sender_email_edit = create_row('发送人邮箱号:')
  33. self.receiver_email_layout, self.receiver_email_edit = create_row('接受人邮箱号:')
  34. self.smtp_proxy_layout, self.smtp_proxy_edit = create_row('邮箱SMTP代理:')
  35. self.smtp_account_layout, self.smtp_account_edit = create_row('SMTP账号:')
  36. self.smtp_password_layout, self.smtp_password_edit = create_row('SMTP密码:')
  37. self.email_alias_layout, self.email_alias_edit = create_row('邮箱特殊代号:')
  38. self.scan_interval_layout, self.scan_interval_edit = create_row('扫描间隔时间:')
  39. self.error_count_layout, self.error_count_edit = create_row('异常次数\n(超出则退出扫描):')
  40. self.success_scan_interval_layout, self.success_scan_interval_edit = create_row('没有短信验证\n中间间隔时间(秒):')
  41. # 将水平布局添加到垂直布局中
  42. for layout in [
  43. self.phone_layout,
  44. self.session_layout,
  45. self.sender_email_layout,
  46. self.receiver_email_layout,
  47. self.smtp_proxy_layout,
  48. self.smtp_account_layout,
  49. self.smtp_password_layout,
  50. self.email_alias_layout,
  51. self.scan_interval_layout,
  52. self.error_count_layout,
  53. self.success_scan_interval_layout
  54. ]:
  55. main_layout.addLayout(layout)
  56. # 创建按钮
  57. self.save_button = QPushButton('保存', self)
  58. self.save_button.clicked.connect(self.save_settings)
  59. self.start_scan_button = QPushButton('开启扫描', self)
  60. self.start_scan_button.clicked.connect(self.start_scan)
  61. self.stop_scan_button = QPushButton('关闭扫描', self)
  62. self.stop_scan_button.clicked.connect(self.stop_scan)
  63. # 创建一个水平布局来放置按钮
  64. button_layout = QHBoxLayout()
  65. button_layout.addWidget(self.save_button)
  66. button_layout.addWidget(self.start_scan_button)
  67. button_layout.addWidget(self.stop_scan_button)
  68. # 将按钮布局添加到主布局中
  69. main_layout.addLayout(button_layout)
  70. # 设置主窗口的布局
  71. container = QWidget()
  72. container.setLayout(main_layout)
  73. container.setMaximumWidth(300) # 设置容器的最大宽度为1000
  74. main_window_layout = QVBoxLayout()
  75. main_window_layout.addWidget(container, alignment=Qt.AlignmentFlag.AlignLeft)
  76. self.setLayout(main_window_layout)
  77. # 从数据库中读取数据并填充到输入框中
  78. self.load_settings()
  79. def save_settings(self):
  80. phone = self.phone_edit.text()
  81. session = self.session_edit.text()
  82. sender_email = self.sender_email_edit.text()
  83. receiver_email = self.receiver_email_edit.text()
  84. smtp_proxy = self.smtp_proxy_edit.text()
  85. smtp_account = self.smtp_account_edit.text()
  86. smtp_password = self.smtp_password_edit.text()
  87. email_alias = self.email_alias_edit.text()
  88. scan_interval = self.scan_interval_edit.text()
  89. error_count = self.error_count_edit.text()
  90. success_scan_interval = self.success_scan_interval_edit.text()
  91. # 保存到SQLite数据库
  92. conn = sqlite3.connect('settings.db')
  93. cursor = conn.cursor()
  94. cursor.execute('''CREATE TABLE IF NOT EXISTS settings (
  95. phone TEXT,
  96. session TEXT,
  97. sender_email TEXT,
  98. receiver_email TEXT,
  99. smtp_proxy TEXT,
  100. smtp_account TEXT,
  101. smtp_password TEXT,
  102. email_alias TEXT,
  103. scan_interval TEXT,
  104. error_count TEXT,
  105. success_scan_interval TEXT
  106. )''')
  107. cursor.execute('''DELETE FROM settings''') # 删除现有数据
  108. cursor.execute('''INSERT INTO settings (
  109. phone,
  110. session,
  111. sender_email,
  112. receiver_email,
  113. smtp_proxy,
  114. smtp_account,
  115. smtp_password,
  116. email_alias,
  117. scan_interval,
  118. error_count,
  119. success_scan_interval
  120. ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?,?,?)''',
  121. (phone, session, sender_email, receiver_email, smtp_proxy, smtp_account, smtp_password, email_alias, scan_interval,error_count,success_scan_interval))
  122. conn.commit()
  123. conn.close()
  124. # 提示保存成功
  125. QMessageBox.information(self, '保存成功', '设置已保存到数据库。')
  126. def load_settings(self):
  127. # 从SQLite数据库中读取数据
  128. conn = sqlite3.connect('settings.db')
  129. cursor = conn.cursor()
  130. cursor.execute('''CREATE TABLE IF NOT EXISTS settings (
  131. phone TEXT,
  132. session TEXT,
  133. sender_email TEXT,
  134. receiver_email TEXT,
  135. smtp_proxy TEXT,
  136. smtp_account TEXT,
  137. smtp_password TEXT,
  138. email_alias TEXT,
  139. scan_interval TEXT,
  140. error_count TEXT,
  141. success_scan_interval TEXT
  142. )''')
  143. cursor.execute('''SELECT * FROM settings''')
  144. row = cursor.fetchone()
  145. conn.close()
  146. if row:
  147. self.phone_edit.setText(row[0])
  148. self.session_edit.setText(row[1])
  149. self.sender_email_edit.setText(row[2])
  150. self.receiver_email_edit.setText(row[3])
  151. self.smtp_proxy_edit.setText(row[4])
  152. self.smtp_account_edit.setText(row[5])
  153. self.smtp_password_edit.setText(row[6])
  154. self.email_alias_edit.setText(row[7])
  155. self.scan_interval_edit.setText(row[8])
  156. self.error_count_edit.setText(row[9])
  157. self.success_scan_interval_edit.setText(row[10])
  158. def start_scan(self):
  159. # 实现开启扫描的逻辑
  160. # 获取session的值
  161. session = self.session_edit.text()
  162. # 获取发送人邮箱号
  163. sender_email = self.sender_email_edit.text()
  164. # 获取接受人邮箱号
  165. receiver_email = self.receiver_email_edit.text()
  166. smtp_proxy = self.smtp_proxy_edit.text()
  167. smtp_account = self.smtp_account_edit.text()
  168. smtp_password = self.smtp_password_edit.text()
  169. email_alias = self.email_alias_edit.text()
  170. # 获取间隔时间
  171. scan_interval = self.scan_interval_edit.text()
  172. # 异常次数
  173. error_count = self.error_count_edit.text()
  174. # 获取成功间隔时间
  175. scan_interval = self.scan_interval_edit.text()
  176. # 获取成功间隔时间
  177. success_scan_interval = self.success_scan_interval_edit.text()
  178. logger.info(f'session:{session}')
  179. logger.info(f'发送人邮箱号:sender_email:{sender_email}')
  180. logger.info(f'接收人邮箱号:receiver_email:{receiver_email}')
  181. logger.info(f'smtp代理:smtp_proxy:{smtp_proxy}')
  182. logger.info(f'smtp账号:smtp_account:{smtp_account}')
  183. logger.info(f'smtp密码:smtp_password:{smtp_password}')
  184. logger.info(f'邮箱特殊代号:email_alias:{email_alias}')
  185. logger.info(f'间隔时长(秒):scan_interval:{scan_interval}')
  186. logger.info(f'异常次数:error_count:{int(error_count)}')
  187. logger.info(f'没有短信验证中间间隔时长(秒):success_scan_interval:{success_scan_interval}')
  188. global stop_flag
  189. stop_flag = False
  190. # 清空表
  191. logger.info("清空表sms_codes")
  192. conn = sqlite3.connect('settings.db')
  193. cursor = conn.cursor()
  194. cursor.execute('''CREATE TABLE IF NOT EXISTS sms_codes (edit_content TEXT, send_content_short TEXT)''')
  195. cursor.execute('delete from sms_codes')
  196. conn.commit()
  197. conn.close()
  198. # 创建一个线程,并启动线程,线程每间隔10时间执行一下extract_and_save.py中的run方法
  199. 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))
  200. thread.start()
  201. logger.info("扫描已开启")
  202. QMessageBox.information(self, '扫描已开启', '扫描已开启。')
  203. def stop_scan(self):
  204. # 获取全局的变量 stop_flag
  205. global stop_flag
  206. stop_flag = True
  207. logger.info("扫描已关闭")
  208. QMessageBox.information(self, '扫描已关闭', '扫描已关闭。')
  209. def thread_function(session, sender_email, receiver_email, smtp_proxy, smtp_account, smtp_password, email_alias,scan_interval,error_count,success_scan_interval):
  210. #获取 全局的变量 stop_flag
  211. global stop_flag
  212. while True:
  213. # logger.info(f'扫描停止标志:stop_flag = {stop_flag}')
  214. # stop_flag 为true 则跳出
  215. if stop_flag:
  216. return
  217. stop1_flag = run(
  218. session=session,
  219. sender_email=sender_email,
  220. receiver_email=receiver_email,
  221. smtp_proxy=smtp_proxy,
  222. smtp_account=smtp_account,
  223. smtp_password=smtp_password,
  224. email_alias=email_alias,
  225. error_count=error_count
  226. )
  227. logger.info(f'扫描停止标志:stop1_flag = {stop1_flag}')
  228. if stop1_flag == 'stop':
  229. return
  230. elif stop1_flag == 'success':
  231. logger.info(f"没有短信验证时,间隔{float(success_scan_interval)}秒")
  232. time.sleep(float(success_scan_interval))
  233. time.sleep(float(scan_interval))
  234. if __name__ == "__main__":
  235. app = QApplication(sys.argv)
  236. ex = MailSenderApp()
  237. ex.show()
  238. sys.exit(app.exec())