Skip to content

Instantly share code, notes, and snippets.

@ZiTAL
Last active January 11, 2024 07:55
Show Gist options
  • Save ZiTAL/fce9ec9bbd987fa1f549c8719a35909e to your computer and use it in GitHub Desktop.
Save ZiTAL/fce9ec9bbd987fa1f549c8719a35909e to your computer and use it in GitHub Desktop.
python: elhuyar-i hitzak itzultzeko eskatzeko script-a
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
import requests
import re
from lxml import html
def printError(search):
print("Errorea gertatu da, ziur aski sartu duzun hitza ez da hiztegian agertzen")
if search[-1] == 'a':
print("Bide batez, beharbada sartu duzun hitzak ez du a itsatsia...")
url="https://hiztegiak.elhuyar.eus"
try:
search=sys.argv[1]
except:
print("Erabilera: python3 elhuyar.py {hitza} {sartutako hitzaren hizkuntza: defektuz eu}")
print("Adibidez:")
print("\tpython3 elhuyar.py txori")
print("\tpython3 elhuyar.py txori eu")
print("\tpython3 elhuyar.py pajaro es")
sys.exit(1)
try:
lang=sys.argv[2]
except:
lang = "eu"
if lang not in ["eu", "es"]:
lang = "eu"
if lang == 'eu':
url_lang = 'eu_es'
else:
url_lang = 'es_eu'
try:
url = f"{url}/{url_lang}/{search}"
response = requests.get(url)
dom = html.fromstring(response.content)
uls = dom.xpath("//ul[contains(@class, 'hizkuntzaren_arabera')]")
result = ""
i = 0
for ul in uls:
ori = dom.xpath("//div[contains(@class, 'emaitza-lerroa')]/h1/span")[i].text_content()
result = result + f"{ori}:\n"
lis = ul.xpath('li')
for li in lis:
t = li.xpath("p[@class='lehena']")[0].text_content().strip()
t = re.sub("\s+", ' ', t)
result = result + f"\t{t}\n"
i = i + 1
if result != '':
result = f"Bilatutakoa: {search}\n"+result
print(result)
sys.exit(0)
else:
printError(search)
sys.exit(1)
except SystemExit as sysexit:
sys.exit(sysexit.code)
@ZiTAL
Copy link
Author

ZiTAL commented Jan 10, 2024

adibidez:

python3 elhuyar.py txori
Bilatutakoa: txori
1 txori:
	1 iz. pájaro
	2 iz. tupé, copete
	3 iz. lazo, cinta anudada de adorno
2 txori:
	iz. (Med.) lobanillo, tumor enquistado
python3 elhuyar.py pajaro es
Bilatutakoa: pajaro
pájaro:
	1 s.m. txori
	2 s.m. (fig.) [persona] maltzur, azeri, zakur; ale, elementu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment