1- import time
2- import urllib .parse
31from pathlib import Path
4- from tkinter import StringVar
5- from typing import TypeVar
2+ from tkinter import BooleanVar , StringVar
3+ from typing import Callable , Optional , TypeVar
64
75import customtkinter as ctk
86import i18n
9- from component .component import EntryComponent , OptionMenuComponent , OptionMenuTupleComponent , PaddingComponent
7+ from component .component import CheckBoxComponent , EntryComponent , OptionMenuComponent , OptionMenuTupleComponent , PaddingComponent
108from component .tab_menu import TabMenuComponent
119from customtkinter import CTkBaseClass , CTkButton , CTkFrame , CTkLabel , CTkScrollableFrame
1210from lib .DGPSessionWrap import DgpSessionWrap
1311from lib .toast import ToastController , error_toast
14- from selenium import webdriver
12+ from models . shortcut_data import BrowserConfigData
1513from static .config import DataPathConfig
1614from static .constant import Constant
17- from utils .utils import children_destroy , file_create
15+ from utils .utils import children_destroy , file_create , get_driver , login_driver
1816
1917T = TypeVar ("T" )
2018
@@ -94,35 +92,27 @@ def callback(self):
9492class AccountBrowserImport (CTkScrollableFrame ):
9593 toast : ToastController
9694 name : StringVar
97- browser : StringVar
95+ data : BrowserConfigData
9896
9997 def __init__ (self , master : CTkBaseClass ):
10098 super ().__init__ (master , fg_color = "transparent" )
10199 self .toast = ToastController (self )
102100 self .name = StringVar ()
103- self .browser = StringVar ()
101+ self .auto_refresh = BooleanVar (value = True )
102+ self .data = BrowserConfigData ()
104103
105104 def create (self ):
106105 CTkLabel (self , text = i18n .t ("app.account.import_browser_detail" ), justify = ctk .LEFT ).pack (anchor = ctk .W )
107106 text = i18n .t ("app.account.filename" )
108107 tooltip = i18n .t ("app.account.filename_tooltip" )
109108 EntryComponent (self , text = text , tooltip = tooltip , required = True , variable = self .name , alnum_only = True ).create ()
109+ CheckBoxComponent (self , text = i18n .t ("app.account.auto_refresh" ), variable = self .auto_refresh ).create ()
110110 text = i18n .t ("app.account.browser_select" )
111111 tooltip = i18n .t ("app.account.browser_select_tooltip" )
112- OptionMenuComponent (self , text = text , tooltip = tooltip , values = ["Chrome" , "Edge" , "Firefox" ], variable = self .browser ).create ()
112+ OptionMenuComponent (self , text = text , tooltip = tooltip , values = ["Chrome" , "Edge" , "Firefox" ], variable = self .data . browser ).create ()
113113 CTkButton (self , text = i18n .t ("app.account.import_browser" ), command = self .callback ).pack (fill = ctk .X , pady = 10 )
114114 return self
115115
116- def get_driver (self ):
117- if self .browser .get () == "Chrome" :
118- return webdriver .Chrome ()
119- elif self .browser .get () == "Edge" :
120- return webdriver .Edge ()
121- elif self .browser .get () == "Firefox" :
122- return webdriver .Firefox ()
123- else :
124- raise Exception (i18n .t ("app.account.browser_not_selected" ))
125-
126116 @error_toast
127117 def callback (self ):
128118 path = DataPathConfig .ACCOUNT .joinpath (self .name .get ()).with_suffix (".bytes" )
@@ -137,19 +127,20 @@ def callback(self):
137127 res = session .post_dgp (DgpSessionWrap .LOGIN_URL , json = {"prompt" : "" }).json ()
138128 if res ["result_code" ] != 100 :
139129 raise Exception (res ["error" ])
140- driver = self .get_driver ()
141- driver .get (res ["data" ]["url" ])
142- parsed_url = urllib .parse .urlparse (driver .current_url )
143- while not (parsed_url .netloc == "webdgp-gameplayer.games.dmm.com" and parsed_url .path == "/login/success" ):
144- time .sleep (0.2 )
145- parsed_url = urllib .parse .urlparse (driver .current_url )
130+
131+ profile_path = DataPathConfig .BROWSER_PROFILE .joinpath (self .data .profile_name .get ()).absolute ()
132+ driver = get_driver (self .data .browser .get (), profile_path )
133+ code = login_driver (res ["data" ]["url" ], driver )
146134 driver .quit ()
147- code = urllib .parse .parse_qs (parsed_url .query )["code" ][0 ]
148135 res = session .post_dgp (DgpSessionWrap .ACCESS_TOKEN , json = {"code" : code }).json ()
149136 if res ["result_code" ] != 100 :
150137 raise Exception (res ["error" ])
151138 session .actauth = {"accessToken" : res ["data" ]["access_token" ]}
152139 session .write_bytes (str (path ))
140+ if self .auto_refresh .get ():
141+ config_path = DataPathConfig .BROWSER_CONFIG .joinpath (self .name .get ()).with_suffix (".json" )
142+ file_create (config_path , name = i18n .t ("app.account.filename" ))
143+ self .data .write_path (config_path )
153144 self .toast .info (i18n .t ("app.account.import_browser_success" ))
154145
155146
@@ -159,6 +150,7 @@ class AccountEdit(CTkScrollableFrame):
159150 filename : StringVar
160151 body : CTkFrame
161152 body_var : dict [str , StringVar ]
153+ browser_config : Optional [BrowserConfigData ]
162154 body_filename : StringVar
163155
164156 def __init__ (self , master : CTkBaseClass ):
@@ -167,6 +159,7 @@ def __init__(self, master: CTkBaseClass):
167159 self .values = [x .stem for x in DataPathConfig .ACCOUNT .iterdir () if x .suffix == ".bytes" ]
168160 self .filename = StringVar ()
169161 self .body_var = {}
162+ self .browser_config = None
170163 self .body_filename = StringVar ()
171164
172165 def create (self ):
@@ -190,6 +183,13 @@ def select_callback(self, value: str):
190183 for key in session .actauth .keys ():
191184 self .body_var [key ] = StringVar (value = session .actauth [key ] or "" )
192185 EntryComponent (self .body , text = key , variable = self .body_var [key ]).create ()
186+ config_path = DataPathConfig .BROWSER_CONFIG .joinpath (self .filename .get ()).with_suffix (".json" )
187+ if config_path .exists ():
188+ self .browser_config = BrowserConfigData .from_path (config_path )
189+ PaddingComponent (self .body , height = 20 ).create ()
190+ EntryComponent (self .body , text = "browser" , variable = self .browser_config .browser ).create ()
191+ EntryComponent (self .body , text = "profile_name" , variable = self .browser_config .profile_name ).create ()
192+
193193 CTkButton (self .body , text = i18n .t ("app.account.save" ), command = self .save_callback ).pack (fill = ctk .X , pady = 10 )
194194 CTkButton (self .body , text = i18n .t ("app.account.delete" ), command = self .delete_callback ).pack (fill = ctk .X )
195195
@@ -202,19 +202,34 @@ def save_callback(self):
202202
203203 path = DataPathConfig .ACCOUNT .joinpath (self .filename .get ()).with_suffix (".bytes" )
204204 body_path = DataPathConfig .ACCOUNT .joinpath (self .body_filename .get ()).with_suffix (".bytes" )
205+ config_path = DataPathConfig .BROWSER_CONFIG .joinpath (self .filename .get ()).with_suffix (".json" )
206+ config_body_path = DataPathConfig .BROWSER_CONFIG .joinpath (self .body_filename .get ()).with_suffix (".json" )
207+
208+ def check_file (callback : Callable [[], None ]):
209+ if self .browser_config :
210+ callback ()
205211
206212 def write ():
207213 session = DgpSessionWrap .read_cookies ((Path (path )))
208214 for key in session .actauth .keys ():
209215 session .actauth [key ] = self .body_var [key ].get ()
210- session .write_bytes (str (Path (body_path )))
216+ session .write_bytes (str (body_path ))
217+ if self .browser_config :
218+ self .browser_config .write_path (config_body_path )
211219
212220 if path == body_path :
213221 write ()
214222 else :
215- file_create (body_path , name = i18n .t ("app.account.filename" ))
216- write ()
223+ try :
224+ file_create (body_path , name = i18n .t ("app.account.filename" ))
225+ check_file (lambda : file_create (config_body_path , name = i18n .t ("app.account.filename" )))
226+ write ()
227+ except Exception as e :
228+ body_path .unlink ()
229+ config_body_path .unlink ()
230+ raise e
217231 path .unlink ()
232+ check_file (lambda : config_path .unlink ())
218233 self .values .remove (self .filename .get ())
219234 self .values .append (self .body_filename .get ())
220235 self .filename .set (self .body_filename .get ())
0 commit comments