Web Browser Extensions (Software) for LingQ

I have asked LingQ in the past to provide a button to translate every sentence in a lesson. Going through sentence mode and clicking the ‘translate’ button is really annoying. I actually prefer translations to dictionary lookups when I’m a beginner. Also, I sometimes like to use the app offline, which makes online translations impossible. At one point I hacked together a script to do this for my private lessons. I’ll include it down below, maybe someone can make use of it.

Summary
import requests

KEY = '12345'

LESSON_ID = '12345'
LANGUAGE = 'zh'

INDEX_URL = f'https://www.lingq.com/api/v3/{LANGUAGE}/lessons/{LESSON_ID}/editor/?'
TRANSLATE_URL = f'https://www.lingq.com/api/v2/{LANGUAGE}/lessons/{LESSON_ID}/sentences/'

HEADERS = {
    'accept': 'application/json',
    'Authorization': f"Token {KEY}",
    'content-type': 'application/json'
}

def get_indexes():
    response = requests.get(INDEX_URL, headers=HEADERS)

    if response.status_code == 200:
        data = response.json()
        indexes = []

        for paragraph in data.get('paragraphs', []):
            for sentence in paragraph.get('sentences', []):
                indexes.append(sentence.get('index'))

        return indexes
    else:
        print(f'Error: {response.status_code}\nResponse: {response.text}')
        return []

def trigger_translation(index):
    data = {
        'index': index,
        'language': 'en',
        'is_google_translate': True
    }

    response = requests.post(TRANSLATE_URL, headers=HEADERS, json=data)

    if response.status_code == 201:
        return response.text
    else:
        return f'Error: {response.status_code}\nResponse: {response.text}'

if __name__ == "__main__":
    indexes = get_indexes()
    if indexes:
        for index in indexes:
            translated_sentence = trigger_translation(index)
            print(f'Translated Sentence for index {index}: {translated_sentence}')
4 Likes