zumat commited on
Commit
b743037
·
verified ·
1 Parent(s): 96830b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -40
app.py CHANGED
@@ -24,10 +24,8 @@ def decode_email_content(content, content_transfer_encoding):
24
  return content
25
 
26
  def parse_email(raw_email):
27
- # Parsing the raw email content
28
  msg = BytesParser(policy=policy.default).parsebytes(raw_email)
29
 
30
- # Extracting email details
31
  email_details = {
32
  "From": msg["From"],
33
  "To": msg["To"],
@@ -36,7 +34,6 @@ def parse_email(raw_email):
36
  "Body": ""
37
  }
38
 
39
- # Extracting the email body content
40
  if msg.is_multipart():
41
  for part in msg.iter_parts():
42
  content_type = part.get_content_type()
@@ -69,18 +66,18 @@ def dataManipulator(converter, filepath):
69
 
70
  if converter == "Email Converter":
71
  records = []
72
-
73
  with open(filepath, 'rb') as file:
74
  raw_email_data = file.read()
75
-
76
  email_entries = raw_email_data.split(b'\nFrom ')
77
 
78
  for raw_email in email_entries:
79
  if not raw_email.startswith(b'From '):
80
  raw_email = b'From ' + raw_email
81
-
82
  parsed_email = parse_email(raw_email)
83
-
84
  record_data = {
85
  "date and time": parsed_email["Date"],
86
  "from": parsed_email["From"],
@@ -89,22 +86,22 @@ def dataManipulator(converter, filepath):
89
  "Subject": parsed_email["Subject"],
90
  "Content": parsed_email["Body"]
91
  }
92
-
93
  records.append(record_data)
94
 
95
  output_file = os.path.basename(os.path.splitext(filepath)[0]) + '_converted.csv'
96
-
97
  columns = ["date and time", "from", "to", "Cc", "Subject", "Content"]
98
-
99
  with open(output_file, 'w', newline='') as csvfile:
100
  writer = csv.DictWriter(csvfile, fieldnames=columns)
101
-
102
  writer.writeheader()
103
  for record in records:
104
  writer.writerow(record)
105
-
106
- return gr.DownloadButton(label=f"Download {output_file}", value=output_file, interactive=True)
107
-
108
  if converter == "Calendar Converter":
109
  try:
110
  with open(filepath, 'rb') as file:
@@ -122,16 +119,16 @@ def dataManipulator(converter, filepath):
122
  })
123
 
124
  output_file = os.path.basename(os.path.splitext(filepath)[0]) + '_converted.csv'
125
-
126
  with open(output_file, 'w', newline='') as csvfile:
127
  fieldnames = ["summary", "dtstart", "dtend", "description", "location"]
128
  writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
129
-
130
  writer.writeheader()
131
  for record in records:
132
  writer.writerow(record)
133
-
134
- return gr.DownloadButton(label=f"Download {output_file}", value=output_file, interactive=True)
135
  except Exception as e:
136
  return gr.Textbox(value=f"Error processing calendar file: {str(e)}", interactive=False)
137
 
@@ -140,23 +137,23 @@ def dataManipulator(converter, filepath):
140
  tree = ET.parse(filepath)
141
  root = tree.getroot()
142
  records = []
143
-
144
  columns = ["startDate", "endDate", "device", "sourceName", "type", "unit", "value"]
145
-
146
  for record in root.findall('Record'):
147
  record_data = {key: record.attrib.get(key, '') for key in columns}
148
  records.append(record_data)
149
-
150
  output_file = os.path.basename(os.path.splitext(filepath)[0]) + '_converted.csv'
151
-
152
  with open(output_file, 'w', newline='') as csvfile:
153
  writer = csv.DictWriter(csvfile, fieldnames=columns)
154
-
155
  writer.writeheader()
156
  for record in records:
157
  writer.writerow(record)
158
-
159
- return gr.DownloadButton(label=f"Download {output_file}", value=output_file, interactive=True)
160
  except Exception as e:
161
  return gr.Textbox(value=f"Error processing AppleHealth file: {str(e)}", interactive=False)
162
 
@@ -177,16 +174,16 @@ def dataManipulator(converter, filepath):
177
  })
178
 
179
  output_file = os.path.basename(os.path.splitext(filepath.name)[0]) + '_converted.csv'
180
-
181
  with open(output_file, 'w', newline='') as csvfile:
182
  fieldnames = records[0].keys() if records else []
