import requests
import json


class GoogleTranslate:
    @staticmethod
    def translate(source, target, text):
        url = "https://translate.google.com/translate_a/single?client=at&dt=t&dt=ld&dt=qca&dt=rm&dt=bd&dj=1&hl=es-ES&ie=UTF-8&oe=UTF-8&inputm=2&otf=2&iid=1dd3b944-fa62-4b55-b330-74909a99969e"
        fields = {
            'sl': source,
            'tl': target,
            'q': text
        }

        response = requests.post(url, data=fields)

        if response.status_code != 200:
            print('Translation failed')
            return False

        list = json.loads(response.text)
        sentences = []

        for s in list.get('sentences', []):
            if 'trans' in s:
                sentences.append(s['trans'])

        return ''.join(sentences)
