zumat commited on
Commit
a269bb3
·
verified ·
1 Parent(s): d606f94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -5
app.py CHANGED
@@ -174,25 +174,32 @@ def dataManipulator(converter, filepath):
174
 
175
  return gr.DownloadButton(label=f"Download {output_file}", value=output_file, interactive=True)
176
  if converter == "WhatsApp Converter":
177
- with open(filepath, 'r', errors='ignore') as file:
178
  lines = file.readlines()
179
 
180
  records = []
 
 
181
  for line in lines:
182
- if ', ' in line and ': ' in line:
183
- date_time, message = line.split(' - ', 1)
 
 
184
  date, time = date_time.split(', ')
185
- sender, message = message.split(': ', 1)
186
  records.append({
187
  "date": date,
188
  "time": time,
189
  "sender": sender,
190
  "message": message
191
  })
 
 
 
 
192
 
193
  output_file = os.path.basename(os.path.splitext(filepath)[0]) + '_converted.csv'
194
 
195
- with open(output_file, 'w', newline='') as csvfile:
196
  fieldnames = ["date", "time", "sender", "message"]
197
  writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
198
 
 
174
 
175
  return gr.DownloadButton(label=f"Download {output_file}", value=output_file, interactive=True)
176
  if converter == "WhatsApp Converter":
177
+ with open(filepath, 'r', encoding='utf-8', errors='ignore') as file:
178
  lines = file.readlines()
179
 
180
  records = []
181
+ pattern = r'^\[?(\d{2}/\d{2}/\d{2}, \d{2}:\d{2}:\d{2})\]?\s-?\s(.+?):\s(.+)$'
182
+
183
  for line in lines:
184
+ line = line.strip()
185
+ match = re.match(pattern, line)
186
+ if match:
187
+ date_time, sender, message = match.groups()
188
  date, time = date_time.split(', ')
 
189
  records.append({
190
  "date": date,
191
  "time": time,
192
  "sender": sender,
193
  "message": message
194
  })
195
+ else:
196
+ # If the line doesn't match the pattern, it might be a continuation of the previous message
197
+ if records:
198
+ records[-1]["message"] += " " + line
199
 
200
  output_file = os.path.basename(os.path.splitext(filepath)[0]) + '_converted.csv'
201
 
202
+ with open(output_file, 'w', newline='', encoding='utf-8') as csvfile:
203
  fieldnames = ["date", "time", "sender", "message"]
204
  writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
205