@@ -67,99 +67,106 @@ async def update_nzb_options():
6767async def load_settings ():
6868 if not Config .DATABASE_URL :
6969 return
70+
7071 for p in ["thumbnails" , "tokens" , "rclone" ]:
7172 if await aiopath .exists (p ):
7273 await rmtree (p , ignore_errors = True )
74+
7375 await database .connect ()
74- if database .db is not None :
75- BOT_ID = Config .BOT_TOKEN .split (":" , 1 )[0 ]
76+ if database .db is None :
77+ return
78+
79+ BOT_ID = Config .BOT_TOKEN .split (":" , 1 )[0 ]
80+
81+ try :
7682 settings = import_module ("config" )
7783 config_file = {
7884 key : value .strip () if isinstance (value , str ) else value
7985 for key , value in vars (settings ).items ()
8086 if not key .startswith ("__" )
8187 }
82- old_config = await database .db .settings .deployConfig .find_one (
83- {"_id" : BOT_ID }, {"_id" : 0 }
84- )
85- if old_config is None :
86- await database .db .settings .deployConfig .replace_one (
87- {"_id" : BOT_ID }, config_file , upsert = True
88- )
89- if old_config and old_config != config_file :
90- LOGGER .info ("Replacing existing deploy config in Database" )
91- await database .db .settings .deployConfig .replace_one (
92- {"_id" : BOT_ID }, config_file , upsert = True
93- )
94- else :
95- config_dict = await database .db .settings .config .find_one (
96- {"_id" : BOT_ID }, {"_id" : 0 }
97- )
98- if config_dict :
99- Config .load_dict (config_dict )
88+ except ModuleNotFoundError :
89+ config_file = {}
10090
101- if pf_dict := await database .db .settings .files .find_one (
102- {"_id" : BOT_ID }, {"_id" : 0 }
103- ):
104- for key , value in pf_dict .items ():
105- if value :
106- file_ = key .replace ("__" , "." )
107- async with aiopen (file_ , "wb+" ) as f :
108- await f .write (value )
109-
110- if a2c_options := await database .db .settings .aria2c .find_one (
91+ old_config = await database .db .settings .deployConfig .find_one (
92+ {"_id" : BOT_ID }, {"_id" : 0 }
93+ )
94+ if old_config is None and config_file :
95+ await database .db .settings .deployConfig .replace_one (
96+ {"_id" : BOT_ID }, config_file , upsert = True
97+ )
98+ elif old_config and config_file and old_config != config_file :
99+ LOGGER .info ("Replacing existing deploy config in Database" )
100+ await database .db .settings .deployConfig .replace_one (
101+ {"_id" : BOT_ID }, config_file , upsert = True
102+ )
103+ else :
104+ config_dict = await database .db .settings .config .find_one (
111105 {"_id" : BOT_ID }, {"_id" : 0 }
112- ):
113- aria2_options .update (a2c_options )
106+ )
107+ if config_dict :
108+ Config .load_dict (config_dict )
114109
115- if qbit_opt := await database .db .settings .qbittorrent .find_one (
116- {"_id" : BOT_ID }, {"_id" : 0 }
117- ):
118- qbit_options .update (qbit_opt )
110+ if pf_dict := await database .db .settings .files .find_one (
111+ {"_id" : BOT_ID }, {"_id" : 0 }
112+ ):
113+ for key , value in pf_dict .items ():
114+ if value :
115+ file_ = key .replace ("__" , "." )
116+ async with aiopen (file_ , "wb+" ) as f :
117+ await f .write (value )
118+
119+ if a2c_options := await database .db .settings .aria2c .find_one (
120+ {"_id" : BOT_ID }, {"_id" : 0 }
121+ ):
122+ aria2_options .update (a2c_options )
119123
120- if nzb_opt := await database .db .settings .nzb .find_one (
121- {"_id" : BOT_ID }, {"_id" : 0 }
122- ):
123- if await aiopath .exists ("sabnzbd/SABnzbd.ini.bak" ):
124- await remove ("sabnzbd/SABnzbd.ini.bak" )
125- ((key , value ),) = nzb_opt .items ()
126- file_ = key .replace ("__" , "." )
127- async with aiopen (f"sabnzbd/{ file_ } " , "wb+" ) as f :
128- await f .write (value )
129-
130- if await database .db .users .find_one ():
131- for p in ["thumbnails" , "tokens" , "rclone" ]:
132- if not await aiopath .exists (p ):
133- await makedirs (p )
134- rows = database .db .users .find ({})
135- async for row in rows :
136- uid = row ["_id" ]
137- del row ["_id" ]
138- thumb_path = f"thumbnails/{ uid } .jpg"
139- rclone_config_path = f"rclone/{ uid } .conf"
140- token_path = f"tokens/{ uid } .pickle"
141- if row .get ("THUMBNAIL" ):
142- async with aiopen (thumb_path , "wb+" ) as f :
143- await f .write (row ["THUMBNAIL" ])
144- row ["THUMBNAIL" ] = thumb_path
145- if row .get ("RCLONE_CONFIG" ):
146- async with aiopen (rclone_config_path , "wb+" ) as f :
147- await f .write (row ["RCLONE_CONFIG" ])
148- row ["RCLONE_CONFIG" ] = rclone_config_path
149- if row .get ("TOKEN_PICKLE" ):
150- async with aiopen (token_path , "wb+" ) as f :
151- await f .write (row ["TOKEN_PICKLE" ])
152- row ["TOKEN_PICKLE" ] = token_path
153- user_data [uid ] = row
154- LOGGER .info ("Users data has been imported from Database" )
155-
156- if await database .db .rss [BOT_ID ].find_one ():
157- rows = database .db .rss [BOT_ID ].find ({})
158- async for row in rows :
159- user_id = row ["_id" ]
160- del row ["_id" ]
161- rss_dict [user_id ] = row
162- LOGGER .info ("Rss data has been imported from Database." )
124+ if qbit_opt := await database .db .settings .qbittorrent .find_one (
125+ {"_id" : BOT_ID }, {"_id" : 0 }
126+ ):
127+ qbit_options .update (qbit_opt )
128+
129+ if nzb_opt := await database .db .settings .nzb .find_one ({"_id" : BOT_ID }, {"_id" : 0 }):
130+ if await aiopath .exists ("sabnzbd/SABnzbd.ini.bak" ):
131+ await remove ("sabnzbd/SABnzbd.ini.bak" )
132+ ((key , value ),) = nzb_opt .items ()
133+ file_ = key .replace ("__" , "." )
134+ async with aiopen (f"sabnzbd/{ file_ } " , "wb+" ) as f :
135+ await f .write (value )
136+
137+ if await database .db .users .find_one ():
138+ for p in ["thumbnails" , "tokens" , "rclone" ]:
139+ if not await aiopath .exists (p ):
140+ await makedirs (p )
141+ rows = database .db .users .find ({})
142+ async for row in rows :
143+ uid = row ["_id" ]
144+ del row ["_id" ]
145+ thumb_path = f"thumbnails/{ uid } .jpg"
146+ rclone_config_path = f"rclone/{ uid } .conf"
147+ token_path = f"tokens/{ uid } .pickle"
148+ if row .get ("THUMBNAIL" ):
149+ async with aiopen (thumb_path , "wb+" ) as f :
150+ await f .write (row ["THUMBNAIL" ])
151+ row ["THUMBNAIL" ] = thumb_path
152+ if row .get ("RCLONE_CONFIG" ):
153+ async with aiopen (rclone_config_path , "wb+" ) as f :
154+ await f .write (row ["RCLONE_CONFIG" ])
155+ row ["RCLONE_CONFIG" ] = rclone_config_path
156+ if row .get ("TOKEN_PICKLE" ):
157+ async with aiopen (token_path , "wb+" ) as f :
158+ await f .write (row ["TOKEN_PICKLE" ])
159+ row ["TOKEN_PICKLE" ] = token_path
160+ user_data [uid ] = row
161+ LOGGER .info ("Users data has been imported from Database" )
162+
163+ if await database .db .rss [BOT_ID ].find_one ():
164+ rows = database .db .rss [BOT_ID ].find ({})
165+ async for row in rows :
166+ user_id = row ["_id" ]
167+ del row ["_id" ]
168+ rss_dict [user_id ] = row
169+ LOGGER .info ("Rss data has been imported from Database." )
163170
164171
165172async def save_settings ():
0 commit comments