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())
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()
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()
python export.py text # python export.py kindle # kindle
Source: https://habr.com/ru/post/276495/
All Articles