Welcome back to the Script Chef series! Today’s dish is a handy Python script that will help you transfer context screenshots between Transifex projects. Screenshots are essential for providing visual context to translators, and moving them manually can be time-consuming. Our recipe will guide you through this process, making it easy and automated using the Transifex API.
Let’s gather our ingredients and dive right into the cooking process!
Ingredients
To whip up this recipe, you’ll need:
- API tokens for both the source (FROM) and destination (TO) organizations.
- Organization and project slugs for both the
FROM
andTO
projects. - Python installed on your machine, along with the required libraries.
Install the required libraries using the following command:
pip install requests transifex.api
Step-by-Step Cooking Instructions
Step 1: Setting Up Your Utensils (Imports)
We begin by importing the necessary libraries. These will help us interact with the Transifex API and handle HTTP requests.
import io
import requests
from transifex.api import TransifexApi
from transifex.api.jsonapi import JsonApiException
Step 2: Preparing the Input Prompt Function
Next, we need a handy kitchen tool to gather inputs from the user. This prompt
function will repeatedly ask for input until it receives a valid response.
def prompt(msg, validate=None):
"""
Repeatedly try to get an answer from the user
"""
while True:
answer = input(msg).strip()
if answer and (validate is None or answer in validate):
return answer
Step 3: Main Recipe – Transferring Screenshots
Now, let’s cook up the main dish: transferring the screenshots. We’ll break this part down step by step.
Step 3.1: Gather Ingredients from the User
First, we’ll ask for the API tokens and project details for both the source (FROM) and destination (TO) projects. These will help us access the projects and carry out the transfer.
def main():
"""Main function"""
# Collect input for the FROM organization
token_from = prompt("Please enter the API token for your FROM organization: ")
api_from = TransifexApi(auth=token_from)
organization_slug_from = prompt("Please enter the Organization slug to transfer screenshots FROM: ")
project_slug_from = prompt("Please enter the project slug to transfer screenshots FROM: ")
project_id_from = f"o:{organization_slug_from}:p:{project_slug_from}"
Step 3.2: Verify the FROM Project
We need to verify that the project we’re transferring screenshots from exists. The script checks the API token and project slugs to ensure everything is correct.
# Verify that the FROM project exists
try:
api_from.Project.get(project_id_from)
except JsonApiException.get(401):
print("Unauthorized, please check the API token you have provided.")
return
except JsonApiException.get(404):
print("Project FROM not found, please check your input.")
return
Step 3.3: Gather Input for the TO Organization
Next, we collect similar information for the destination (TO) project.
# Collect input for the TO organization
token_to = prompt("Please enter the API token for your TO organization: ")
api_to = TransifexApi(auth=token_to)
organization_slug_to = prompt("Please enter the Organization slug to transfer screenshots TO: ")
project_slug_to = prompt("Please enter the project slug to transfer screenshots TO: ")
project_id_to = f"o:{organization_slug_to}:p:{project_slug_to}"
Step 3.4: Verify the TO Project
We ensure the destination project exists before proceeding.
# Verify that the TO project exists
try:
api_to.Project.get(project_id_to)
except JsonApiException.get(401):
print("Unauthorized, please check the API token you have provided.")
return
except JsonApiException.get(404):
print("Project TO not found, please check your input.")
return
Step 3.5: The Magic – Transfer Screenshots
Now comes the magic moment: transferring the screenshots! The script will loop through all screenshots in the source project, download them, and upload them to the destination project.
# Transfer screenshots from the FROM project to the TO project
for screenshot in api_from.ContextScreenshot.filter(project=project_id_from).all():
name, _ = screenshot.name.rsplit(".", 1) # Get screenshot name without the file extension
# Download the screenshot from the FROM project
download_response = requests.get(screenshot.media_url)
download_response.raise_for_status()
# Upload the screenshot to the TO project
upload_response = requests.post(
f"https://usermedia.transifex.com/context_screenshots/{project_id_to}",
data={"name": name},
files={"content": io.BytesIO(download_response.content)},
headers={"Authorization": f"Bearer {token_to}"},
)
upload_response.raise_for_status()
print(f"Screenshot '{name}' uploaded successfully.")
Step 4: Taste Test – Running the Script
Finally, all that’s left is to run the script and transfer those screenshots!
if __name__ == "__main__":
main()
Final Notes
And there you have it! A simple, streamlined way to transfer screenshots between Transifex projects using the API. This recipe is perfect for those who want to automate the tedious task of moving visual context between projects, making localization smoother and more efficient.
Feel free to modify this script to suit your specific needs, and stay tuned for more recipes in our Script Chef series. Happy coding and bon appétit!