66from urllib .parse import urlparse
77
88
9- # Function to convert Discord snowflake ID to timestamp
109def snowflake_to_timestamp (snowflake ):
1110 try :
1211 discord_epoch = 1420070400000
@@ -16,21 +15,16 @@ def snowflake_to_timestamp(snowflake):
1615 return int (datetime .datetime .now ().timestamp ())
1716
1817
19- # Function to load existing database
2018def load_database (file_path ):
2119 try :
2220 print (f"Loading database from { file_path } ..." )
2321 with open (file_path , 'r' ) as file :
2422 return json .load (file )
25- except FileNotFoundError :
26- print (f"Database file not found. Creating new database." )
27- return {}
28- except json .JSONDecodeError :
29- print (f"Error parsing database file. Creating new database." )
23+ except (FileNotFoundError , json .JSONDecodeError ):
24+ print (f"Database file not found or invalid. Creating new database." )
3025 return {}
3126
3227
33- # Function to save updated database
3428def save_database (file_path , data ):
3529 directory = os .path .dirname (file_path )
3630 if not os .path .exists (directory ):
@@ -43,25 +37,46 @@ def save_database(file_path, data):
4337 print (f"Database saved successfully." )
4438
4539
46- # Function to ensure all entries have all required fields
4740def normalize_entry (entry ):
4841 required_fields = ["INVITE_URL" , "FOUND_ON" , "SERVER_ID" , "REASON" ]
4942 for field in required_fields :
5043 if field not in entry :
5144 entry [field ] = "UNKNOWN"
45+ for field in ["SERVER_STATUS" , "SERVER_STATUS_CHANGE" , "INVITE_STATUS" , "INVITE_STATUS_CHANGE" ]:
46+ entry .setdefault (field , "UNKNOWN" )
5247 return entry
5348
5449
55- # Function to convert old format to new format
50+ def normalize_invite_url (url ):
51+ try :
52+ parts = url .split ("/" )
53+ for i , part in enumerate (parts ):
54+ if part == "invite" and i + 1 < len (parts ):
55+ return f"https://discord.com/invite/{ parts [i + 1 ]} "
56+ return url
57+ except :
58+ return url
59+
60+
61+ def extract_invite_code (url ):
62+ try :
63+ url = normalize_invite_url (url )
64+ parts = url .lower ().split ("/" )
65+ return parts [- 1 ] if parts [- 1 ] else ""
66+ except :
67+ return ""
68+
69+
5670def convert_database_format (old_database ):
5771 print ("Converting database to new format..." )
5872 new_database = {}
5973 count = 1
60- processed_urls = set ()
74+ processed_codes = set ()
6175
62- for url , data in old_database .items ():
63- normalized_url = url .lower ()
64- if normalized_url in processed_urls :
76+ for _ , data in old_database .items ():
77+ url = normalize_invite_url (data .get ("INVITE_URL" , "" ))
78+ code = extract_invite_code (url )
79+ if code in processed_codes :
6580 continue
6681
6782 new_entry = {
@@ -76,36 +91,13 @@ def convert_database_format(old_database):
7691 }
7792
7893 new_database [f"DISCORD_SERVER_{ count } " ] = normalize_entry (new_entry )
79- processed_urls .add (normalized_url )
94+ processed_codes .add (code )
8095 count += 1
8196
8297 print (f"Converted { count - 1 } entries to new format." )
8398 return new_database
8499
85100
86- # Function to extract invite code from URL
87- def extract_invite_code (url ):
88- try :
89- parts = url .lower ().split ("/" )
90- return parts [- 1 ] if parts [- 1 ] else ""
91- except :
92- return ""
93-
94-
95- # Function to check if invite code already exists in database
96- def invite_code_exists_in_database (invite_code , database ):
97- if not invite_code :
98- return False
99- invite_code = invite_code .lower ()
100- for entry_data in database .values ():
101- url = entry_data .get ("INVITE_URL" , "" ).lower ()
102- existing_code = extract_invite_code (url )
103- if existing_code == invite_code :
104- return True
105- return False
106-
107-
108- # Function to renumber all database entries sequentially
109101def renumber_database (database ):
110102 print ("Renumbering database entries sequentially..." )
111103 new_database = {}
@@ -117,27 +109,31 @@ def renumber_database(database):
117109 return new_database
118110
119111
120- # Main function to fetch and process data
121112def update_discord_servers_database ():
122113 start_time = time .time ()
123114 print ("Starting Discord server database update..." )
124115
125116 api_url = "https://api.phish.gg/servers/all"
126117 db_file_path = "../Database-Files/Filter-Database/Discord-Servers.json"
118+ compromised_db_path = "../Database-Files/Main-Database/Compromised-Discord-Accounts.json"
127119
128120 database = load_database (db_file_path )
129121
130- # Check if old format
131122 is_old_format = any (key .startswith ("http" ) for key in database .keys ())
132123 if is_old_format :
133124 database = convert_database_format (database )
134125
135- # Normalize entries
136126 print ("Normalizing existing entries..." )
137127 for key , entry in database .items ():
128+ entry ["INVITE_URL" ] = normalize_invite_url (entry .get ("INVITE_URL" , "" ))
138129 database [key ] = normalize_entry (entry )
139130
140- # Fetch from API
131+ existing_invite_codes = {
132+ extract_invite_code (entry .get ("INVITE_URL" , "" ))
133+ for entry in database .values ()
134+ }
135+
136+ # Fetch data from API
141137 print (f"Fetching data from { api_url } ..." )
142138 try :
143139 response = requests .get (api_url )
@@ -146,62 +142,56 @@ def update_discord_servers_database():
146142 print (f"Successfully fetched data: { len (servers )} servers found." )
147143 except requests .RequestException as e :
148144 print (f"Error fetching data from API: { e } " )
149- return
145+ servers = []
150146
151- new_entries_data = []
152- print ("Processing server data..." )
147+ # Load compromised accounts (even if unused now)
148+ try :
149+ with open (compromised_db_path , 'r' ) as file :
150+ compromised_accounts = json .load (file )
151+ except Exception as e :
152+ print (f"Error loading compromised accounts: { e } " )
153+ compromised_accounts = {}
154+
155+ # Process new servers
156+ new_entries_added = 0
153157 for server in servers :
154158 server_id = server .get ("serverID" , "UNKNOWN" )
155- invite_code = server .get ("invite" , "" )
159+ raw_invite = server .get ("invite" , "" )
156160 reason = server .get ("reason" , "UNKNOWN" )
157161
158- if not invite_code :
162+ if not raw_invite :
159163 continue
160164
161- if invite_code_exists_in_database (invite_code , database ):
165+ normalized_url = normalize_invite_url (f"https://discord.com/invite/{ raw_invite } " )
166+ invite_code = extract_invite_code (normalized_url )
167+
168+ if invite_code .lower () in existing_invite_codes :
162169 continue
163170
164- found_on = snowflake_to_timestamp (server_id ) if server_id != "UNKNOWN" else int (datetime .datetime .now ().timestamp ())
165- url = f"https://discord.com/invite/{ invite_code } "
171+ found_on = snowflake_to_timestamp (server_id )
166172
167- new_entries_data . append ( {
168- "INVITE_URL" : url ,
173+ new_entry = {
174+ "INVITE_URL" : normalized_url ,
169175 "FOUND_ON" : found_on ,
170176 "SERVER_ID" : server_id ,
171177 "REASON" : reason ,
172178 "SERVER_STATUS" : "UNKNOWN" ,
173179 "SERVER_STATUS_CHANGE" : "UNKNOWN" ,
174180 "INVITE_STATUS" : "UNKNOWN" ,
175181 "INVITE_STATUS_CHANGE" : "UNKNOWN"
176- })
177-
178- # Track existing invite codes
179- existing_invite_codes = {
180- extract_invite_code (entry .get ("INVITE_URL" , "" ))
181- for entry in database .values ()
182- }
183-
184- for entry_data in new_entries_data :
185- invite_code = extract_invite_code (entry_data ["INVITE_URL" ])
186- if invite_code in existing_invite_codes :
187- continue
182+ }
188183
189- highest_num = max (
190- [int (k .split ("_" )[- 1 ]) for k in database if k .startswith ("DISCORD_SERVER_" )],
191- default = 0
192- )
193- new_key = f"DISCORD_SERVER_{ highest_num + 1 } "
194- database [new_key ] = entry_data
195- existing_invite_codes .add (invite_code )
184+ database [f"DISCORD_SERVER_{ len (database ) + 1 } " ] = normalize_entry (new_entry )
185+ existing_invite_codes .add (invite_code .lower ())
186+ new_entries_added += 1
196187
188+ print (f"New entries added: { new_entries_added } " )
197189 database = renumber_database (database )
198190 save_database (db_file_path , database )
199191
200- end_time = time .time ()
201- print (f"Database update completed in { end_time - start_time :.2f} seconds." )
202- print (f"Added { len (new_entries_data )} new entries." )
203- print (f"Total entries in database: { len (database )} " )
192+ elapsed_time = time .time () - start_time
193+ print (f"Update completed in { elapsed_time :.2f} seconds." )
204194
205195
206196if __name__ == "__main__" :
207- update_discord_servers_database ()
197+ update_discord_servers_database ()
0 commit comments