# Path: newsApp/translation.py
# def translate(jobId, text, fromLang, toLang = 'en'):
#   jobInfo = "fromLang: " + fromLang + " toLang: " + toLang \
#      + " Job id: " + jobId
# 
#   # clip text if too long to save costs
#   if len(text) > 800:
#     text = text[:800]
# 
#   if fromLang in MSTRANSLATE_LANGS and fromLang in GOOGLE_LANGS:
#     msResult = translateMicrosoft(jobInfo, text, fromLang, toLang)
#     if len(msResult) > 0:
#       return msResult
#     else:
#       return translateGoogle(jobInfo, text, fromLang, toLang)
#   elif fromLang in GOOGLE_LANGS:
#     return translateGoogle(jobInfo, text, fromLang, toLang)
# 
# def translateMicrosoft(jobInfo, text, fromLang, toLang = 'en'):
#   try:
#     logger.info("Started microsoft translation. %s", jobInfo)
# 
#     # get the access token
#     auth_token = _getMicrosoftAccessToken(jobInfo)
# 
#     translation_args = {
#       'text': text.encode(),
#       'to': toLang,
#       'from': fromLang
#     }
# 
#     headers={'Authorization': 'Bearer ' + auth_token.decode()}
#     translate_url = 'https://api.microsofttranslator.com/V2/Ajax.svc/Translate?'
#     translation_result = requests.get(
#       translate_url + urllib.parse.urlencode(translation_args),
#       headers=headers)
#     response = translation_result.content.decode()
# 
#     if translation_result.status_code == 200 and \
#       'Exception:' not in response:
#       logger.info("Completed microsoft translation. %s", jobInfo)
#       return response
#     else:
#       logger.info(
#         "Microsoft translation call failed. Status code %i. Response: %s",
#         translation_result.status_code,
#         response)
#       return ""
#   except Exception:
#     logging.exception("Microsoft translation failed. %s", jobInfo)
#     return ""
# 
# def translateGoogle(jobInfo, text, fromLang, toLang = 'en'):
#   try:
#     logger.info("Started google translation. %s", jobInfo)
# 
#     service = build('translate', 'v2',
#             developerKey = os.environ['GOOGLE_DEV_KEY'])
#     result = service.translations().list(
#       source=fromLang,
#       target=toLang,
#       q=text).execute()['translations'][0]['translatedText']
# 
#     logger.info("Completed google translation. %s", jobInfo)
#     return result
#   except:
#     logger.exception("Google translation failed. %s", jobInfo)
#     return ""


import unittest
from newsApp.translation import translate, translateMicrosoft, translateGoogle


class ClusterTableManagerTests(unittest.TestCase):

  def testTranslateHindi(self):
    with open('tests/testData/hindiLong.txt', 'r', encoding='utf8') as sourceFile:
      text = sourceFile.read()

    result = translateMicrosoft('job', text, 'hi')
    self.assertTrue(isinstance(result, str))
    self.assertIsNot(result, '')

  def testTranslateBengali(self):
    with open('tests/testData/bengaliLong.txt', 'r', encoding='utf8') as sourceFile:
      text = sourceFile.read()


# Path: newsApp/translation.py
# def translate(jobId, text, fromLang, toLang = 'en'):
#   jobInfo = "fromLang: " + fromLang + " toLang: " + toLang \
#      + " Job id: " + jobId
# 
#   # clip text if too long to save costs
#   if len(text) > 800:
#     text = text[:800]
# 
#   if fromLang in MSTRANSLATE_LANGS and fromLang in GOOGLE_LANGS:
#     msResult = translateMicrosoft(jobInfo, text, fromLang, toLang)
#     if len(msResult) > 0:
#       return msResult
#     else:
#       return translateGoogle(jobInfo, text, fromLang, toLang)
#   elif fromLang in GOOGLE_LANGS:
#     return translateGoogle(jobInfo, text, fromLang, toLang)
# 
# def translateMicrosoft(jobInfo, text, fromLang, toLang = 'en'):
#   try:
#     logger.info("Started microsoft translation. %s", jobInfo)
# 
#     # get the access token
#     auth_token = _getMicrosoftAccessToken(jobInfo)
# 
#     translation_args = {
#       'text': text.encode(),
#       'to': toLang,
#       'from': fromLang
#     }
# 
#     headers={'Authorization': 'Bearer ' + auth_token.decode()}
#     translate_url = 'https://api.microsofttranslator.com/V2/Ajax.svc/Translate?'
#     translation_result = requests.get(
#       translate_url + urllib.parse.urlencode(translation_args),
#       headers=headers)
#     response = translation_result.content.decode()
# 
#     if translation_result.status_code == 200 and \
#       'Exception:' not in response:
#       logger.info("Completed microsoft translation. %s", jobInfo)
#       return response
#     else:
#       logger.info(
#         "Microsoft translation call failed. Status code %i. Response: %s",
#         translation_result.status_code,
#         response)
#       return ""
#   except Exception:
#     logging.exception("Microsoft translation failed. %s", jobInfo)
#     return ""
# 
# def translateGoogle(jobInfo, text, fromLang, toLang = 'en'):
#   try:
#     logger.info("Started google translation. %s", jobInfo)
# 
#     service = build('translate', 'v2',
#             developerKey = os.environ['GOOGLE_DEV_KEY'])
#     result = service.translations().list(
#       source=fromLang,
#       target=toLang,
#       q=text).execute()['translations'][0]['translatedText']
# 
#     logger.info("Completed google translation. %s", jobInfo)
#     return result
#   except:
#     logger.exception("Google translation failed. %s", jobInfo)
#     return ""


# Path: tests/translation_test.py
import unittest
from newsApp.translation import translate, translateMicrosoft, translateGoogle



class ClusterTableManagerTests(unittest.TestCase):

  def testTranslateHindi(self):
    with open('tests/testData/hindiLong.txt', 'r', encoding='utf8') as sourceFile:
      text = sourceFile.read()

    result = translateMicrosoft('job', text, 'hi')
    self.assertTrue(isinstance(result, str))
    self.assertIsNot(result, '')

  def testTranslateBengali(self):
    with open('tests/testData/bengaliLong.txt', 'r', encoding='utf8') as sourceFile:
      text = sourceFile.read()


    result = translate('job', text, 'bn')