183
  writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
184
-
185
  writer.writeheader()
186
  for record in records:
187
  writer.writerow(record)
188
-
189
- return gr.DownloadButton(label=f"Download {output_file}", value=output_file, interactive=True)
190
  except Exception as e:
191
  return gr.Textbox(value=f"Error processing GPS file: {str(e)}", interactive=False)
192
 
@@ -209,27 +206,25 @@ def dataManipulator(converter, filepath):
209
  })
210
 
211
  output_file = os.path.basename(os.path.splitext(filepath)[0]) + '_converted.csv'
212
-
213
  with open(output_file, 'w', newline='') as csvfile:
214
  fieldnames = ["date", "time", "sender", "message"]
215
  writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
216
-
217
  writer.writeheader()
218
  for record in records:
219
  writer.writerow(record)
220
-
221
- return gr.DownloadButton(label=f"Download {output_file}", value=output_file, interactive=True)
222
  except Exception as e:
223
  return gr.Textbox(value=f"Error processing WhatsApp file: {str(e)}", interactive=False)
224
 
225
  if converter == "Google Foto Converter":
226
  try:
227
  records = []
228
- # Unzip the file
229
  with zipfile.ZipFile(filepath, 'r') as zip_ref:
230
  zip_ref.extractall('json_folder')
231
 
232
- # Process the extracted files
233
  for filename in os.listdir('json_folder'):
234
  if filename.endswith('.json'):
235
  with open(os.path.join('json_folder', filename), 'r') as json_file:
@@ -237,25 +232,28 @@ def dataManipulator(converter, filepath):
237
  records.append(data)
238
 
239
  output_file = 'googleFotoConverted.csv'
240
-
241
  with open(output_file, 'w', newline='') as csvfile:
242
  fieldnames = set()
243
  for record in records:
244
  fieldnames.update(record.keys())
245
  writer = csv.DictWriter(csvfile, fieldnames=list(fieldnames))
246
-
247
  writer.writeheader()
248
  for record in records:
249
  writer.writerow(record)
250
-
251
- return gr.DownloadButton(label=f"Download {output_file}", value=output_file, interactive=True)
252
  except Exception as e:
253
  return gr.Textbox(value=f"Error processing Google Foto file: {str(e)}", interactive=False)
254
 
255
  return gr.Textbox(value="Converter type not recognized.", interactive=False)
256
 
257
  def process_input(converter, input_file):
258
- return dataManipulator(converter, input_file.name)
 
 
 
259
 
260
  with gr.Blocks() as demo:
261
  converter = gr.Dropdown(["Email Converter", "Calendar Converter", "AppleHealth Converter", "AppleHealth GPS Converter", "WhatsApp Converter", "Google Foto Converter"], label="Select Converter")
 
24
  return content
25
 
26
  def parse_email(raw_email):
 
27
  msg = BytesParser(policy=policy.default).parsebytes(raw_email)
28
 
 
29
  email_details = {
30
  "From": msg["From"],
31
  "To": msg["To"],
 
34
  "Body": ""
35
  }
36
 
 
37
  if msg.is_multipart():
38
  for part in msg.iter_parts():
39
  content_type = part.get_content_type()
 
66
 
67
  if converter == "Email Converter":
68
  records = []
69
+
70
  with open(filepath, 'rb') as file:
71
  raw_email_data = file.read()
72
+
73
  email_entries = raw_email_data.split(b'\nFrom ')
74
 
75
  for raw_email in email_entries:
76
  if not raw_email.startswith(b'From '):
77
  raw_email = b'From ' + raw_email
78
+
79
  parsed_email = parse_email(raw_email)
80
+
81
  record_data = {
82
  "date and time": parsed_email["Date"],
83
  "from": parsed_email["From"],
 
86
  "Subject": parsed_email["Subject"],
87
  "Content": parsed_email["Body"]
88
  }
89
+
90
  records.append(record_data)
91
 
92
  output_file = os.path.basename(os.path.splitext(filepath)[0]) + '_converted.csv'
93
+
94
  columns = ["date and time", "from", "to", "Cc", "Subject", "Content"]
95
+
96
  with open(output_file, 'w', newline='') as csvfile:
97
  writer = csv.DictWriter(csvfile, fieldnames=columns)
98
+
99
  writer.writeheader()
100
  for record in records:
101
  writer.writerow(record)
102
+
103
+ return gr.File(output_file, label="Download converted file")
104
+
105
  if converter == "Calendar Converter":
