#!/usr/bin/env python3

import argparse
from urllib.request import urlopen
from urllib.parse import quote_plus
from bs4 import BeautifulSoup
import sys

if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("word")
    args = ap.parse_args()

    response = urlopen(f"https://pl.bab.la/slownik/angielski-polski/{quote_plus(args.word)}")
    soup = BeautifulSoup(response, "lxml")

    translations = []

    try:
        content_container = soup.findAll("div", attrs={"class": "quick-results container"})[0]
        for ul in content_container.findAll("ul", attrs={"class": "sense-group-results"}):
            for a in ul.findAll("a"):
                translations.append(a.text)
    except IndexError:
        print("uhh")

    if not translations:
        sys.exit(1)

    for t in translations:
        print(t)
