-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslation.py
31 lines (26 loc) · 1.06 KB
/
translation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import requests
import uuid
from config import get_value_from_api
def translate_text(text, lang,city):
key =get_value_from_api('TRANSLATOR_KEY')
endpoint = get_value_from_api('TRANSLATOR_ENDPOINT')
location = "eastus"
path = '/translate'
constructed_url = endpoint + path
params = {
'api-version': '3.0',
'from': 'en',
'to': str(lang)
}
headers = {
'Ocp-Apim-Subscription-Key': key,
'Ocp-Apim-Subscription-Region': location,
'Content-type': 'application/json',
'X-ClientTraceId': str(uuid.uuid4())
}
body = [{
'text': f'The weather forecast for {city} is {text["weather"][0]["description"]} with a temperature of {text["main"]["temp"]} degree celsius and a humidity of {text["main"]["humidity"]} percent and a wind speed of {text["wind"]["speed"]} meter per second.'
}]
request = requests.post(constructed_url, params=params, headers=headers, json=body)
response = request.json()
print('Translated weather report:', response[0]['translations'][0]['text'])