Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,6 +13,8 @@ from icalendar import Calendar
|
|
| 13 |
import zipfile
|
| 14 |
import re
|
| 15 |
import traceback
|
|
|
|
|
|
|
| 16 |
|
| 17 |
def decode_email_content(content, content_transfer_encoding):
|
| 18 |
if content_transfer_encoding == 'base64':
|
|
@@ -61,6 +63,47 @@ def parse_email(raw_email):
|
|
| 61 |
|
| 62 |
return email_details
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
def dataManipulator(converter, filepath):
|
| 65 |
global output_file
|
| 66 |
output_file = filepath
|
|
@@ -247,6 +290,9 @@ def dataManipulator(converter, filepath):
|
|
| 247 |
for record in records:
|
| 248 |
writer.writerow(record)
|
| 249 |
|
|
|
|
|
|
|
|
|
|
| 250 |
return gr.DownloadButton(label=f"Download {output_file}", value=output_file, interactive=True), ""
|
| 251 |
else:
|
| 252 |
return gr.DownloadButton(label="Download not ready", value="output_file", interactive=False), "You need to select a converter before"
|
|
|
|
| 13 |
import zipfile
|
| 14 |
import re
|
| 15 |
import traceback
|
| 16 |
+
from datetime import datetime
|
| 17 |
+
from bs4 import BeautifulSoup
|
| 18 |
|
| 19 |
def decode_email_content(content, content_transfer_encoding):
|
| 20 |
if content_transfer_encoding == 'base64':
|
|
|
|
| 63 |
|
| 64 |
return email_details
|
| 65 |
|
| 66 |
+
def parse_google_translate_html(filepath):
|
| 67 |
+
with open(filepath, 'r', encoding='utf-8') as file:
|
| 68 |
+
soup = BeautifulSoup(file, 'html.parser')
|
| 69 |
+
|
| 70 |
+
translations = []
|
| 71 |
+
for div in soup.find_all('div', class_='outer-cell'):
|
| 72 |
+
title = div.find('p', class_='mdl-typography--title').text.strip()
|
| 73 |
+
if title == "Google Traduttore":
|
| 74 |
+
content = div.find('div', class_='content-cell mdl-cell mdl-cell--6-col mdl-typography--body-1')
|
| 75 |
+
if content:
|
| 76 |
+
text = content.text.strip()
|
| 77 |
+
translation_match = re.search(r'Hai tradotto "(.*?)"', text)
|
| 78 |
+
date_match = re.search(r'(\d{1,2} \w{3} \d{4}, \d{2}:\d{2}:\d{2} \w+)', text)
|
| 79 |
+
|
| 80 |
+
if translation_match and date_match:
|
| 81 |
+
translation = translation_match.group(1)
|
| 82 |
+
date_str = date_match.group(1)
|
| 83 |
+
date = datetime.strptime(date_str, '%d %b %Y, %H:%M:%S %Z')
|
| 84 |
+
|
| 85 |
+
translations.append({
|
| 86 |
+
'date': date.strftime('%Y-%m-%d %H:%M:%S'),
|
| 87 |
+
'translation': translation
|
| 88 |
+
})
|
| 89 |
+
|
| 90 |
+
return translations
|
| 91 |
+
|
| 92 |
+
def google_translate_to_csv(filepath):
|
| 93 |
+
translations = parse_google_translate_html(filepath)
|
| 94 |
+
|
| 95 |
+
output_file = os.path.basename(os.path.splitext(filepath)[0]) + '_converted.csv'
|
| 96 |
+
|
| 97 |
+
with open(output_file, 'w', newline='', encoding='utf-8') as csvfile:
|
| 98 |
+
fieldnames = ['date', 'translation']
|
| 99 |
+
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
|
| 100 |
+
|
| 101 |
+
writer.writeheader()
|
| 102 |
+
for translation in translations:
|
| 103 |
+
writer.writerow(translation)
|
| 104 |
+
|
| 105 |
+
return output_file
|
| 106 |
+
|
| 107 |
def dataManipulator(converter, filepath):
|
| 108 |
global output_file
|
| 109 |
output_file = filepath
|
|
|
|
| 290 |
for record in records:
|
| 291 |
writer.writerow(record)
|
| 292 |
|
| 293 |
+
return gr.DownloadButton(label=f"Download {output_file}", value=output_file, interactive=True), ""
|
| 294 |
+
if converter == "Google Translate Converter":
|
| 295 |
+
output_file = google_translate_to_csv(filepath)
|
| 296 |
return gr.DownloadButton(label=f"Download {output_file}", value=output_file, interactive=True), ""
|
| 297 |
else:
|
| 298 |
return gr.DownloadButton(label="Download not ready", value="output_file", interactive=False), "You need to select a converter before"
|