|
7 | 7 | # You may need to install requests and uuid. |
8 | 8 | # Run: pip install requests uuid |
9 | 9 |
|
10 | | - |
11 | 10 | import os, requests, uuid, json |
12 | 11 |
|
13 | | -# Checks to see if the Translator Text subscription key is available |
14 | | -# as an environment variable. If you are setting your subscription key as a |
15 | | -# string, then comment these lines out. |
16 | | -if 'TRANSLATOR_TEXT_KEY' in os.environ: |
17 | | - subscriptionKey = os.environ['TRANSLATOR_TEXT_KEY'] |
18 | | -else: |
19 | | - print('Environment variable for TRANSLATOR_TEXT_KEY is not set.') |
20 | | - exit() |
21 | | -# If you want to set your subscription key as a string, uncomment the next line. |
22 | | -#subscriptionKey = 'put_your_key_here' |
| 12 | +key_var_name = 'TRANSLATOR_TEXT_SUBSCRIPTION_KEY' |
| 13 | +if not key_var_name in os.environ: |
| 14 | + raise Exception('Please set/export the environment variable: {}'.format(key_var_name)) |
| 15 | +subscriptionKey = os.environ[key_var_name] |
| 16 | + |
| 17 | +endpoint_var_name = 'TRANSLATOR_TEXT_ENDPOINT' |
| 18 | +if not endpoint_var_name in os.environ: |
| 19 | + raise Exception('Please set/export the environment variable: {}'.format(endpoint_var_name)) |
| 20 | +endpoint = os.environ[endpoint_var_name] |
23 | 21 |
|
24 | 22 | # If you encounter any issues with the base_url or path, make sure |
25 | 23 | # that you are using the latest endpoint: https://docs.microsoft.com/azure/cognitive-services/translator/reference/v3-0-break-sentence |
26 | | -base_url = 'https://api.cognitive.microsofttranslator.com' |
27 | 24 | path = '/breaksentence?api-version=3.0' |
28 | 25 | params = '&language=en' |
29 | | -constructed_url = base_url + path + params |
| 26 | +constructed_url = endpoint + path + params |
30 | 27 |
|
31 | 28 | headers = { |
32 | 29 | 'Ocp-Apim-Subscription-Key': subscriptionKey, |
|
0 commit comments