def get(self,keyword, state, format='json'): #set content-type self.response.headers['Content-Type'] = Formatter.contentType(format) returnData = MutableString() returnData = '' try: #get XML from the service state = "%s%s" % ("NA%3AUS%3A", state) xml = minidom.parse(urllib.urlopen('%s&term=%s&state1=%s' % (AppConfig.clinicalTrialsAPIURL, keyword, state.upper()))) if (xml): documentElms = xml.getElementsByTagName('clinical_study') if (documentElms): for docNode in documentElms: content = APIUtils.parseClinicalTrialsContent(format,docNode) returnData += content else: logging.error('unable to retrieve content') self.response.out.write(Formatter.error(format, 'No results')) return else: logging.error('unable to retrieve content') self.response.out.write(Formatter.error(format, 'Unable to retrieve content from provider')) return except Exception, e: logging.error('GetClinicalTrialsHandler: unable to get health topics or parse XML: %s' % e) self.response.out.write(Formatter.error(format, 'Exception: %s' % (e))) return
def post(self): #set content-type format = "json" formatParamName = "format" if (formatParamName in self.request.params): format = self.request.params[formatParamName] self.response.headers['Content-Type'] = Formatter.contentType(format) #get content textToTranslate = "" textParamName = "data" if (textParamName in self.request.params): textToTranslate = self.request.params[textParamName] if (textToTranslate == None or textToTranslate == ''): logging.error('GetTranslateHandler: invalid parameters') self.response.out.write(Formatter.error(format, 'Invalid parameters')) return #strip tags? stripTagsParam = "striphtml" if (stripTagsParam in self.request.params): if (self.request.params[stripTagsParam] == 'true'): textToTranslate = CharReplacementMap.remove_html_tags(textToTranslate) #language toLanguage = "es" lanParam = "language" if (textParamName in self.request.params): toLanguage = self.request.params[lanParam] #auto-detect language (blank) sourceLanguage = '' params = ({'langpair': '%s|%s' % (sourceLanguage, toLanguage), 'v': '1.0' }) returnData = MutableString() translatedText = '' for textToTranslate in self.getSplits(textToTranslate): params['q'] = textToTranslate resp = simplejson.load(urllib.urlopen('%s' % (AppConfig.googleTranslateAPIURL), data = urllib.urlencode(params))) try: translatedText += resp['responseData']['translatedText'] except Exception, e: logging.error('GetTranslateHandler: error(s) translating data: %s' % e) self.response.out.write(Formatter.error(format, 'Exception: %s' % (e))) return
def get(self,keyword,format='json'): #set content-type self.response.headers['Content-Type'] = Formatter.contentType(format) returnData = MutableString() try: #get XML from the service xml = minidom.parse(urllib.urlopen('%s?db=healthTopics&term=%s' % (AppConfig.medlinePlusHealthTopicURL, keyword))) if (xml): documentElms = xml.getElementsByTagName('document') if (documentElms): #grab two highest ranked content content1 = '' content2 = '' content3 = '' contentNode1 = documentElms[0] content1 = APIUtils.parseHealthTopicContent(format,contentNode1) if (len(documentElms) > 1): contentNode2 = documentElms[1] content2 = APIUtils.parseHealthTopicContent(format,contentNode2) if (len(documentElms) > 2): contentNode3 = documentElms[2] content3 = APIUtils.parseHealthTopicContent(format,contentNode3) returnData = "%s%s%s" % (content1, content2, content3) else: logging.error('unable to retrieve content') self.response.out.write(Formatter.error(format, 'No results')) return else: logging.error('unable to retrieve content') self.response.out.write(Formatter.error(format, 'Unable to retrieve content from provider')) return except Exception, e: logging.error('GetHealthTopicsHandler: unable to get health topics or parse XML: %s' % e) self.response.out.write(Formatter.error(format, 'Exception: %s' % (e))) return
def get(self,code, lan = 'en', format='json'): #set content-type self.response.headers['Content-Type'] = Formatter.contentType(format) returnData = MutableString() try: #webservice conversion if (lan == 'es'): lan = 'sp' #get XML from the service xml = minidom.parse(urllib.urlopen('%s&mainSearchCriteria.v.c=%s&informationRecipient.languageCode.c=%s' % (AppConfig.medlinePlusHealthDiagnosisICD9URL, code, lan))) if (xml): documentElms = xml.getElementsByTagName('entry') if (documentElms): #grab two highest ranked content content1 = '' content2 = '' contentNode1 = documentElms[0] content1 = APIUtils.parseHealthDiagnosisICD9Content(format,contentNode1) if (len(documentElms) > 0): contentNode2 = documentElms[1] content2 = APIUtils.parseHealthDiagnosisICD9Content(format,contentNode2) returnData = "%s%s" % (content1, content2) else: logging.error('unable to retrieve content') self.response.out.write(Formatter.error(format, 'No results')) return else: logging.error('unable to retrieve content') self.response.out.write(Formatter.error(format, 'Unable to retrieve content from provider')) return except Exception, e: logging.error('GetHealthTopicsHandler: unable to get health topics or parse XML: %s' % e) self.response.out.write(Formatter.error(format, 'Exception: %s' % (e))) return
def get(self,id=None,format='json'): #set content-type self.response.headers['Content-Type'] = Formatter.contentType(format) #validate id if (not id or id == ''): self.response.out.write(Formatter.error(format, 'invalid id')) return #get the record q = db.GqlQuery("SELECT * FROM HospitalInfo WHERE hospital_id = :1", id) #call storage and build up result string results = q.fetch(1) returnData = MutableString() returnData = '' for p in results: if (p): returnData = "%s%s%s%s%s%s%s%s%s%s%s%s%s" % (Formatter.data(format, 'hospital_id', p.hospital_id), Formatter.data(format, 'name', p.name.replace('&', '&')), Formatter.data(format, 'address', p.address.replace('&', '&')), Formatter.data(format, 'city', p.city), Formatter.data(format, 'state', p.state), Formatter.data(format, 'zip_code', p.zip_code), Formatter.data(format, 'county', p.county), Formatter.data(format, 'phone', p.phone), Formatter.data(format, 'hospital_type', p.hospital_type.replace('&', '&')), Formatter.data(format, 'hospital_owner', p.hospital_owner.replace('&', '&')), Formatter.data(format, 'emergency_service', p.emergency_service), Formatter.data(format, 'geo_location', p.location), Formatter.data(format, 'geo_box', string.join(p.geoboxes, ",")) ) #output to the browser if (format == 'json'): self.response.out.write('{') self.response.out.write(returnData[:-1]) self.response.out.write('}') else: self.response.out.write('<?xml version="1.0"?>') self.response.out.write('<root>') self.response.out.write(returnData) self.response.out.write('</root>')
def get(self,zip=None,format='json'): #set content-type self.response.headers['Content-Type'] = Formatter.contentType(format) #validate zip if (not zip or zip == ''): self.response.out.write(Formatter.error(format, 'invalid zipcode')) return #get lat/lon from zipcode urlStr = 'http://maps.google.com/maps/geo?q=%s&output=json&sensor=true&key=%s' % (zip,AppConfig.googleMapsAPIKey) jsonData = UtilLib.reverseGeo(urlStr) #lets see if have jsonData from reverse geocoding call if (jsonData): lon = jsonData['Placemark'][0]['Point']['coordinates'][0] lat = jsonData['Placemark'][0]['Point']['coordinates'][1] logging.debug("GPS Coordinates: %s,%s" % (lat,lon)) gb = geobox2.Geobox(lat, lon) #scope 100 miles box = gb.search_geobox(100) query = HospitalInfo.all().filter("geoboxes IN", [box]) #get 100 records results = query.fetch(100) db_recs = {} for result in results: distance = UtilLib.getEarthDistance(lat, lon, result.location.lat, result.location.lon) if (distance and distance > 0): db_recs[distance] = result returnData = MutableString() returnData = '' #output this badboy if (db_recs and len(db_recs) > 0): for key in sorted(db_recs.iterkeys()): p = db_recs[key] if (format == 'json'): startTag = '{' endTag = '},' distance = Formatter.data(format, 'distance', '%s %s' % (str(math.ceil(key)), "mi"))[:-1]#'%.2g %s' % (key, "mi"))[:-1] else: startTag = '<record>' endTag = '</record>' distance = Formatter.data(format, 'distance', '%s %s' % (str(math.ceil(key)), "mi")) #build the string returnData = "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s" % (returnData,startTag, Formatter.data(format, 'hospital_id', p.hospital_id), Formatter.data(format, 'name', p.name.replace('&', '&')), Formatter.data(format, 'address', p.address.replace('&', '&')), Formatter.data(format, 'city', p.city), Formatter.data(format, 'state', p.state), Formatter.data(format, 'zip_code', p.zip_code), Formatter.data(format, 'county', p.county.replace('&', '&')), Formatter.data(format, 'phone', p.phone), Formatter.data(format, 'hospital_type', p.hospital_type.replace('&', '&')), Formatter.data(format, 'hospital_owner', p.hospital_owner.replace('&', '&')), Formatter.data(format, 'emergency_service', p.emergency_service), Formatter.data(format, 'geo_location', p.location), distance, endTag ) #output to the browser self.response.out.write(Formatter.dataWrapper(format, returnData)) else: self.response.out.write(Formatter.error(format, 'Unable to perform reverse geoencoding')) return
def get(self,geopt=None,format='json'): #set content-type self.response.headers['Content-Type'] = Formatter.contentType(format) #validate lat/lon if (not geopt or geopt == ''): self.response.out.write(Formatter.error(format, 'invalid GeoPoint')) return geoData = urllib.unquote(geopt).split(',') if (geoData and len(geoData) == 2): lon = geoData[1] lat = geoData[0] logging.debug("GPS Coordinates: %s,%s" % (lat,lon)) gb = geobox2.Geobox(lat, lon) #scope 100 miles box = gb.search_geobox(100) query = HospitalInfo.all().filter("geoboxes IN", [box]) #get 100 records results = query.fetch(100) db_recs = {} for result in results: distance = UtilLib.getEarthDistance(lat, lon, result.location.lat, result.location.lon) if (distance and distance > 0): db_recs[distance] = result returnData = MutableString() returnData = '' #output this badboy if (db_recs and len(db_recs) > 0): for key in sorted(db_recs.iterkeys()): p = db_recs[key] if (format == 'json'): startTag = '{' endTag = '},' distance = Formatter.data(format, 'distance', '%s %s' % (str(math.ceil(key)), "mi"))[:-1]#'%.2g %s' % (key, "mi"))[:-1] else: startTag = '<record>' endTag = '</record>' distance = Formatter.data(format, 'distance', '%s %s' % (str(math.ceil(key)), "mi")) #build the string returnData = "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s" % (returnData,startTag, Formatter.data(format, 'hospital_id', p.hospital_id), Formatter.data(format, 'name', p.name.replace('&', '&')), Formatter.data(format, 'address', p.address.replace('&', '&')), Formatter.data(format, 'city', p.city), Formatter.data(format, 'state', p.state), Formatter.data(format, 'zip_code', p.zip_code), Formatter.data(format, 'county', p.county.replace('&', '&')), Formatter.data(format, 'phone', p.phone), Formatter.data(format, 'hospital_type', p.hospital_type.replace('&', '&')), Formatter.data(format, 'hospital_owner', p.hospital_owner.replace('&', '&')), Formatter.data(format, 'emergency_service', p.emergency_service), Formatter.data(format, 'geo_location', p.location), distance, endTag ) #output to the browser self.response.out.write(Formatter.dataWrapper(format, returnData)) else: self.response.out.write(Formatter.error(format, 'Unable to retrieve lat/lon from received GeoPoint')) return