-
-
Save bkawakami/b4ddbf572ffeabdfe2ba6ab900d8fb48 to your computer and use it in GitHub Desktop.
Python Script to ContentStack
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
from urllib.request import Request, urlopen | |
import json | |
import sys | |
import getopt | |
def getDataNative(env, loc, key, token): | |
req = Request( | |
'https://cdn.contentstack.io/v3/content_types/mobile_translation_poc_/entries/blt9624c908f899c058?environment=%s&locale=%s' % ( | |
env, loc)) | |
req.add_header('api_key', key) | |
req.add_header('access_token', token) | |
content = urlopen(req).read().decode("utf-8") | |
obj = json.loads(content) | |
return obj | |
def getArgs(argv): | |
env = '' | |
loc = '' | |
key = '' | |
token = '' | |
platform = '' | |
try: | |
opts, args = getopt.getopt(argv, "he:l:k:t:p:", | |
["environment=", "locale=", "apikey=", "accesstoken=", "platform="]) | |
except getopt.GetoptError: | |
print('main.py -e <environment:development|production> -l <locale:en-us> -k <apikey> -t <accesstoken> -p ' | |
'<platform:android|ios>') | |
sys.exit(2) | |
for opt, arg in opts: | |
if opt == '-h': | |
print('main.py -e <environment:development|production> -l <locale:en-us> -k <apikey> -t <accesstoken> -p ' | |
'<platform:android|ios>') | |
sys.exit() | |
elif opt in ("-e", "--environment"): | |
env = arg | |
elif opt in ("-l", "--locale"): | |
loc = arg | |
elif opt in ("-k", "--apikey"): | |
key = arg | |
elif opt in ("-t", "--accesstoken"): | |
token = arg | |
elif opt in ("-p", "--platform"): | |
platform = arg | |
if not env or not loc or not key or not token or not platform: | |
print('main.py -e <environment:development|production> -l <locale:en-us> -k <apikey> -t <accesstoken> -p ' | |
'<platform:android|ios>') | |
sys.exit() | |
return { | |
"environment": env, | |
"locale": loc, | |
"key": key, | |
"token": token, | |
"platform": platform | |
} | |
def androidTransfer(labels_dict): | |
info_data = '' | |
for key, value in labels_dict.items(): | |
info_data += ' <string name="%s">%s</string>\n' % (key, value) | |
return "<resources>\n%s</resources>" % info_data | |
def iosTransfer(labels_dict): | |
# TODO: make the structure to IOS | |
info_data = '' | |
for key, value in labels_dict.items(): | |
info_data += ' <string name="%s">%s</string>\n' % (key, value) | |
return "<resources>\n%s</resources>" % info_data | |
def saveData(labels_dict, platform): | |
if platform == "ios": | |
text_file = open("strings.xml", "w") | |
text_file.write(androidTransfer(labels_dict)) | |
text_file.close() | |
else: | |
text_file = open("strings.xml", "w") | |
text_file.write(androidTransfer(labels_dict)) | |
text_file.close() | |
def main(argv): | |
labels_dict = {} | |
international_data = getDataNative( | |
getArgs(argv)['environment'], | |
getArgs(argv)['locale'], | |
getArgs(argv)['key'], | |
getArgs(argv)['token'] | |
)['entry']['data'] | |
for key, value in international_data.items(): | |
if isinstance(international_data[key], dict): | |
for key_item, value_item in international_data[key].items(): | |
labels_dict[key_item] = value_item | |
saveData(labels_dict, getArgs(argv)['platform']) | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment