11import fnmatch
22import json
33import os
4- import platform
54import re
65import socket
76import subprocess
@@ -96,6 +95,10 @@ def save_config(update_data=True):
9695 config ['qb_api_ip' ] = '127.0.0.1'
9796 if 'qb_api_port' not in config :
9897 config ['qb_api_port' ] = 38080
98+ if 'qb_api_username' not in config :
99+ config ['qb_api_username' ] = ''
100+ if 'qb_api_password' not in config :
101+ config ['qb_api_password' ] = ''
99102 if 'text_browser_height' in config :
100103 del config ['text_browser_height' ]
101104except :
@@ -148,7 +151,12 @@ def format_path(s):
148151
149152
150153def format_path_by_system (s ):
151- if platform .system () == 'Windows' :
154+ # 保存路径格式化 兼容linux路径
155+ # 由于有远程调用api的需求, 所以这里不能限制斜杠格式
156+ # 简单判断一下吧
157+ if not s :
158+ return ''
159+ if s [0 ] != '/' :
152160 return format_path (s ).replace ('/' , '\\ ' )
153161 else :
154162 return format_path (s )
@@ -231,8 +239,10 @@ def check_qb_port_open():
231239 location = (config ['qb_api_ip' ], int (config ['qb_api_port' ]))
232240 result_of_check = a_socket .connect_ex (location )
233241 if result_of_check == 0 :
242+ logger .info ('qb端口可用' )
234243 return True
235244 else :
245+ logger .info ('qb端口不可用' )
236246 return False
237247
238248
@@ -429,7 +439,7 @@ class App(QWidget):
429439
430440 def __init__ (self ):
431441 super ().__init__ ()
432- self .title = 'qBittorrent 订阅下载规则管理 v1.1.8 by Nriver'
442+ self .title = 'qBittorrent 订阅下载规则管理 v1.1.9 by Nriver'
433443 # 图标
434444 self .setWindowIcon (QtGui .QIcon (resource_path ('QBRssManager.ico' )))
435445 self .left = 0
@@ -934,11 +944,29 @@ def on_cell_changed(self):
934944 def on_import_exist_qb_rule_action (self ):
935945 global data_list
936946 logger .info ('读取qb订阅规则' )
937- try :
938- with open (config ['rules_path' ], 'r' , encoding = 'utf-8' ) as f :
939- rss_rules = json .loads (f .read ())
940- except :
941- return
947+
948+ # 尝试通过api读取rss配置
949+ rss_rules = []
950+
951+ # 尝试通过api和qb通信
952+ if config ['use_qb_api' ] == 1 and check_qb_port_open ():
953+ # 使用qb的api, 可以不重启qb
954+ try :
955+ qb_client .auth_log_in (username = config ['qb_api_username' ], password = config ['qb_api_password' ])
956+ logger .info ('通过api获取已有规则' )
957+ rss_rules = qb_client .rss_rules ()
958+ except qbittorrentapi .LoginFailed as e :
959+ logger .error (e )
960+ else :
961+ logger .info ('无法通过qb的api获取rss数据' )
962+
963+ if not rss_rules :
964+ logger .info ('尝试读取本机rss配置文件' )
965+ try :
966+ with open (config ['rules_path' ], 'r' , encoding = 'utf-8' ) as f :
967+ rss_rules = json .loads (f .read ())
968+ except :
969+ return
942970
943971 # 对比表格内已有数据
944972 exist_data = {}
@@ -947,7 +975,7 @@ def on_import_exist_qb_rule_action(self):
947975 "enabled" : True ,
948976 "mustContain" : x [2 ],
949977 "mustNotContain" : x [3 ],
950- "savePath" : format_path (x [5 ]),
978+ "savePath" : format_path_by_system (x [5 ]),
951979 "affectedFeeds" : [x [6 ], ],
952980 "assignedCategory" : x [7 ]
953981 }
@@ -1034,7 +1062,7 @@ def on_export_click(self):
10341062 "enabled" : True ,
10351063 "mustContain" : x [2 ],
10361064 "mustNotContain" : x [3 ],
1037- "savePath" : format_path (x [5 ]),
1065+ "savePath" : format_path_by_system (x [5 ]),
10381066 "affectedFeeds" : [x [6 ], ],
10391067 "assignedCategory" : x [7 ]
10401068 }
@@ -1049,7 +1077,7 @@ def on_export_click(self):
10491077 "enabled" : True ,
10501078 "mustContain" : x [2 ],
10511079 "mustNotContain" : x [3 ],
1052- "savePath" : format_path (x [5 ]),
1080+ "savePath" : format_path_by_system (x [5 ]),
10531081 "affectedFeeds" : [x [6 ], ],
10541082 "assignedCategory" : x [7 ]
10551083 }
0 commit comments