Azure AI and Python: Automate HTML Translation and Medium Publishing

Azure AI and Python: Automate HTML Translation and Medium Publishing This blog post was created automatically from this blog post using Python and the power of Azure AI Text Translator. Translating and publishing content on different platfo

Source comparison and review notes
  • Automated pair-review warning (AI bilingual content review): The English Translator code changes source_language_code from the Spanish source's es to is even though the article describes Spanish-to-English translation; is denotes Icelandic and would translate the wrong declared source language.

This blog post was created automatically from this blog post using Python and the power of Azure AI Text Translator.

Image

Translating and publishing content on different platforms can be a tedious and time-consuming process. However, thanks to advances in artificial intelligence and software development, this process can be automated. In this article, we’ll explore how Azure AI integration for text translation and post automation in Medium with Python.

This stems from a personal need, as I am currently publishing IT content in both Spanish (madsblog.net) and English (on this blog). After doing this process manually, I sometimes started thinking about automating it. For this, I started exploring a solution using Python and Azure AI.

Although it is a Work In Progress, the Work In Progress has come up with a fairly solid solution to automate almost 100% of the process.

Here’s the repository (the app don’t have the automatic passage of Links configured yet, ha!, I’ll incorporate this functionality soon. But Medium automatically detect written links, so is not so bad…):

GitHub - mdiloreto/translate-blog-html-and-post-to-medium
Contribute to mdiloreto/translate-blog-html-and-post-to-medium development by creating an account on GitHub.github.com

The Repo

This project, called “translate-blog-html-and-post-to-medium,” automates the translation of blog posts into HTML and their publication on Medium. It aims to help bloggers and content creators reach a wider audience by translating and publishing their blogs in different languages.

Characteristics

  • Extracts HTML content from a specified blog post.
  • Translate your content using Azure AI Translation services.
  • Convert translated content to Markdown format.
  • Publish the translated entry on Medium as a draft.

Translating Blog HTML and Publishing on Medium

Description

This project, called “translate-blog-html-and-post-to-medium,” automates the translation of blog posts into HTML and their publication on Medium. It aims to help bloggers and content creators reach a wider audience by translating and publishing their blogs in different languages.

Characteristics

  • Extracts HTML content from a specified blog post.
  • Translate your content using Azure AI Translation services.
  • Convert translated content to Markdown format.
  • Publish the translated entry on Medium as a draft.

Prerequisites

  • Python 3.x
  • Azure AI Translation API Key and Endpoint
  • Medium API Access Token

Installation

Clone the repository:

Bash
git clone https://github.com/mdiloreto/translate-blog-html-and-post-to-medium

Navigate to directory:

Bash
cd translate-blog-html-and-post-to-medium

Install the dependencies:

Bash
pip install -r requirements.txt

Use

Set the URL in the ‘main.py’ file on line #16:

Plain text
main.py
Plain text
scraper = Scraper('https://madsblog.net/2023/11/24/storage-persistente-en-gke/') # <<<<< <<<<<<<<< SET THE URL

Sets the required environment variables for Azure AI and Medium API tokens.

PowerShell
$env:AZUREAI_ENDPOINT='' $env:AZUREAI_CREDENTIAL='' <Your Key><TU_CREDENCIAL_AZUREAI>$env:MEDIUM_ACCESS_TOKEN='<Your Medium personal access token>'

Then, run the scriptmain.py:

Plain text
main.py
Plain text
Python main.py

Now we can see the Draft in the Medium Stories:

Translate with Azure Text Translator

In this post, we won’t focus on each of the parts of the code, but we will take a look at the code used to perform the translation with Azure Text Translator:

Python
from azure.ai.translation.document import DocumentTranslationClient
from azure.core.credentials import AzureKeyCredential
import requests, uuid, json
class Translator:
def __init__(self, endpoint, key):
self.endpoint = endpoint # Store the endpoint as an instance variable
self.key = key # Store the key as an instance variable
self.client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))
def translate(self, text):
# Your code to translate text
location = "eastus"
path = '/translate'
constructed_url = self.endpoint + path
params = {
'api-version': '3.0',
'from': 'is',
'to': ['en']
}
headers = {
'Ocp-Apim-Subscription-Key': self.key,
'Ocp-Apim-Subscription-Region': location,
'Content-type': 'application/json',
'X-ClientTraceId': str(uuid.uuid4())
}
# You can pass more than one object in body.
body = [{
'text': text
}]
try:
request = requests.post(constructed_url, params=params, headers=headers, json=body)
request.raise_for_status() # Raise exception for HTTP errors
response = request.json()
# Assuming response contains the translated text
return response[0]['translations'][0]['text']
except Exception as e:
print(f"Error during translation: {e}")
return None

1.ClassTranslator: Defines a class in Python to handle text translation.

Plain text
Translator

2.Azure AI Imports and Utilities: Uses Azure AI libraries (DocumentTranslationClient,AzureKeyCredential) and standard Python modules (requests,uuid,json) to perform network operations and data handling.

Plain text
DocumentTranslationClient
Plain text
AzureKeyCredential
Plain text
Requests
Plain text
uuid
Plain text
json

3.Constructor__init__: Initializes the class with variables for the endpoint and Azure AI key, and creates an instance of the DocumentTranslationClient for interaction with Azure AI.

