zumat commited on
Commit
475feaf
·
verified ·
1 Parent(s): 7df0482

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -44
app.py CHANGED
@@ -65,6 +65,28 @@ def parse_email(raw_email):
65
 
66
  return email_details
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  def dataManipulator(converter, filepath):
69
  global output_file
70
  output_file = filepath
@@ -193,42 +215,7 @@ def dataManipulator(converter, filepath):
193
  return output_file
194
 
195
  if converter == "WhatsApp Converter":
196
- with open(filepath, 'r', errors='ignore') as file:
197
- lines = file.readlines()
198
-
199
- records = []
200
- for line in lines:
201
- try:
202
- if ', ' in line and ' - ' in line and ': ' in line:
203
- date_time, message = line.split(' - ', 1)
204
- date, time = date_time.split(', ')
205
- sender, message = message.split(': ', 1)
206
- records.append({
207
- "date": date,
208
- "time": time,
209
- "sender": sender,
210
- "message": message
211
- })
212
- else:
213
- print(f"Line doesn't match expected format: {line}")
214
- except ValueError as e:
215
- print(f"Skipping line due to error: {line} - {str(e)}")
216
- continue
217
-
218
- print("WhatsApp records parsed:", records)
219
-
220
- output_file = os.path.basename(os.path.splitext(filepath)[0]) + '_converted.csv'
221
-
222
- columns = ["date", "time", "sender", "message"]
223
-
224
- with open(output_file, 'w', newline='') as csvfile:
225
- writer = csv.DictWriter(csvfile, fieldnames=columns)
226
-
227
- writer.writeheader()
228
- for record in records:
229
- writer.writerow(record)
230
-
231
- print(f"File {output_file} written with {len(records)} records")
232
  return output_file
233
 
234
  if converter == "Google Foto Converter":
@@ -262,15 +249,15 @@ def dataManipulator(converter, filepath):
262
 
263
  return "Converter type not recognized."
264
 
265
- def process_input(converter, input_file):
266
- if input_file is not None:
267
- result = dataManipulator(converter, input_file.name)
268
- if result.startswith("Error:"):
269
- return None, result
270
  else:
271
- return result, None
272
- else:
273
- return None, "No file uploaded."
274
 
275
  with gr.Blocks() as demo:
276
  converter = gr.Dropdown(["Email Converter", "Calendar Converter", "AppleHealth Converter", "AppleHealth GPS Converter", "WhatsApp Converter", "Google Foto Converter"], label="Select Converter")
 
65
 
66
  return email_details
67
 
68
+ def parse_whatsapp_chat(input_file_path, output_file_path):
69
+ with open(input_file_path, 'r', encoding='utf-8') as input_file:
70
+ lines = input_file.readlines()
71
+
72
+ messages = []
73
+ for line in lines:
74
+ try:
75
+ if line.startswith('['):
76
+ date_time, sender_message = line[1:].split('] ', 1)
77
+ date, time = date_time.split(', ')
78
+ sender, message = sender_message.split(': ', 1)
79
+ messages.append([date, time, sender, message])
80
+ except ValueError:
81
+ print(f"Skipping line due to error: {line.strip()} - not enough values to unpack (expected 2, got 1)")
82
+
83
+ with open(output_file_path, 'w', newline='', encoding='utf-8') as output_file:
84
+ writer = csv.writer(output_file)
85
+ writer.writerow(['Date', 'Time', 'Sender', 'Message'])
86
+ writer.writerows(messages)
87
+
88
+ print(f"File {output_file_path} written with {len(messages)} records")
89
+
90
  def dataManipulator(converter, filepath):
91
  global output_file
92
  output_file = filepath
 
215
  return output_file
216
 
217
  if converter == "WhatsApp Converter":
218
+ parse_whatsapp_chat(input_file_path, output_file)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
219
  return output_file
220
 
221
  if converter == "Google Foto Converter":
 
249
 
250
  return "Converter type not recognized."
251
 
252
+ def process_input(input_file):
253
+ try:
254
+ if input_file is not None:
255
+ output_file = dataManipulator(None, input_file.name)
256
+ return gr.File.update(value=output_file, label="Download converted file")
257
  else:
258
+ return gr.Textbox.update(value="No file uploaded.")
259
+ except Exception as e:
260
+ return gr.Textbox.update(value=f"Error processing WhatsApp file: {str(e)}")
261
 
262
  with gr.Blocks() as demo:
263
  converter = gr.Dropdown(["Email Converter", "Calendar Converter", "AppleHealth Converter", "AppleHealth GPS Converter", "WhatsApp Converter", "Google Foto Converter"], label="Select Converter")