📜 ⬆️ ⬇️

Import dictionary in Lingualeo.com



Prehistory


There are several solutions for importing words into Lingualeo.com :


The disadvantages of these methods are that you can add words one by one. We need an implementation that allows us to add a few words at a time.

Should there be an API?


The official api could not be found. BUT! There are browser extensions, and this is a great way to find an internal api for the service.
Go to Google Chrome and stamp on the site of the service. At the bottom of the page offers applications and extensions. Choosing for Chrome. The browser installs the extension to the Extensions folder (I do not specify the full path, it depends on the OS). Inside the directory will be several folders with hashes in the form of names. Select the last by date. Inside you can find the file config.js - it contains all the paths to the project API. We are only interested in three of them:
')

What to write?


Chose python, because installed by default on most OS. Modules take those that do not require additional installation. We implement a solution for working with api.

service.py
import urllib import urllib2 import json from cookielib import CookieJar class Lingualeo: def __init__(self, email, password): self.email = email self.password = password self.cj = CookieJar() def auth(self): url = "http://api.lingualeo.com/api/login" values = { "email": self.email, "password": self.password } return self.get_content(url, values) def add_word(self, word, tword, context): url = "http://api.lingualeo.com/addword" values = { "word": word, "tword": tword, "context": context, } self.get_content(url, values) def get_translates(self, word): url = "http://api.lingualeo.com/gettranslates?word=" + urllib.quote_plus(word) try: result = self.get_content(url, {}) translate = result["translate"][0] return { "is_exist": translate["is_user"], "word": word, "tword": translate["value"].encode("utf-8") } except Exception as e: return e.message def get_content(self, url, values): data = urllib.urlencode(values) opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj)) req = opener.open(url, data) return json.loads(req.read()) 


The original solution was for text files only. Each word must be on a new line. But in the process of writing code, I decided to add an implementation for the Kindle, because periodically from him, too, must take the words.

handler.py
 class Word: text = ''; context = ''; def __init__(self, text): self.text = text class Base(object): data = [] def __init__(self, source): self.source = source def get(self): return self.data def read(self): raise NotImplementedError('Not implemented yet') class Kindle(Base): def read(self): conn = sqlite3.connect(self.source) sql = 'select word, usage from words LEFT JOIN LOOKUPS ON words.id = LOOKUPS.word_key where words.lang="en" GROUP BY word ORDER BY word;' for row in conn.execute(sql): if isinstance(row[0], unicode): word = Word(row[0]) if isinstance(row[1], unicode): word.context = row[1] self.data.append(word) conn.close() class Text(Base): def read(self): f = open(self.source) for word in f.readlines(): self.data.append(Word(word)) f.close() 


And the implementation of the script itself for the export / import of words:

export.py
 import handler import config import service import sys email = config.auth.get('email') password = config.auth.get('password') export_type = sys.argv[1] if export_type == 'text': word_handler = handler.Text(config.sources.get('text')) elif export_type == 'kindle': word_handler = handler.Kindle(config.sources.get('kindle')) else: raise Exception('unsupported type') word_handler.read() lingualeo = service.Lingualeo(email, password) lingualeo.auth() for word_dto in word_handler.get(): word = word_dto.text.lower().encode('utf-8') translate = lingualeo.get_translates(word) if translate["is_exist"]: print "Already exists: " + word.strip() else: context = word_dto.context.encode('utf-8') lingualeo.add_word(word, translate["tword"], context) print "Add word: " + word.strip() 


Startup and installation


Download the code from github. Create a config.py file from config.py.dist. Register the path to the file with the words. In a case with kindle to sqlite of basis inside kindle.

 python export.py text #   python export.py kindle # kindle 

Sources


GitHub: lingualeo.export .

Source: https://habr.com/ru/post/276495/


All Articles