-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedical_checker.py
More file actions
288 lines (256 loc) · 10.5 KB
/
Copy pathmedical_checker.py
File metadata and controls
288 lines (256 loc) · 10.5 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import requests
from bs4 import BeautifulSoup
import psycopg2
from learn_env.sergeyGit.config import DB_PWD, new_bad_list
class Tracker():
def __init__(self, url='https://tabletki.ua'):
self.url = url
# Function make_link() returning link depend of u_choice (user choice) and of selr.url.
def make_link(self, u_choice):
link = self.url + '/' + u_choice + '/'
return link
def get_html(self, url):
r = requests.get(url)
return r.text
# div class='goog-trans-section' exist only in case when u_choice pills was founded in html.
# Function need to check is correct user input (u_choice) or not.
def check_is_exist(self, u_choice):
soup = BeautifulSoup(self.get_html(self.make_link(u_choice)), "html.parser")
check = soup.find_all('div', class_='goog-trans-section')
if check:
print('True')
return True
else:
print('False')
return False
#Function get_main_list collects all elements with necessarry id's and search link with image, returning list
def get_main_list(self, html):
soup = BeautifulSoup(html, "html.parser")
list = []
names = soup.find('div', class_='goog-trans-section')
global all_el
try:
all_el = names.find_all(class_=None)
except AttributeError:
print('Не корректное название лекарства')
return False
for i in range(0,len(all_el)):
if all_el[i] == soup.find('h2', id='Состав'):
list.append(i)
elif all_el[i] == soup.find('h2', id='Показания'):
list.append(i)
elif all_el[i] == soup.find('h2', id='Противопоказания'):
list.append(i)
elif all_el[i] == soup.find('h2', id='Способ_применения_и_дозы'):
list.append(i)
elif all_el[i] == soup.find('h2', id='Передозировка'):
list.append(i)
elif all_el[i] == soup.find('h2', id='Побочные_эффекты'):
list.append(i)
else:
pass
image_obj = soup.find('div', class_='swiper-wrapper')
try:
image_link = 'http:' + image_obj.find('img').get('src')
obj_list = [list, image_link]
except AttributeError:
print('Not image for this pharmacy')
obj_list = [list, 'no_image_links']
return obj_list
#Function get_msg_substance find and returning all elements that apply to different group about piils (like substance,
#affects etc)
def get_msgs_all(self, list):
substance = list[0] #getting indexes of all necessarry tags (like <h2>)
indications = list[1]
anti_indications = list[2]
method_to_eat = list[3]
overdose = list[4]
try:
affects = list[5]
except:
affects = list[4]
list_substance =[]
z = substance
while z < (indications-1):
z = z+1
msg = all_el[z].text #getting text from all elements between two tags (this text which mean all substance)
if msg in new_bad_list: #here deleting are repetitive strings
pass
else:
list_substance.append(msg.replace("'","")+'\n')
msg_substance = []
for i in list_substance:
if i not in msg_substance:
msg_substance.append(i)
msg_substance = ''.join(msg_substance) # creating new unique message about substance of pill which user choose
list_indications = []
x = indications -1
while x < anti_indications -1:
x = x+1
msg1 = all_el[x].text
list_indications.append(msg1.replace("'","") + '\n')
msg_indications = []
for i in list_indications:
if i not in msg_indications:
msg_indications.append(i)
msg_indications = ''.join(msg_indications)
list_anti_indications = []
c = anti_indications -1
while c < method_to_eat -1:
c = c+1
msg2 = all_el[c].text
if msg2 in new_bad_list:
pass
else:
list_anti_indications.append(msg2.replace("'","")+'\n')
msg_anti_indications = []
for i in list_anti_indications:
if i not in msg_anti_indications:
msg_anti_indications.append(i)
msg_anti_indications = ''.join(msg_anti_indications)
list_meth_eat = []
v = method_to_eat -1
while v < overdose -1:
v = v+1
msg3 = all_el[v].text
if msg3 in new_bad_list:
pass
else:
list_meth_eat.append(msg3.replace("'","")+'\n')
msg_meth_eat = []
for i in list_meth_eat:
if i not in msg_meth_eat:
msg_meth_eat.append(i)
msg_meth_eat = ''.join(msg_meth_eat)
list_overdose = []
v = overdose -1
while v < affects - 1:
v = v+1
msg = all_el[v].text
if msg in new_bad_list:
pass
else:
list_overdose.append(msg.replace("'","")+'\n')
msg_overdose = []
for i in list_overdose:
if i not in msg_overdose:
msg_overdose.append(i)
msg_overdose = ''.join(msg_overdose)
list_affects = []
b = affects -1
while b < len(all_el) -13:
b = b+1
msg4 = all_el[b].text
if msg4 in new_bad_list:
pass
else:
list_affects.append(msg4.replace("'","")+'\n')
msg_affects = []
for i in list_affects:
if i not in msg_affects:
msg_affects.append(i)
msg_affects = ''.join(msg_affects)
result_dict = { #Creating dictionary with all messages about user choice
'substance':msg_substance,
'indications': msg_indications,
'anti_indications': msg_anti_indications,
'method_eat': msg_meth_eat,
'affects': msg_affects,
'overdose': msg_overdose
}
return result_dict
#Before start parsing the donor site, trying to search info in database (PostgeSQL).If exist - returning True
def check_exist_database(self, u_choice):
try:
con = psycopg2.connect(host='localhost', user='sergeymoroz', password=DB_PWD, database='test1')
except Exception as e:
print(e)
C = con.cursor()
C.execute("select name from pharm1 where name = '{name}'".format(name=u_choice))
if len(C.fetchall()) > 0:
return True
else:
return False
#Returning information from database about user choice (previously checking if exist)
def get_db_data(self, u_choice):
try:
con = psycopg2.connect(host='localhost', user='sergeymoroz', password=DB_PWD, database='test1')
except Exception as e:
print(e)
C = con.cursor()
C.execute(
"select substance, indications, anti_indications, method_eat, affects, imagelink from pharm1 "
"where name = '{name}'".format(name=u_choice))
rows = C.fetchone()
return rows
#Here final message is formed. First checking if user choice exist in database, if yes - forming dictionary from
#database. If not - forming message dictionary from parsed message(get_msgs_all()) and added to database.
def get_msg_bot(self, u_choice):
if self.check_exist_database(u_choice) == True:
print('Connecting to database')
rows = self.get_db_data(u_choice)
msg_substance = rows[0]
msg_indications = rows[1]
msg_antiindications = rows[2]
msg_method_eat = rows[3]
msg_affects = rows[4]
image_link = rows[5]
rows_msg = {
'substance':rows[0],
'indications':rows[1],
'anti_indications':rows[2],
'method_eat':rows[3],
'affects':rows[4],
'imagelink':rows[5],
}
print(rows_msg['substance'])
return rows_msg
else:
obj_list = self.get_main_list(self.get_html(self.make_link(u_choice)))
list = obj_list[0]
print(obj_list)
image_link = obj_list[1]
msg = self.get_msgs_all(list)
msg_substance = msg['substance']
msg_indications = msg['indications']
msg_antiindications = msg['anti_indications']
msg_method_eat = msg['method_eat']
msg_affects = msg['affects']
msg_overdose = msg['overdose']
try:
con = psycopg2.connect(host='localhost', user='sergeymoroz', password=DB_PWD, database='test1')
except Exception as e:
print(e)
C = con.cursor()
try:
C.execute("""insert into pharm1 (name,
substance,
indications,
anti_indications,
method_eat,
affects,
imagelink) VALUES ('{pharm_name}', '{substances}', '{indicat}', '{anti_ind}', '{meth_eat}', '{affect}', '{image}') returning id, name, imagelink""".format(
pharm_name=u_choice,
substances=str(msg['substance']),
indicat=str(msg['indications']),
anti_ind=str(msg['anti_indications']),
meth_eat=str(msg['method_eat']),
affect=str(msg['affects']),
image=image_link))
con.commit()
rowss = C.fetchall()
print(rowss)
except psycopg2.ProgrammingError as e:
print (e)
print('Cant add to database')
dict_to_return = {
'substance': msg['substance'],
'indications': msg['indications'],
'anti_indications': msg['anti_indications'],
'method_eat': msg['method_eat'],
'affects': msg['affects'],
'imagelink': image_link,
}
return dict_to_return
# track = Tracker()
# track.get_msg_bot('Омез')