4 Copyright (c) 2010, Dmitry
7 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
9 1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
10 2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
11 3) Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
13 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16 REMOTE_ENCODING
= "cp1251"
18 DEFAULT_COLOUR
= "\x1b[0;0m"
19 GREY_COLOUR
= "\x1b[0;37;m"
21 BAR_COLOUR
= DEFAULT_COLOUR
22 MEANING_COLOUR
= "\x1b[3;32m"
23 SYNONYM_COLOUR
= "\x1b[0;0m"
24 WORD_COLOUR
= "\x1b[1m"
25 PARTICLE_COLOUR
= "\x1b[0;36m"
27 from sys
import argv
, exit
28 from urllib
.request
import urlopen
29 from urllib
.parse
import urlencode
31 class TruncationException(Exception):
32 def __init__(self
, value
):
35 return repr(self
.value
)
37 def ltrunc( page
, sub
):
41 raise TruncationException("Necessary html sequence is not found.")
43 return page
[pos
+ len(sub
):]
45 def remove_crap( page
):
47 page
= ltrunc(page
, """document.translation.s.select()\r\ndocument.translation.s.focus()""")
49 endsign
= """|<span STYLE="color:black"> <a href="#start"><FONT SIZE=2>в начало</FONT></a></td></tr></table>"""
50 end
= page
.find(endsign
)
56 def read_meanings( page
):
62 page
= ltrunc(page
, "<td bgcolor=\"#DBDBDB\" colspan=\"2\"> <a href=\"")
63 page
= ltrunc(page
, "\">")
65 word
= page
[:page
.find("</a>")]
67 page
= ltrunc(page
, "<em>")
69 particle_type
= page
[:page
.find("</em>")]
76 print("%s=== %s%s %s(%s%s%s) ===" % (BAR_COLOUR
,
79 PARTICLE_COLOUR
, particle_type
,
82 except TruncationException
:
86 page
= ltrunc(page
, "<td > <a title=\"")
87 except TruncationException
:
90 page
= ltrunc(page
, "<i>")
92 closing_pos
= page
.find("</i>")
93 meaning_type
= page
[:closing_pos
]
97 page
= ltrunc(page
, "</i> </a>\r\n</td>")
101 page
= ltrunc(page
, "<a href=\"m.exe?t=")
102 page
= ltrunc(page
, "\">")
104 synonym_words
.append(page
[:page
.find("<")])
106 page
= ltrunc(page
, "</a>")
111 print(" %s%8s %s%s" % (MEANING_COLOUR
, meaning_type
,
112 SYNONYM_COLOUR
, ", ".join(synonym_words
)))
115 print("Usage: %s word" % argv
[0])
120 args
= urlencode([("l1", "1"), ("l2", "2"), ("s", word
)],
121 encoding
= REMOTE_ENCODING
)
122 fp
= urlopen('http://www.multitran.ru/c/m.exe?%s' % args
)
124 page
= fp
.read().decode(REMOTE_ENCODING
)
126 page
= remove_crap(page
)
127 page
= page
.replace(" ", " ")
128 page
= page
.replace("<span STYLE="color:gray">", GREY_COLOUR
)
129 page
= page
.replace("<span STYLE="color:black">", DEFAULT_COLOUR
)