#!/usr/bin/env python
# coding=utf-8

import json
import requests
from app import app
from flask.ext.babel import gettext
from config import MS_TRANSLATOR_CLIENT_ID, MS_TRANSLATOR_CLIENT_SECRET


def microsoft_translate(text, sourceLang, destLang):
    if MS_TRANSLATOR_CLIENT_ID == "" or MS_TRANSLATOR_CLIENT_SECRET == "":
        return gettext('Error: translation service not configured.')
    try:
        # get access token
        payload = {
            'client_id': MS_TRANSLATOR_CLIENT_ID,
            'client_secret': MS_TRANSLATOR_CLIENT_SECRET,
            'scope': 'http://api.microsofttranslator.com',
            'grant_type': 'client_credentials'}

        r = requests.post('https://datamarket.accesscontrol.windows.net/v2/OAuth2-13', payload)
        token = r.json()[u'access_token']


        # translate
        payload = {
            'appId': 'Bearer ' + token,
            'from': sourceLang,
            'to':   destLang,
            'text': text.encode('utf-8')
        }

        r = requests.get('http://api.microsofttranslator.com/V2/Ajax.svc/Translate', params=payload)
        result = r.content.decode('utf-8-sig')
        response_json = json.loads(u'{{"response":{0}}}'.format(result))
        return response_json["response"]
    except:
        raise


def google_translate(text, sourceLang, destLang):
    if not app.debug:
        return gettext('Error: translation service not available.')
    try:
        params = urlencode({
            'client': 't',
            'text': text.encode("utf-8"),
            'sl': sourceLang,
            'tl': destLang,
            'ie': 'UTF-8',
            'oe': 'UTF-8'})
        conn = httplib.HTTPSConnection("translate.google.com")
        conn.request("GET", "/translate_a/t?" + params,
                     headers={'User-Agent': 'Mozilla/5.0'})
        httpresponse = conn.getresponse().read().replace(
            ",,,", ",\"\",\"\",").replace(",,", ",\"\",")
        response = json.loads("{\"response\":" + httpresponse + "}")
        return response["response"][0][0][0]
    except:
        return gettext('Error: Unexpected error.')

@@ -1284,838 +1284,4 @@
 ise%0A
-%0A%0Adef google_translate(text, sourceLang, destLang):%0A    if not app.debug:%0A        return gettext('Error: translation service not available.')%0A    try:%0A        params = urlencode(%7B%0A            'client': 't',%0A            'text': text.encode(%22utf-8%22),%0A            'sl': sourceLang,%0A            'tl': destLang,%0A            'ie': 'UTF-8',%0A            'oe': 'UTF-8'%7D)%0A        conn = httplib.HTTPSConnection(%22translate.google.com%22)%0A        conn.request(%22GET%22, %22/translate_a/t?%22 + params,%0A                     headers=%7B'User-Agent': 'Mozilla/5.0'%7D)%0A        httpresponse = conn.getresponse().read().replace(%0A            %22,,,%22, %22,%5C%22%5C%22,%5C%22%5C%22,%22).replace(%22,,%22, %22,%5C%22%5C%22,%22)%0A        response = json.loads(%22%7B%5C%22response%5C%22:%22 + httpresponse + %22%7D%22)%0A        return response%5B%22response%22%5D%5B0%5D%5B0%5D%5B0%5D%0A    except:%0A        return gettext('Error: Unexpected error.')%0A

Remove unused google translate function