Last active
July 6, 2018 15:40
-
-
Save xlbruce/d2f4f196a137e3e6fc0ed01548dfd5fd to your computer and use it in GitHub Desktop.
Usage: python translate_words.py <input_file> <output_file>
This file contains hidden or 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
| { | |
| "1": "Crate", | |
| "2": "Cave Entrance", | |
| "3": "Door", | |
| "5": "Broken multicannon", | |
| "6": "Dwarf multicannon", | |
| "7": "Cannon base", | |
| "8": "Cannon stand", | |
| "9": "Cannon barrels", | |
| "11": "Ladder", | |
| "13": "Mud pile", | |
| "15": "Railing", | |
| "16": "Railing", | |
| "17": "Railing", | |
| "18": "Railing", | |
| "19": "Railing", | |
| "20": "Railing", | |
| "22": "Door", | |
| "23": "Sacks", | |
| "25": "Clock spindle", | |
| "26": "Clock spindle", | |
| "27": "Clock spindle", | |
| "28": "Clock spindle", | |
| "29": "Clock spindle", | |
| "30": "Clock spindle", | |
| "31": "Clock spindle", | |
| "32": "Clock spindle", | |
| "33": "Lever", | |
| "34": "Lever", | |
| "35": "Lever", | |
| "65570": "Table", | |
| "36": "Lever", | |
| "65573": "Gate", | |
| "37": "Gate", | |
| "38": "Gate", | |
| "65575": "Gate", | |
| "39": "Gate", | |
| "65574": "Gate", | |
| "40": "Food trough", | |
| "65577": "Gate", | |
| "41": "Wall Pipe", | |
| "65576": "Gate", | |
| "43": "Water", | |
| "65578": "Gate", | |
| "45": "Staircase", | |
| "46": "Staircase", | |
| "47": "Gate", | |
| "48": "Gate", | |
| "49": "Gate", | |
| "50": "Gate", | |
| "51": "Loose Railing", | |
| "52": "Gate", | |
| "53": "Gate", | |
| "54": "Stairs", | |
| "55": "Stairs", | |
| "56": "Stairs", | |
| "57": "Stairs", | |
| "58": "Vine", | |
| "59": "Door", | |
| "61": "Chaos altar", | |
| "63": "Crate", | |
| "64": "Crate", | |
| "65": "Crate wall", | |
| "66": "Crate wall", | |
| "65602": "Range", | |
| "68": "Beehive", | |
| "69": "Gangplank", | |
| "70": "Gangplank", | |
| "71": "Large door", | |
| "72": "Large door", | |
| "73": "Large door", | |
| "74": "Large door", | |
| "76": "Parasol", | |
| "77": "Door", | |
| "79": "Prison door", | |
| "80": "Prison door", | |
| "65617": "Obelisk", | |
| "81": "Door", | |
| "65616": "Obelisk", | |
| "82": "Door", | |
| "65619": "Obelisk", | |
| "65618": "Obelisk", | |
| "65621": "Obelisk", | |
| "65620": "Obelisk", | |
| "86": "Lever bracket", | |
| "87": "Lever", | |
| "65622": "Obelisk", | |
| "65625": "Obelisk", | |
| "89": "Gate", | |
| "90": "Gate", | |
| "65627": "Obelisk", | |
| "91": "Lever", | |
| "65626": "Glowing Orb", | |
| "92": "Door", | |
| "93": "Door", | |
| "94": "Gate", | |
| "95": "Gate", | |
| "96": "Stairs", | |
| "99": "Door", | |
| "100": "Trapdoor" | |
| } |
This file contains hidden or 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 requests as req | |
| import codecs | |
| import json | |
| import sys | |
| import requests_cache | |
| from concurrent.futures import ThreadPoolExecutor | |
| requests_cache.install_cache('cache') | |
| template_url = 'https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=pt&dt=t&q={query}' | |
| def get_source(filename): | |
| print('Opening {}'.format(filename)) | |
| with codecs.open(filename, 'r', 'utf-8') as f: | |
| return json.load(f) | |
| def async_process(filename): | |
| with ThreadPoolExecutor(max_workers=10, thread_name_prefix='req_thread') as executor: | |
| obj = get_source(filename) | |
| results = [] | |
| for k, v in obj.iteritems(): | |
| print('Processing {%s,%s}' % (k,v)) | |
| result = executor.submit(translate, k, v) | |
| results.append(result) | |
| return results | |
| def process(filename): | |
| results = async_process(filename) | |
| output = {} | |
| for result in results: | |
| output.update(result.result()) | |
| return output | |
| def translate(key, word): | |
| url = template_url.format(query=word) | |
| print('Url: {}'.format(url)) | |
| r = req.get(url) | |
| if r.status_code != 200: | |
| print('Error: {}'.format(r)) | |
| raise Exception | |
| response = r.json() | |
| try: | |
| translated = response[0][0][0] | |
| return {key: { | |
| word: translated | |
| } | |
| } | |
| except IndexError: | |
| return {key: "not found"} | |
| print('Starting') | |
| result = process(sys.argv[1]) | |
| with codecs.open(sys.argv[2], 'w', 'utf-8') as f: | |
| json.dump(result, f) | |
| f.flush() | |
| print('Finished') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment