Initial import of vt. - vt - Print definitions from Vocabolario Treccani
(HTM) hg clone https://bitbucket.org/iamleot/vt
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
(DIR) changeset d0743819002c3b8cd4875e3568fb1280c6528e68
(HTM) Author: Leonardo Taccari <iamleot@gmail.com>
Date: Mon, 1 Jan 2018 01:17:12
Initial import of vt.
Diffstat:
vt.py | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 101 insertions(+), 0 deletions(-)
---
diff -r 000000000000 -r d0743819002c vt.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/vt.py Mon Jan 01 01:17:12 2018 +0100
@@ -0,0 +1,101 @@
+#!/usr/pkg/bin/python3.6
+
+#
+# Copyright (c) 2017 Leonardo Taccari
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 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.
+#
+# 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.
+#
+
+
+from urllib import parse, request
+import textwrap
+
+from bs4 import BeautifulSoup, SoupStrainer
+
+
+def lookup_term(term):
+ """Lookup a term in the Treccani's Vocabolario
+
+ Given a term try to search it in Vocabolario Treccani.
+
+ Returns the relevant text output of the term or None if nothing relevant
+ was found.
+ """
+ url = 'http://www.treccani.it/vocabolario/' + parse.quote(term)
+
+ req = request.Request(url)
+ req.add_header('User-Agent', 'Mozilla/5.0')
+ with request.urlopen(req) as r:
+ t = BeautifulSoup(r, 'html.parser', parse_only=SoupStrainer(class_='text spiega'))
+
+ return t.p.text if t.p else None
+
+
+def terms(term):
+ """Return a list of terms in the Treccani's Vocabolario
+
+ Given a term return a list of zero or more definition regarding the
+ term.
+ """
+ ts = []
+
+ t = lookup_term(term)
+ if t:
+ ts.append(t)
+ else:
+ i = 1
+ while True:
+ t = lookup_term(term + str(i))
+ if not t:
+ break
+ ts.append(t)
+ i += 1
+
+ return ts
+
+
+def print_term(term):
+ """Pretty print a definition"""
+ print(textwrap.fill(term, width=80, break_long_words=False,
+ break_on_hyphens=False).strip())
+
+
+if __name__ == '__main__':
+ import sys
+
+ def usage():
+ print('usage: {} term'.format(sys.argv[0]))
+ sys.exit(1)
+
+ if len(sys.argv) != 2:
+ usage()
+
+ term = sys.argv[1]
+
+ first = True
+ for t in terms(term):
+ if not first:
+ print()
+ print_term(t)
+ first = False