-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLingua.py
148 lines (122 loc) · 4.75 KB
/
Lingua.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
__author__ = 'FishHead'
# -*- coding: utf-8 -*-
import requests
import sys
from re import findall, sub
reload(sys)
sys.setdefaultencoding("utf8")
class Variables():
"""
Variables collector :)
"""
def __init__(self):
self.mail = ''
self.password = ''
self.filename = ''
self.r = ''
self.cookie = {}
self.w = ''
self.words = ()
self.example = ''
def start():
"""
Introduction.
"""
Variables.mail = raw_input('Введите логин от сайта:\n')
Variables.password = raw_input('Введите пароль:\n')
Variables.filename = raw_input('Введите имя файла для выгрузки в Anki(включая расширение .txt):\n')
def create_wordslist():
"""
Open file with words and read them into list.
"""
global words
with open('C:\\Users\\Dmitriy\\Documents\\Anki\\fish\\test.txt') as f:
words = (f.read()).split('\n')
return
def login(email, password):
"""
Login to LinguaLeo.
"""
url = 'http://lingualeo.ru/ru/login'
data = {'email': email, 'password': password}
Variables.r = requests.post(url=url, data=data)
Variables.cookie = {'servid': (Variables.r.cookies.extract_cookies.im_self.get('servid')),
"userid": "3299",
"AWSELB": (Variables.r.cookies.extract_cookies.im_self.get('AWSELB')),
"remember": "e30c0000af17f67875ab20ad59d1ed6aa5c8161a4876d31e25ee67f1996883d45e06b1058abdfa0d"}
def ask_leo(word):
"""
Do the query to LinguaLeo with chosen word and return value to function make_word.
"""
if ' ' in word:
link = 'http://api.lingualeo.com/gettranslates?word=' + (word.split(' '))[0] + '%20' + (word.split(' '))[1]
else:
link = 'http://api.lingualeo.com/gettranslates?word=' + word
hit = requests.get(url=link, cookies=Variables.cookie)
Variables.w = word
return make_word(hit.text)
def get_example(word):
"""
Getting example for the word.
"""
url = "http://www.oxforddictionaries.com/search/all/?direct=1&multi=1&q=" + word
page = requests.get(url)
if findall('<em class="example">(.*?)</em>', page.text) == []:
raw = findall('<li class="sentence">(.*?)</li>', page.text)[0]
else:
raw = findall('<em class="example">(.*?)</li>', page.text)[0]
Variables.example = sub('<[^<]+?>', '', raw)
return Variables.example
def make_word(so):
"""
Assemble the word.
"""
transcription = ((so.split(',')[-4]).split(':')[1]).encode('utf-8')
translation = (so.split(',')[7]).split(":")[1]
picture = (((so.encode('ascii', 'replace')).split(',')[5]).split('"')[3]).replace('\\', '')
sound = (((so.encode('ascii', 'replace')).split(',')[-1]).split('"')[3]).replace('\\', '')
download_file(sound)
download_file(picture)
return save_files(Variables.w, transcription.split('"')[1], translation.split('"')[1], picture.split('/')[-1],
sound.split('/')[-1], get_example(Variables.w))
def download_file(file):
"""
Downloading image and audio files.
"""
Variables.r = requests.get(file)
with open('C:\\Users\\Dmitriy\\Documents\\Anki\\fish\\collection.media\\' + file.split('/')[-1], "wb") as code:
code.write(Variables.r.content)
def save_files(word, transcription, translation, picture, sound, defenition):
"""
Saving values into file for next import to Anki.
"""
with open('C:\\Users\\Dmitriy\\Documents\\Anki\\fish\\' + Variables.filename + '.txt', 'a') as handle:
handle.write(
'\n' + word + '\t' + transcription + '\t' + translation + '\t' + '<img src="' + picture + '">' + '\t' + '[sound:' + sound + ']' + '\t' + defenition)
def instruction():
"""
Writes instruction after installation.
"""
print '------' * 10
print ('Чтобы произвести импорт карточек в Anki:\n'
'1) Запустите Anki.\n'
'2) В меню выберите Файл-Импортировать.\n'
'3) В меню выберите файл который вы сохранили вначале.')
def login_words_from_text():
cookie = {'login': 'fishhead', 'password': 'pass'}
link = 'http://wordsfromtext.com/save/login/'
header = {'POST': '/save/login/ HTTP/1.1', 'User-Agent': 'Mozilla/5.0', 'Content-Type': 'multipart/form-data',
'Referer': 'http://wordsfromtext.com/login/'}
site = requests.post(url=link, cookies=cookie, headers=header)
def main():
"""
Main entry point for the script.
"""
start()
login(Variables.mail, Variables.password)
create_wordslist()
for word in words:
ask_leo(word)
instruction()
if __name__ == '__main__':
sys.exit(main())