-
Notifications
You must be signed in to change notification settings - Fork 1
/
hqbot.py
144 lines (109 loc) · 4.39 KB
/
hqbot.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import sys
import tkinter as tk
import os
import ocr
import re
import pyscreenshot as ImageGrab
from bs4 import BeautifulSoup
from colorama import Fore, Style
from PyQt5 import QtWidgets, QtCore, QtGui
from urllib.request import Request, urlopen
class HQBotWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
root = tk.Tk()
self.setWindowTitle('HQBot')
# Calculate the size of the screen that the script is being executed on
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
self.setGeometry(0, 0, screen_width, screen_height)
self.drag_start = QtCore.QPoint()
self.drag_end = QtCore.QPoint()
self.setWindowOpacity(0.3)
# Override our system cursor with the QT cursor
QtWidgets.QApplication.setOverrideCursor(
QtGui.QCursor(QtCore.Qt.CrossCursor)
)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.show()
def paintEvent(self, event):
qp = QtGui.QPainter(self)
qp.setPen(QtGui.QPen(QtGui.QColor(255, 100, 100), 1))
qp.setBrush(QtGui.QColor(200, 100, 100))
qp.drawRect(QtCore.QRect(self.drag_start, self.drag_end))
def mousePressEvent(self, event):
self.drag_start = event.pos()
# Set the drag_end position to be equal to our start position so that the rectangle appears to be 1px on initial press
self.drag_end = self.drag_start
self.update()
def mouseMoveEvent(self, event):
self.drag_end = event.pos()
self.update()
def mouseReleaseEvent(self, event):
self.close()
x1 = min(self.drag_start.x(), self.drag_end.x())
y1 = min(self.drag_start.y(), self.drag_end.y())
x2 = max(self.drag_start.x(), self.drag_end.x())
y2 = max(self.drag_start.y(), self.drag_end.y())
img = ImageGrab.grab(bbox=(x1, y1, x2, y2))
img.save('capture.png')
text = ocr.readImg()
print('TEXT', text)
self.buildQAndA(text)
def buildQAndA(self, text):
print('text', text)
lines = text.splitlines()
question = ''
questionAsked = False
answers = list()
for line in lines:
line = line.lower()
if line == '':
continue
if not questionAsked:
question += f'{line} '
if '?' in line:
questionAsked = True
continue
if questionAsked:
answers.append(line)
print(f'{Fore.GREEN}Q: {Fore.WHITE}{question}')
for index, answer in enumerate(answers):
print(f'{index}: {answer}')
answer_dict = { key: 0 for key in answers }
question_params = '+'.join(question.split(' '))
url = f'https://www.google.co.uk/search?q={question_params}'
self.search(question, answers, answer_dict, url, recursive = True)
def search(self, question, answers, answer_dict, url, recursive = False):
try:
# print('url', url)
# Set request Headers to avoid 403 errors
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
uClient = urlopen(req)
search_html = uClient.read()
uClient.close()
soup = BeautifulSoup(search_html, 'html.parser')
parsed_text = soup.get_text().lower()
# answer_dict = { key: 0 for key in answers }
for key in answer_dict:
answer_dict[key] += parsed_text.count(key)
# if recursive == True:
# for link in soup.findAll('a'):
# print('link', link.get('href'))
# test = link
# try:
# test = link['href'].split('/url?q=')[1]
# print('test', test)
# except Exception as e:
# print('split test', e)
# self.search(question, answers, answer_dict, test, recursive = False)
# print('soup text', soup)
print('dict', answer_dict)
except Exception as e:
print('search error', e)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = HQBotWidget()
window.show()
app.aboutToQuit.connect(app.deleteLater)
sys.exit(app.exec_())