Yaron Koresh commited on
Commit
0307843
·
verified ·
1 Parent(s): 6cdce4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -8
app.py CHANGED
@@ -43,22 +43,26 @@ def translate(text,lang):
43
  print(url)
44
  resp = requests.get(
45
  url = url,
46
- params = {'q': f'{lang}: {text}'},
47
  headers = {
48
  'User-Agent': random.choice(user_agents)
49
  }
50
  )
51
  content = resp.content
52
  html = fromstring(content)
53
- rslt = html.xpath('//pre[@aria-label="Translated text"]/span')
54
- print(rslt)
 
55
  translated = text
56
- try:
57
- t = rslt[0].text.strip()
58
- translated = t
 
 
 
59
  except:
60
- if( len(rslt)>=1 ):
61
- print(f'"{text}" is already in {lang}!')
62
  ret = re.sub(f'[{string.punctuation}]', '', re.sub('[\s+]', ' ', translated)).lower().strip()
63
  print(ret)
64
  return ret
 
43
  print(url)
44
  resp = requests.get(
45
  url = url,
46
+ params = {'q': f'{lang} translate {text}'},
47
  headers = {
48
  'User-Agent': random.choice(user_agents)
49
  }
50
  )
51
  content = resp.content
52
  html = fromstring(content)
53
+
54
+ #src = html.xpath('//pre[@data-placeholder="Enter text"]/textarea')[0].text.strip()
55
+
56
  translated = text
57
+
58
+ try:
59
+ trgt = html.xpath('//span[@class="target-language"]')[0].text.strip()
60
+ rslt = html.xpath('//pre[@aria-label="Translated text"]/span')[0].text.strip()
61
+ if trgt.lower() == lang.lower():
62
+ translated = rslt
63
  except:
64
+ print("Translation Error!")
65
+
66
  ret = re.sub(f'[{string.punctuation}]', '', re.sub('[\s+]', ' ', translated)).lower().strip()
67
  print(ret)
68
  return ret