Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -77,21 +77,45 @@ def parse_google_translate_html(filepath):
|
|
| 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 |
-
|
| 85 |
-
date_str = date_match.
|
| 86 |
try:
|
| 87 |
-
date = datetime.strptime(date_str, '%d %b %Y
|
| 88 |
-
formatted_date = date.strftime('%
|
|
|
|
| 89 |
except ValueError:
|
| 90 |
-
formatted_date = date_str
|
|
|
|
| 91 |
|
| 92 |
translations.append({
|
| 93 |
-
'
|
| 94 |
-
'
|
|
|
|
|
|
|
|
|
|
| 95 |
})
|
| 96 |
|
| 97 |
if not translations:
|
|
@@ -114,7 +138,7 @@ def google_translate_to_csv(filepath):
|
|
| 114 |
|
| 115 |
try:
|
| 116 |
with open(output_file, 'w', newline='', encoding='utf-8') as csvfile:
|
| 117 |
-
fieldnames = ['
|
| 118 |
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
|
| 119 |
|
| 120 |
writer.writeheader()
|
|
|
|
| 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 |
+
link = content.find('a')
|
| 81 |
+
if link:
|
| 82 |
+
url = link.get('href', '')
|
| 83 |
+
query_params = url.split('&')
|
| 84 |
+
input_lang = output_lang = input_text = ''
|
| 85 |
+
for param in query_params:
|
| 86 |
+
if param.startswith('sl='):
|
| 87 |
+
input_lang = param.split('=')[1]
|
| 88 |
+
elif param.startswith('tl='):
|
| 89 |
+
output_lang = param.split('=')[1]
|
| 90 |
+
elif param.startswith('q='):
|
| 91 |
+
input_text = param.split('=')[1]
|
| 92 |
+
|
| 93 |
+
input_text = input_text.replace('+', ' ')
|
| 94 |
+
language = f"{input_lang} -> {output_lang}"
|
| 95 |
+
else:
|
| 96 |
+
input_text = ''
|
| 97 |
+
language = ''
|
| 98 |
+
|
| 99 |
translation_match = re.search(r'Hai tradotto\s+(?:<a.*?>)?"(.*?)"', str(content))
|
| 100 |
+
date_match = re.search(r'(\d{1,2}\s+\w{3}\s+\d{4}),\s+(\d{2}:\d{2}:\d{2}\s+\w+)', text)
|
| 101 |
|
| 102 |
if translation_match and date_match:
|
| 103 |
+
output_text = translation_match.group(1)
|
| 104 |
+
date_str, time_str = date_match.groups()
|
| 105 |
try:
|
| 106 |
+
date = datetime.strptime(f"{date_str} {time_str}", '%d %b %Y %H:%M:%S %Z')
|
| 107 |
+
formatted_date = date.strftime('%d/%m/%Y')
|
| 108 |
+
formatted_time = date.strftime('%H:%M:%S')
|
| 109 |
except ValueError:
|
| 110 |
+
formatted_date = date_str
|
| 111 |
+
formatted_time = time_str
|
| 112 |
|
| 113 |
translations.append({
|
| 114 |
+
'Date': formatted_date,
|
| 115 |
+
'Time': formatted_time,
|
| 116 |
+
'Requested translation': input_text,
|
| 117 |
+
'Output': output_text,
|
| 118 |
+
'Language': language
|
| 119 |
})
|
| 120 |
|
| 121 |
if not translations:
|
|
|
|
| 138 |
|
| 139 |
try:
|
| 140 |
with open(output_file, 'w', newline='', encoding='utf-8') as csvfile:
|
| 141 |
+
fieldnames = ['Date', 'Time', 'Requested translation', 'Output', 'Language']
|
| 142 |
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
|
| 143 |
|
| 144 |
writer.writeheader()
|