Last active
September 29, 2022 18:03
-
-
Save lgaud/d5e51e5b7ba455e639b20a0678d3ed98 to your computer and use it in GitHub Desktop.
This gist uses the Microsoft Azure Cognitive Services Translator API to translate the title of a Notion page and print out the translation. Accompanies a post on PyNotion.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from dotenv import load_dotenv | |
import requests | |
""" | |
This example uses Azure Cognitive Services Translator and the Notion API to translate the title of a page. | |
For more details, see [Translate a Notion page using Azure Cognitive Services Translator (Part 1)](https://www.pynotion.com/translate-with-azure-translator) | |
Create a Translator resource in Azure, and set the key and region in your environment or a .env file as | |
COG_SERVICE_KEY and COG_SERVICE_REGION. The Free tier (F0) should be plenty for demonstration purposes. | |
This code also works with a multi-service Cognitive Services resource (which does not have a free tier). | |
More details on creating a Translator Resource | |
https://learn.microsoft.com/en-us/azure/cognitive-services/translator/quickstart-translator?tabs=python | |
Create a Notion integration, and set an environment variable or entry in the .env file for NOTION_KEY. | |
Share a page you'd like to run through the translator with the integration, and take a note of the id | |
from the URL to use as the page_id argument. | |
""" | |
class TranslatorClient(): | |
def __init__(self, cog_service_key, cog_service_region): | |
self.cog_key = cog_service_key | |
self.cog_region = cog_service_region | |
self.translator_endpoint = 'https://api.cognitive.microsofttranslator.com' | |
self.default_headers = { | |
'Ocp-Apim-Subscription-Key': self.cog_key, | |
'Ocp-Apim-Subscription-Region': self.cog_region, | |
'Content-type': 'application/json' | |
} | |
self.session = requests.Session() | |
self.session.headers.update(self.default_headers) | |
def translate(self, text, source_language, target_language): | |
url = self.translator_endpoint + '/translate' | |
# Specify Query Parameters | |
params = { | |
'api-version': '3.0', # Required | |
'from': source_language, # Optional, will auto-detect in most cases | |
'to': target_language # Required. | |
} | |
body = [{ | |
'text': text | |
}] | |
# Send the request and get response | |
request = self.session.post(url, params=params, json=body) | |
# Parse the JSON Response | |
response = request.json() | |
translation = response[0]["translations"][0]["text"] | |
# Return the translation | |
return translation | |
class NotionClient(): | |
def __init__(self, notion_key): | |
self.notion_key = notion_key | |
self.default_headers = {'Authorization': f"Bearer {self.notion_key}", | |
'Content-Type': 'application/json', 'Notion-Version': '2022-06-28'} | |
self.session = requests.Session() | |
self.session.headers.update(self.default_headers) | |
def get_property(self, page_id, property_id): | |
url = f"https://api.notion.com/v1/pages/{page_id}/properties/{property_id}" | |
response = self.session.get(url) | |
return response.json() | |
def get_title_text(self, page_id): | |
title_property = self.get_property(page_id, "title")["results"][0] | |
return title_property["title"]["plain_text"] | |
class NotionTranslator(): | |
def __init__(self, notion_client, translate_client, source_language, target_language): | |
self.notion_client = notion_client | |
self.translate_client = translate_client | |
self.source_language = source_language | |
self.target_language = target_language | |
def translate_title(self, source_page_id): | |
title = self.notion_client.get_title_text(source_page_id) | |
translated_title = self.translate_client.translate( | |
title, self.source_language, self.target_language) | |
return translated_title | |
def main(notion_page_id, source_language="en", target_language="fr"): | |
notion_client = NotionClient(os.getenv('NOTION_KEY')) | |
translate_client = TranslatorClient( | |
os.getenv('COG_SERVICE_KEY'), os.getenv('COG_SERVICE_REGION')) | |
translator = NotionTranslator( | |
notion_client, translate_client, source_language, target_language) | |
translated_title = translator.translate_title(notion_page_id) | |
print(translated_title) | |
if __name__ == "__main__": | |
import argparse | |
load_dotenv(override=True) | |
parser = argparse.ArgumentParser( | |
description="Translate a Notion page's title. Supported language codes are listed at https://learn.microsoft.com/en-us/azure/cognitive-services/translator/language-support") | |
parser.add_argument('page_id', type=str, | |
help='A Notion page ID to translate') | |
parser.add_argument('target', | |
help='language code to translate the page to') | |
parser.add_argument('--source', default="en", | |
help="language code for the original language of the page") | |
args = parser.parse_args() | |
main(args.page_id, args.source, args.target) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment