-
Notifications
You must be signed in to change notification settings - Fork 1
/
cambridge.py
66 lines (57 loc) · 1.98 KB
/
cambridge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import requests
from bs4 import BeautifulSoup
import sys
import sqlite3
import os
if not os.path.isfile("database.db"):
exists = False
else:
exists = True
conn = sqlite3.connect("database.db")
c = conn.cursor()
if exists == False:
c.execute('''CREATE TABLE dictionary(word TEXT, meaning TEXT)''')
headers = {
"Accept-Encoding": "gzip, deflate, br",
"Upgrade-Insecure-Requests": "1",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8",
"Sec-Gpc": "1",
"Accept-Language": "en-US,en;q=0.6",
"Sec-Fetch-Site": "same-origin",
"Sec-Fetch-User": "?1",
"Sec-Fetch-Dest": "document",
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36",
"Sec-Fetch-Mode": "navigate"
}
word = ' '.join([str(elem) for elem in sys.argv[1:]])
c.execute('''SELECT * FROM dictionary WHERE word = ?''', (word,))
entry = c.fetchone()
def cambridge(word):
url = "https://dictionary.cambridge.org/dictionary/english/" + word
response = requests.get(url, headers=headers)
remove = ["A1", "A2", "B1", "B2", "C1"]
soup = BeautifulSoup(response.text, "html.parser")
print(f'{soup.find(class_="ti fs fs12 lmb-0 hw superentry").get_text()}:')
num = 0
meaning_ = ""
for item in soup.select(".ddef_h"):
num += 1
a = item.get_text().lstrip(" ").rstrip(": ").split()
a_list = [word for word in a if word not in remove]
actual = ' '.join(a_list)
meaning_ = meaning_ + f'{num}. {actual}\n'
print(meaning_)
c.execute('''INSERT INTO dictionary VALUES(?,?)''', (word, meaning_))
def indb(word):
if entry:
print(f"Meaning of {entry[0]} in English: ")
print(entry[1])
else:
cambridge(word)
try:
indb(word)
except:
print("Something went wrong!")
print("Possibly the word you entered is not in the dictionary or there's a connection issue")
conn.commit()
conn.close()