updated on Mon Jan 16 00:01:41 UTC 2012
[aur-mirror.git] / pymultitran / pymultitran.py
blob016fb5fae4a48ba0183c6e06dff75dc3fe2d0f19
1 #!/usr/bin/env python3
3 """
4 Copyright (c) 2010, Dmitry
5 All rights reserved.
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.
14 """
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):
33 self.value = value
34 def __str__(self):
35 return repr(self.value)
37 def ltrunc( page, sub ):
39 pos = page.find(sub)
40 if pos < 0:
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">&nbsp;<a href="#start"><FONT SIZE=2>в начало</FONT></a></td></tr></table>"""
50 end = page.find(endsign)
52 page = page[:end]
54 return page
56 def read_meanings( page ):
58 first = True
60 while 1:
61 try:
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>")]
71 if first:
72 first = False
73 else:
74 print("")
76 print("%s=== %s%s %s(%s%s%s) ===" % (BAR_COLOUR,
77 WORD_COLOUR, word,
78 BAR_COLOUR,
79 PARTICLE_COLOUR, particle_type,
80 BAR_COLOUR))
82 except TruncationException:
83 pass
85 try:
86 page = ltrunc(page, "<td > <a title=\"")
87 except TruncationException:
88 break
90 page = ltrunc(page, "<i>")
92 closing_pos = page.find("</i>")
93 meaning_type = page[:closing_pos]
95 synonym_words = []
97 page = ltrunc(page, "</i> </a>\r\n</td>")
100 while 1:
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>")
108 if page[0] != ";":
109 break
111 print(" %s%8s %s%s" % (MEANING_COLOUR, meaning_type,
112 SYNONYM_COLOUR, ", ".join(synonym_words)))
114 if len(argv) < 2:
115 print("Usage: %s word" % argv[0])
116 exit(1)
118 word = argv[1]
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("&nbsp;", " ")
128 page = page.replace("<span STYLE=&#34;color:gray&#34;>", GREY_COLOUR)
129 page = page.replace("<span STYLE=&#34;color:black&#34;>", DEFAULT_COLOUR)
130 read_meanings(page)