-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsymptoms.py
149 lines (119 loc) · 4.76 KB
/
symptoms.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
import apidemic
import scrapper
# import database
import config
import json
import requests
import ast
class HealthCare:
def __init__ (self):
'''
Main Singleton object to connect with apimedic API and respond all 5 APIs endpoints
'''
# Retrieve username
username = config.username
# Retrieve password
password = config.password
authUrl = config.priaid_authservice_url
healthUrl = config.priaid_healthservice_url
language = config.language
self._printRawOutput = config.pritnRawOutput
# APIMEDIC Diagnosis Client
self._diagnosisClient = apidemic.DiagnosisClient(username, password, authUrl, language, healthUrl)
# List of all available symptoms from Apimedic API.
self.availableSymptoms = self._diagnosisClient.loadSymptoms()
# Client for database transactions and requests
# self._databaseClient = database.Database()
def getSymptoms (self):
'''
Return dictionary of all (ID, Name) pairs from apimedic api.
'''
result = dict()
for symptom in self.availableSymptoms:
result[symptom['ID']] = symptom['Name']
return result
def getDiseasefromSymptoms (self, id, gender, year):
'''
Params:
id: IDs of all possible symptoms to patient concatenated by '+'
gender: Gender of patient (male/female)
Year: DOB Year of patient
'''
gender = gender.lower()
# Replace + with space
id = id.replace('+', ' ')
ids = id.split()
# Check if all ids are integer
for num in ids:
if not num.isdigit():
print('a' + num)
return {
"status": "error",
"error": "ID should be Integers"
}
# Check if gender is valid
if gender not in ['male', 'female']:
return {
"status": "error",
"error": "Gender should be either male or female"
}
# Check if year is valid
if not year.isdigit():
return {
"status": "error",
"error": "Invalid Year"
}
# List of all possible diseases
user_diagnosis = self._diagnosisClient.loadDiagnosis(id, gender, year)
# Chose most probable (accurate) disease
profName = user_diagnosis[0]['Issue']['ProfName']
simpleName = user_diagnosis[0]['Issue']['Name']
result = {
"status": "ok",
"profName": profName,
"simpleName": simpleName
}
return result
def getConditonfromDisease(self, disease):
disease = disease.lower()
# Search for disease condition's in database.
# db_result = self._databaseClient.search(disease)
db_result = None
if db_result:
result = db_result
else:
# If not found in database, Scrap the info from Google Search
result = scrapper.GoogleScrapper.getTreatment(disease)
if result:
# Put info in database for futuee use
# self._databaseClient.insert(result['disease'], result['details'],
# result['selfcare'], result['medications'], result['specialists'])
pass
result['status'] = 'ok'
return result
def getConditionfromText(self, text):
found_symptoms = ""
text = text.replace(' ', '+')
url = 'https://fathomless-ridge-45332.herokuapp.com/text={}'.format(text)
USER_AGENT = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'}
result = requests.get(url, headers=USER_AGENT)
page = ast.literal_eval(result.text)
# Concatenate all symptoms present in text
for symptom in self.availableSymptoms:
for el in page:
if symptom['Name'].lower() == el["label"].lower():
found_symptoms += ' ' + str(symptom['ID'])
found_symptoms = found_symptoms.strip()
# If no system is found, return error
if len(found_symptoms) == 0:
return {
'status': 'error',
'error': 'No Symptom matched with given text.'
}
# Check disease from symptoms (Gender default to male, Year default to 1998)
disease = self.getDiseasefromSymptoms(found_symptoms, 'male', '1998')
result = self.getConditonfromDisease(disease['profName'])
return result
def getNearestDoctor(self):
# Scrap Google for nearest doctors from Location
return scrapper.GoogleScrapper.getDoctor()