11from InquirerPy import inquirer
2+ import requests
3+ import json
4+ import hashlib
5+ import os
26
37from utils .get_translation import get_translation
48from spice .analyze import analyze_file
59
10+ # Add this at the top - server configuration
11+ SERVER_URL = "http://localhost:3000/api/submit"
12+
13+ def get_file_hash (file_path ):
14+ """Generate a hash for the file based on content and modification time"""
15+ try :
16+ with open (file_path , 'rb' ) as f :
17+ file_content = f .read ()
18+
19+ # Include file modification time to detect changes
20+ mod_time = os .path .getmtime (file_path )
21+ hash_input = file_content + str (mod_time ).encode ()
22+
23+ return hashlib .md5 (hash_input ).hexdigest ()
24+ except Exception as e :
25+ print (f"Error generating hash: { e } " )
26+ return None
27+
28+ def send_to_server (data ):
29+ """Send analysis data to the server"""
30+ try :
31+ response = requests .post (SERVER_URL , json = data , timeout = 5 )
32+
33+ if response .status_code == 200 :
34+ result = response .json ()
35+ if result .get ('isDuplicate' ):
36+ print ("✓ Data updated on server (file was modified)" )
37+ else :
38+ print ("✓ New analysis sent to server" )
39+ return True
40+ else :
41+ print (f"⚠ Server responded with status { response .status_code } " )
42+ return False
43+
44+ except requests .exceptions .ConnectionError :
45+ print ("⚠ Could not connect to server - make sure it's running on localhost:3000" )
46+ return False
47+ except requests .exceptions .Timeout :
48+ print ("⚠ Server request timed out" )
49+ return False
50+ except Exception as e :
51+ print (f"⚠ Error sending to server: { e } " )
52+ return False
653
754def analyze_command (file , all , json_output , LANG_FILE ):
855 """
@@ -79,6 +126,18 @@ def analyze_command(file, all, json_output, LANG_FILE):
79126 # get analysis results from analyze_file
80127 results = analyze_file (file , selected_stats = selected_stat_keys )
81128
129+ # Prepare data for server
130+ server_data = {
131+ "file_name" : results .get ("file_name" ),
132+ "file_path" : results .get ("file_path" ),
133+ "file_size" : results .get ("file_size" ),
134+ "file_extension" : results .get ("file_extension" ),
135+ ** {k : v for k , v in results .items () if k not in ["file_name" , "file_path" , "file_size" , "file_extension" ]}
136+ }
137+
138+ # Send to server (always attempt this, regardless of output mode)
139+ send_to_server (server_data )
140+
82141 # output in JSON format if flag
83142 if json_output :
84143 import json
@@ -107,4 +166,4 @@ def analyze_command(file, all, json_output, LANG_FILE):
107166 error_msg = str (e ).replace ('\n ' , ' ' )
108167 print (json .dumps ({"error" : error_msg }))
109168 else :
110- print (f"{ messages .get ('error' , 'Error' )} : { e } " )
169+ print (f"{ messages .get ('error' , 'Error' )} : { e } " )
0 commit comments