-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
191 lines (155 loc) · 5.98 KB
/
main.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# Basic imports
import webbrowser
import sys
import os
import logging
import requests
import winsound
from PIL import Image
import time
import threading
from multiprocessing import Process
import json
# Third-party imports
import openai
import easygui
from bs4 import BeautifulSoup
# -- Kivy
import kivy
from kivy.app import App
from kivy.clock import Clock
from kivy.config import Config
from kivy.properties import NumericProperty
Config.set('graphics', 'resizable', False)
Config.set('graphics', 'width', '600')
Config.set('graphics', 'height', '400')
Config.set('input', 'mouse', 'mouse,multitouch_on_demand')
kivy.require('2.1.0')
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import ScreenManager, Screen
# -- Others
import pickle
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.animation import Animation
# METHODS
from methods.initialization import Initialize
# Screens
class StartWindow(Screen):
pass
class ResultsWindow(Screen):
pass
class LoadingScreen(Screen):
pass
class WindowManager(ScreenManager):
pass
# KV Files
kv = Builder.load_file('Insight.kv')
class SearchRoutine:
def __init__(self):
self.description = None
self.keyword = None
self.images = [False, False]
def run(self, query, top):
self.move_screens("loading", top)
def construct(inner):
self.build(query, top)
def init(inner):
routine_thread = Process(target=self.search_routine(query=query, top=top))
routine_thread.start()
routine_thread.join()
Clock.schedule_once(construct, 5)
Clock.schedule_once(init, 5)
def build(self, query, top):
logging.info(f"[Interface ] Building...")
top.root.ids.summary.text = self.description
top.root.ids.image1.reload()
top.root.ids.image2.reload()
def move_screens(self, target, top):
top.root.current = f"{target}"
def google_search(self, query):
with requests.session() as session:
# Insight gets a Google result
url = f"""https://www.google.com/search?q="{query}"
"""
headers = {
"referer": "referer: https://www.google.com/",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/89.0.4389.114 Safari/537.36"
}
session.post(url, headers=headers)
response = session.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
company_url = soup.find('cite').text
return company_url
def verify_company(self, target):
with requests.session() as session:
url = f"{target}/"
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/89.0.4389.114 Safari/537.36"
}
session.post(url, headers=headers)
response = session.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
page_text = soup.get_text()
page_images = soup.find_all("img", limit=2, src=True)
for index, image in enumerate(page_images):
logging.info(f"[Search Callback Function ] Getting images...")
if page_images[index]['src'].startswith("http"):
r = requests.get(f"{page_images[index]['src']}", headers=headers)
else:
r = requests.get(f"{url}{page_images[index]['src']}", headers=headers)
r.raise_for_status()
r.raw.decode_content = True
if r.status_code == 200:
with open(f"widgets/images/image{index + 1}.jpg", "wb") as f:
f.write(r.content)
context = openai.chat.completions.create(
model="gpt-3.5-turbo",
max_tokens=256,
messages=[
{"role": "system",
"content": ""},
{"role": "user",
"content": f"Look at this page and tell me what this company is about in one SMALL paragraph. Make it short."
f"PAGE: {page_text}"}
]
)
self.description = context.choices[0].message.content
def search_routine(self, query, top):
# Make a Google search from Query and use Query as target
# ++ Verification of competitor's website
with requests.session() as session:
self.verify_company(self.google_search(query))
# titles = soup.findAll('h3')
# titles_url = [element for element in soup.findAll('a', href=True)]
# titles = [element.get_text() for element in titles]
# for i in range(0, len(titles) - 2):
# print(titles[i], titles_url[i]['href'])
time.sleep(3)
self.move_screens("results", top)
class InsightApp(App):
def build(self):
self.title = 'Kairos Praxis: Competitor Analysis'
return kv
def search_callback(self):
logging.info(f"[Search Callback Function ] Running...")
query = self.root.ids.searchbar.text
query = query.replace(' ', '+')
logging.info(f"[Search Callback Function ] Query set as {query}...")
SearchRoutine().run(query, self)
if __name__ == '__main__':
try:
initialization_routine = threading.Thread(target=Initialize().run())
initialization_routine.start()
initialization_routine.join()
InsightApp().run()
except Exception as e:
print(e)