106
  try:
107
  with open(filepath, 'rb') as file:
 
119
  })
120
 
121
  output_file = os.path.basename(os.path.splitext(filepath)[0]) + '_converted.csv'
122
+
123
  with open(output_file, 'w', newline='') as csvfile:
124
  fieldnames = ["summary", "dtstart", "dtend", "description", "location"]
125
  writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
126
+
127
  writer.writeheader()
128
  for record in records:
129
  writer.writerow(record)
130
+
131
+ return gr.File(output_file, label="Download converted file")
132
  except Exception as e:
133
  return gr.Textbox(value=f"Error processing calendar file: {str(e)}", interactive=False)
134
 
 
137
  tree = ET.parse(filepath)
138
  root = tree.getroot()
139
  records = []
140
+
141
  columns = ["startDate", "endDate", "device", "sourceName", "type", "unit", "value"]
142
+
143
  for record in root.findall('Record'):
144
  record_data = {key: record.attrib.get(key, '') for key in columns}
145
  records.append(record_data)
146
+
147
  output_file = os.path.basename(os.path.splitext(filepath)[0]) + '_converted.csv'
148
+
149
  with open(output_file, 'w', newline='') as csvfile:
150
  writer = csv.DictWriter(csvfile, fieldnames=columns)
151
+
152
  writer.writeheader()
153
  for record in records:
154
  writer.writerow(record)
155
+
156
+ return gr.File(output_file, label="Download converted file")
157
  except Exception as e:
158
  return gr.Textbox(value=f"Error processing AppleHealth file: {str(e)}", interactive=False)
159
 
 
174
  })
175
 
176
  output_file = os.path.basename(os.path.splitext(filepath.name)[0]) + '_converted.csv'
177
+
178
  with open(output_file, 'w', newline='') as csvfile:
179
  fieldnames = records[0].keys() if records else []
180
  writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
181
+
182
  writer.writeheader()
183
  for record in records:
184
  writer.writerow(record)
185
+
186
+ return gr.File(output_file, label="Download converted file")
187
  except Exception as e:
188
  return gr.Textbox(value=f"Error processing GPS file: {str(e)}", interactive=False)
189
 
 
206
  })
207
 
208
  output_file = os.path.basename(os.path.splitext(filepath)[0]) + '_converted.csv'
209
+
210
  with open(output_file, 'w', newline='') as csvfile:
211
  fieldnames = ["date", "time", "sender", "message"]
212
  writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
213
+
214
  writer.writeheader()
215
  for record in records:
216
  writer.writerow(record)
217
+
218
+ return gr.File(output_file, label="Download converted file")
219
  except Exception as e:
220
  return gr.Textbox(value=f"Error processing WhatsApp file: {str(e)}", interactive=False)
221
 
222
  if converter == "Google Foto Converter":
223
  try:
224
  records = []
 
225
  with zipfile.ZipFile(filepath, 'r') as zip_ref:
226
  zip_ref.extractall('json_folder')
227
 
 
228
  for filename in os.listdir('json_folder'):
229
  if filename.endswith('.json'):
230
  with open(os.path.join('json_folder', filename), 'r') as json_file:
 
232
  records.append(data)
233
 
234
  output_file = 'googleFotoConverted.csv'
235
+
236
  with open(output_file, 'w', newline='') as csvfile:
237
  fieldnames = set()
238
  for record in records:
239
  fieldnames.update(record.keys())
240
  writer = csv.DictWriter(csvfile, fieldnames=list(fieldnames))
241
+
242
  writer.writeheader()
243
  for record in records:
244
  writer.writerow(record)
245
+
246
+ return gr.File(output_file, label="Download converted file")
247
  except Exception as e:
248
  return gr.Textbox(value=f"Error processing Google Foto file: {str(e)}", interactive=False)
249
 
250
  return gr.Textbox(value="Converter type not recognized.", interactive=False)
251
 
252
  def process_input(converter, input_file):
253
+ if input_file is not None:
254
+ return dataManipulator(converter, input_file.name)
255
+ else:
256
+ return gr.Textbox(value="No file uploaded.", interactive=False)
257
 
258
  with gr.Blocks() as demo:
259
  converter = gr.Dropdown(["Email Converter", "Calendar Converter", "AppleHealth Converter", "AppleHealth GPS Converter", "WhatsApp Converter", "Google Foto Converter"], label="Select Converter")