Skip to content

Instantly share code, notes, and snippets.

@mapapage
Last active January 6, 2022 21:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mapapage/bb07703afaa4ea590109 to your computer and use it in GitHub Desktop.
Save mapapage/bb07703afaa4ea590109 to your computer and use it in GitHub Desktop.
This script helps you migrate the source+translation files from one project to another
Run the script providing the slug of the old project and the slug of the new project.
Example:
Old project slug: 'old'
New project slug: 'new'
python migrate_project.py old new
After running the command you will be asked for your Transifex username and password.
Any exceptions will be logged in the file tx_logs.txt.
from hashlib import md5
import requests
from requests.exceptions import RequestException
import json
import getpass
import sys
SERVER = "https://www.transifex.com"
def get_source_entity_hash(context, key):
if isinstance(context, list):
if context:
keys = [key] + context
else:
keys = [key, '']
else:
if context:
keys = [key, context]
else:
keys = [key, '']
return str(md5(':'.join(keys).encode('utf-8')).hexdigest())
def migrate_project(from_slug, to_slug, AUTH):
headers = {'Content-type': 'application/json'}
langs_url = "/api/2/project/%s/languages/" % from_slug
lang_entries = requests.get(
SERVER + langs_url,
headers=headers,
auth=AUTH
).content
lang_entries = json.loads(lang_entries)
res_from_url = "/api/2/project/%s/resources/" % from_slug
res_entries = requests.get(
SERVER + res_from_url,
headers=headers,
auth=AUTH
).content
res_entries = json.loads(res_entries)
res_to_url = "/api/2/project/%s/resources/" % to_slug
for res_entry in res_entries:
content_url = "/api/2/project/%s/resource/%s/content" % (from_slug, res_entry["slug"])
try:
response = requests.get(
SERVER + content_url,
headers=headers,
auth=AUTH
)
response.raise_for_status()
except RequestException as e:
msg = "HTTP ERROR %s occurred for the resource %s. Reason: " % (
response.status_code, res_entry["slug"]
)
error_msg = msg + response.text
with open("tx_logs.txt", "a") as f:
f.write(error_msg)
response = json.loads(response.content)
content = response['content']
del res_entry['source_language_code']
try:
response = requests.post(
SERVER + res_to_url,
data=json.dumps(
{'slug':res_entry['slug'], 'name':res_entry['name'],
'i18n_type':res_entry['i18n_type'], 'content': content}
),
headers=headers,
auth=AUTH
)
response.raise_for_status()
except RequestException as e:
msg = "HTTP ERROR %s occurred for the resource %s. Reason: " % (
response.status_code, res_entry["slug"]
)
error_msg = msg + response.text
with open("tx_logs.txt", "a") as f:
f.write(error_msg)
for lang_entry in lang_entries:
try:
strings_url = "/api/2/project/%s/resource/%s/translation/%s/strings/"
strings_from_url = strings_url % (
from_slug,
res_entry["slug"],
lang_entry['language_code']
)
strings_to_url = strings_url % (
to_slug,
res_entry["slug"],
lang_entry['language_code']
)
strings = requests.get(
SERVER + strings_from_url + "?details",
headers=headers,
auth=AUTH
).content
reviewed = list()
strings = json.loads(strings)
for elem in strings:
del elem["user"]
context = elem["context"]
key = elem["key"]
source_entity_hash = get_source_entity_hash(
context, key
)
elem["source_entity_hash"] = source_entity_hash
if elem["reviewed"]:
reviewed.append(elem)
elem["reviewed"] = False
response = requests.put(
SERVER + strings_to_url,
data=json.dumps(strings),
headers=headers,
auth=AUTH
)
response.raise_for_status()
if reviewed:
s = [{
"source_entity_hash": elem["source_entity_hash"],
"translation": elem["translation"],
"reviewed": True
} for elem in reviewed]
response = requests.put(
SERVER + strings_to_url,
data=json.dumps(s),
headers=headers,
auth=AUTH
)
response.raise_for_status()
except RequestException as e:
msg = "HTTP ERROR %s occurred for the resource %s in the \
language %s. Reason: " % (
response.status_code, res_entry["slug"],
lang_entry["language_code"]
)
error_msg = msg + response.text
with open("tx_logs.txt", "a") as f:
f.write(error_msg)
except Exception, e:
msg = 'Unknown exception caught: %s' % (
unicode(e)).encode('utf-8')
with open("tx_logs.txt", "a") as f:
f.write(msg)
def run():
if not len(sys.argv) == 3:
print "You should do: python script_name.py <project_from> <project_to>"
sys.exit()
#from_project: the slug of the old project
#to_project: the slug of the new project
from_project = sys.argv[1]
to_project = sys.argv[2]
username = raw_input("What's your Transifex username?")
pwd = getpass.getpass(prompt="..and your Transifex password?")
# Your Transifex credentials
AUTH = (username, pwd)
migrate_project(from_project, to_project, AUTH)
if __name__ == '__main__':
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment