|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import sys |
| 4 | +import CloudFlare |
| 5 | + |
| 6 | +def translate_call(cf): |
| 7 | + # So we walk over the @ via a getattr() call. |
| 8 | + # We also have to deal with a . in a verb - that does not work in Python. So sad. |
| 9 | + # Also, the - is actually an _ in this Python library. |
| 10 | + # This is not normally needed for other calls |
| 11 | + m = cf.accounts.ai.run |
| 12 | + m = getattr(m, '@cf') |
| 13 | + m = getattr(m, 'meta') |
| 14 | + m = getattr(m, 'm2m100_1.2b') |
| 15 | + return m |
| 16 | + |
| 17 | +def doit(account_name, english_text): |
| 18 | + |
| 19 | + cf = CloudFlare.CloudFlare() |
| 20 | + |
| 21 | + try: |
| 22 | + params = {'name': account_name, 'per_page': 1} |
| 23 | + accounts = cf.accounts.get(params=params) |
| 24 | + except CloudFlare.exceptions.CloudFlareAPIError as e: |
| 25 | + exit('/accounts %d %s - api call failed' % (e, e)) |
| 26 | + |
| 27 | + account_id = accounts[0]['id'] |
| 28 | + |
| 29 | + translate_data = { |
| 30 | + 'text': english_text, |
| 31 | + 'source_lang': 'english', |
| 32 | + 'target_lang': 'french', |
| 33 | + } |
| 34 | + |
| 35 | + # This should be easy to call; however, the @ will not work in Python (or many languages) |
| 36 | + # r = [email protected](account_id, data=translate_data) |
| 37 | + |
| 38 | + the_call = translate_call(cf) |
| 39 | + |
| 40 | + try: |
| 41 | + r = the_call.post(account_id, data=translate_data) |
| 42 | + except CloudFlare.exceptions.CloudFlareAPIError as e: |
| 43 | + exit('/ai.run %d %s - api call failed' % (e, e)) |
| 44 | + |
| 45 | + print(r['translated_text']) |
| 46 | + |
| 47 | +def main(): |
| 48 | + account_name = sys.argv[1] |
| 49 | + if len(sys.argv) > 2: |
| 50 | + english_text = ' '.join(sys.argv[2:]) |
| 51 | + else: |
| 52 | + english_text = "I'll have an order of the moule frites" |
| 53 | + doit(account_name, english_text) |
| 54 | + |
| 55 | +if __name__ == '__main__': |
| 56 | + main() |
0 commit comments