Spaces:
Runtime error

zumat commited on
Commit
fe4dd7f
·
verified ·
1 Parent(s): f0d9ad9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -29
app.py CHANGED
@@ -64,45 +64,69 @@ def parse_email(raw_email):
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
 
64
  return email_details
65
 
66
  def parse_google_translate_html(filepath):
67
+ try:
68
+ with open(filepath, 'r', encoding='utf-8') as file:
69
+ content = file.read()
70
+
71
+ soup = BeautifulSoup(content, 'html.parser')
72
 
73
+ translations = []
74
+ for div in soup.find_all('div', class_='outer-cell mdl-cell mdl-cell--12-col mdl-shadow--2dp'):
75
+ title = div.find('p', class_='mdl-typography--title')
76
+ if title and "Google Traduttore" in title.text:
77
+ content = div.find('div', class_='content-cell mdl-cell mdl-cell--6-col mdl-typography--body-1')
78
+ if content:
79
+ text = content.text.strip()
80
+ translation_match = re.search(r'Hai tradotto\s+(?:<a.*?>)?"(.*?)"', str(content))
81
+ date_match = re.search(r'(\d{1,2}\s+\w{3}\s+\d{4},\s+\d{2}:\d{2}:\d{2}\s+\w+)', text)
 
 
 
 
 
82
 
83
+ if translation_match and date_match:
84
+ translation = translation_match.group(1)
85
+ date_str = date_match.group(1)
86
+ try:
87
+ date = datetime.strptime(date_str, '%d %b %Y, %H:%M:%S %Z')
88
+ formatted_date = date.strftime('%Y-%m-%d %H:%M:%S')
89
+ except ValueError:
90
+ formatted_date = date_str # Use the original string if parsing fails
91
+
92
+ translations.append({
93
+ 'date': formatted_date,
94
+ 'translation': translation
95
+ })
96
+
97
+ if not translations:
98
+ print("No translations found in the file.")
99
+
100
+ return translations
101
 
102
+ except Exception as e:
103
+ print(f"Error parsing HTML file: {str(e)}")
104
+ return []
105
 
106
  def google_translate_to_csv(filepath):
107
  translations = parse_google_translate_html(filepath)
108
 
109
+ if not translations:
110
+ print("No translations to write to CSV.")
111
+ return None
112
+
113
  output_file = os.path.basename(os.path.splitext(filepath)[0]) + '_converted.csv'
114
 
115
+ try:
116
+ with open(output_file, 'w', newline='', encoding='utf-8') as csvfile:
117
+ fieldnames = ['date', 'translation']
118
+ writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
119
+
120
+ writer.writeheader()
121
+ for translation in translations:
122
+ writer.writerow(translation)
123
 
124
+ print(f"Successfully wrote {len(translations)} translations to {output_file}")
125
+ return output_file
126
+
127
+ except Exception as e:
128
+ print(f"Error writing to CSV file: {str(e)}")
129
+ return None
130
 
131
  def dataManipulator(converter, filepath):
132
  global output_file