Plain text
__Init__
Plain text
DocumentTranslationClient

4.Translate Method:

Plain text
Translate
  • Build the URL for the Azure AI API.
  • It prepares the parameters and headers of the HTTP request, includes the API version, the source and target languages, the subscription key, and a unique identifier for the client trace.
  • Make a POST request to translate the text, handling the response and possible errors.
  1. Response and Exception Handling: Converts the JSON response into translated text and handles exceptions to ensure code robustness.

This class is called from the principalmain.py file:

Plain text
main.py
Python
from translator_azureai import Translator
## ...
## ...... rest of the code ....#
## ...
# Check Enviroment Variables
if not endpoint:
raise ValueError("No endpoint found in environment variables")
if not credential:
raise ValueError("No credential found in environment variables")
# Initialize Translator
translator = Translator(endpoint, credential)
# Translate Content
content_en = []
print("Translation in progress...")
for element in content:
if element['type'] != 'image': # Skip translation for images
translated_text = translator.translate(element['content'])
content_en.append({'type': element['type'], 'content': translated_text})
else:
content_en.append(element) # Add images as-is
print("Translation finished...")
print("Showing translation...")
for element in content_en:
print(f"{element['type']}: {element['content']}\n")

This code snippet performs the following operations:

1.Environment Variable Check: Check if the endpointycredential variables are defined. If they are not present, an error is raised.

Plain text
Endpoint
Plain text
credential

2.Translator Initialization: Instantiates the Translator class using the endpointycredential variables.

Plain text
Translator
Plain text
Endpoint
Plain text
credential
  1. Content Translation: Scroll through a list of content items, translate each element that is not an image using the created translator, and add the results to a new list (content_en).
Plain text
Content
Plain text
content_en
  1. Display of Results: Prints the translated content, showing both the type of element and its translated content.

Publication to Medium

For publishing to Medium we use the Medium API.

Python
import requests
class MediumPublisher:
def __init__(self, access_token):
self.base_url = "https://api.medium.com/v1"
self.headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json",
"Accept": "application/json"
}
def create_post(self, user_id, title, content,):
url = f"{self.base_url}/users/{user_id}/posts"
data = {
"title": title,
"contentFormat": "markdown",
"content": content,
"tags": [],
"publishStatus": "draft"
}
response = requests.post(url, json=data, headers=self.headers)
return response.json()
def get_user_id(self):
url = f"{self.base_url}/me"
response = requests.get(url, headers=self.headers)
return response.json().get("data").get("id")

The code defines the MediumPublisher class to publish content to Medium using its API:

Plain text
MediumPublisher

1.Initialization: The class is initialized with an access token and prepares the necessary headers for HTTP requests to the Medium API.

  1. Publication of Content:
  • create_post: Publish a new article on Medium. It takes the user ID, title, and content of the article, and sends it to Medium using a POST request.
  • The post is created in Markdown format and set as a draft.
Plain text
create_post
  1. Obtaining User ID:
  • get_user_id: Obtains the Medium user ID through a GET request to the API.
Plain text
get_user_id

This class is called from the principalmain.py file:

Plain text
main.py
Python
from publish_medium import MediumPublisher
## ...
## ...... rest of the code ....#
## ...
# Medium publish
print("Initializing Medium posting...")
access_token = os.getenv('MEDIUM_ACCESS_TOKEN')
if not access_token:
raise ValueError("No Medium access token found in environment variables")
Medium_publisher = MediumPublisher(access_token)
# Get content for the file
with open("C:\\Users\\mateo\\OneDrive\\vscode\\py-chatgpt-translate-webpost\\output.md", "r", encoding="utf-8") as file:
file_content = file.read()
markdown_file_path = 'C:\\Users\\mateo\\OneDrive\\vscode\\py-chatgpt-translate-webpost\\output.md'
# Prepare Medium publish
user_id = Medium_publisher.get_user_id()
title = ""
for element in content_en:
if element['type'] == 'title':
title = element['content']
break
print("Posting content to Medium as Draft...")
# Publish
post = Medium_publisher.create_post(user_id, title, file_content)
print("Medium post draft was created sucessfully!")

1.Initializing the Publication on Medium: Begins with a message indicating that the publication on Medium is being initialized.

2.Obtaining the Access Token: Retrieves the Medium access token from the environment variables. If it can’t find the token, it generates an error.

  1. Create an Instance to Publish to Medium: Create an instance of MediumPublisher using the access token.
Plain text
MediumPublisher

4.Read File Content: Open and read the contents of the Markdown (output.md) file to be published.

Plain text
output.md
  1. Obtaining the User ID on Medium: Use the instance created to obtain the User ID on Medium.
  2. Determining the Title of the Article: Go through the elements of the translated content (content_en) to find and assign the title of the article.
Plain text
content_en
  1. Publishing to Medium: Call the métodocreate_postde the MediumPublisher instance to publish the read content as a draft on Medium, using the user ID and title obtained.
Plain text
create_post
Plain text
MediumPublisher
  1. Publication Confirmation: Prints a message indicating that the draft publication was successfully created on Medium.

Mateo Di Loreto

Thanks for reading. The learning continues in the next article.