From 979423478440452fc1b7b5ffc1b5b80bc98a7f3b Mon Sep 17 00:00:00 2001 From: Furen Xiao Date: Wed, 11 Dec 2024 13:19:30 +0800 Subject: [PATCH] add portal.py back --- portal.py | 990 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 990 insertions(+) create mode 100755 portal.py diff --git a/portal.py b/portal.py new file mode 100755 index 0000000..74078be --- /dev/null +++ b/portal.py @@ -0,0 +1,990 @@ +# -*- coding: utf-8 -*- + +from .settings import USER_ID, PASSWORD +DefaultUserID = USER_ID +DefaultPassword = PASSWORD + +# from datetime import * +# from urllib2 import * +try: + # For Python 3.0 and later + from urllib.request import urlopen +except ImportError: + # Fall back to Python 2's urllib2 + from urllib2 import urlopen + +from bs4 import BeautifulSoup +# from BeautifulSoup import * + +import datetime +import hashlib +import math +import pdb +import re +import time +import urllib +# import urllib2 + +# import mechanize +# print mechanize._sockettimeout._GLOBAL_DEFAULT_TIMEOUT +# mechanize._sockettimeout._GLOBAL_DEFAULT_TIMEOUT = 100 +# print mechanize._sockettimeout._GLOBAL_DEFAULT_TIMEOUT +import mechanicalsoup + +#from ClientForm import ParseResponse + + +# br = mechanize.Browser( +# # factory=mechanize.RobustFactory() +# ) +br = mechanicalsoup.StatefulBrowser() + +# br.set_handle_robots(False) + +def xtrace(R): + pdb.set_trace() + + +def remove_space(s): + return s.replace(' ','').strip() + + +def remove_tags(s): + return remove_space(re.sub('<[^<]+?>', '', s)) + + +#print remove_space(' 123 ') + + +def minguo2ce(minguo): + pattern = '(\d+)\.([ 0-9]{1,2})\.([ 0-9]{1,2})' + s = re.search(pattern, minguo) + if s: + yy = int(s.group(1))+1911 + + try: + mm = int(s.group(2)) + except: + mm = 1 + + try: + dd = int(s.group(3)) + except: + dd = 1 + + return date( yy, mm , dd ) + + pattern = '(\d+)/([ 0-9]{1,2})/([ 0-9]{1,2})' + s = re.search(pattern, minguo) + if s: + yy = int(s.group(1))+1911 + + try: + mm = int(s.group(2)) + except: + mm = 1 + + try: + dd = int(s.group(3)) + except: + dd = 1 + + return date( yy, mm , dd ) + + return + +#print minguo2ce(' 75.01.25') + + +def Default_Dr(REQUEST): + if (REQUEST.has_key('ChartNo')) and REQUEST['ChartNo'] != "": + values = { 'ChartNo' : REQUEST['ChartNo'] } + elif (REQUEST.has_key('Name')) and REQUEST['Name'] != "": + values = { 'Name' : REQUEST['Name'].decode('utf_8').encode('big5') } + elif (REQUEST.has_key('idcode')) and REQUEST['idcode'] != "": + values = { 'idcode' : REQUEST['idcode'] } + else: + return "" + + url = 'http://intra.mc.ntu.edu.tw/main/ChartNo/Default_Dr.asp' + + data = urllib.urlencode(values) + req = urllib2.Request(url, data) + response = urllib2.urlopen(req) + the_page = response.read() + the_page = the_page.decode('big5','ignore').encode('utf_8') + + pattern = ( '(?s)(.*?)(.*?)' + + '(.*?)(.*?)' + + '(.*?)(.*?)' + + '(.*?)(.*?)' + + '(.*?)(.*?)' + + '(.*?)(.*?)' + + '(.*?)(.*?)' + + '(.*?)' ) + + matches = re.findall(pattern, the_page) + + result = [] + for match in matches: + r = {} + r['name'] = remove_space(match[2]) + r['medical_records'] = remove_space(match[4]) + r['gender'] = remove_space(match[6]) + r['birthday'] = minguo2ce(match[8]) + r['address'] = remove_space(match[10]) + r['phone'] = remove_space(match[12]) + r['id_cards'] = remove_space(match[14]) + result.append(r) + + return result + + + +def percent_encoding(keys, REQUEST): + data = {} + for key in keys: + if REQUEST.__contains__(key): + data[key] = REQUEST[key] + + return urllib.urlencode(data) + + +def chinese2date(chinese): + pattern = '(\d+)(\D+)(\d{1,2})(\D+)(\d{1,2})' + s = re.search(pattern, chinese) + if s: + yy = int(s.group(1)) + mm = int(s.group(3)) + dd = int(s.group(5)) + return date( yy, mm , dd ) + + return + + + + +########################################New portal system + +SESSION = False +SESSION_TIME = time.time() + +def Login(): + global SESSION, SESSION_TIME + + if SESSION and time.time() - SESSION_TIME < 600: # seconds +# print SESSION, time.time() - SESSION_TIME + SESSION_TIME = time.time() + return SESSION + +# br.open("http://portal.ntuh.gov.tw/General/Login.aspx") + try: + br.open("http://portal.ntuh.gov.tw/General/Login.aspx", timeout=1) + except: + return None + +# br._factory.encoding = enc +# br._factory._forms_factory.encoding = enc +# br._factory._links_factory._encoding = enc + +# print br._factory.encoding +# print br._factory._forms_factory.encoding +# print br._factory._links_factory._encoding + +# br.open("http://portal.ntuh.gov.tw/General/Login.aspx") + +# print br.response().read() + + br.select_form(name="Form1") +# print br +# br["rdblQuickMenu"] = ['O'] + br["txtUserID"] = DefaultUserID + br["txtPass"] = hashlib.md5(DefaultPassword).hexdigest() + +# print br.possible_items("rdblQuickMenu") +# print br.form + response = br.submit() # submit current form + + pattern = "http://hisaw.ntuh.gov.tw/WebApplication/Clinics/OpenClinics.aspx\?SESSION=(\w*)" + string = str(response.read()) +# print string + matches = re.findall(pattern, string) + SESSION = matches[0] + SESSION_TIME = time.time() + return SESSION + +def HeightWeight(PersonID): #身高體重 + SESSION = Login() + url = "http://ihisaw.ntuh.gov.tw/WebApplication/OtherIndependentProj/PatientBasicInfoEdit/QueryHeightWeightByPersonID.aspx?SESSION=%s&PersonID=%s" % (SESSION,PersonID) + response = br.open(url) + body = response.read() + ''' + 151.7 + + + + 40.8 + + + ''' + + pattern = ''' +_HeightLabel">(?P.*?) +(.*?)_WeightLabel">(?P.*?) +'''.replace('\n', '').replace('"', '\\"') + + +# matches = re.findall(pattern, body, re.DOTALL) + matches = [m.groupdict() for m in re.finditer(pattern, body, re.DOTALL)] +# print matches[0] + + h = 0 + w = 0 + bsa = 0 + + if matches: + try: + h = float(matches[0]['HeightLabel']) + w = float(matches[0]['WeightLabel']) + bsa = math.sqrt(h * w / 3600) #Mosteller formula + except: + pass + + return {'Height': h, 'Weight': w, 'BSA': bsa} + + +def ReportResult(PersonID): + SESSION = Login() + url = "http://ihisaw.ntuh.gov.tw/WebApplication/OtherIndependentProj/PatientBasicInfoEdit/ReportResultQuery.aspx?SESSION=%s&PersonID=%s" % (SESSION,PersonID) + + response = br.open(url) + br.select_form(name="Form1") + response = br.submit() # submit current form + body = response.read() + # print body + return + + response = urlopen(url) + forms = ParseResponse(response, backwards_compat=False) + form = forms[0] + # print form + form.set_all_readonly(False) + form["__EVENTTARGET"] = "LinkbuttonRadReport" + +# form.click() returns a urllib2.Request object +# (see HTMLForm.click.__doc__ if you don't have urllib2) + # print urlopen(form.click()).read() + + +def icd_query(ChartNo): + Login() + + br.select_form(name="Form1") + br["NTUHWeb1:QueryPersonIDByChartNo2:txbChartNoInput"] = str(CharNo) + br["NTUHWeb1:QueryPersonIDByChartNo2:AutoShowRecord"] = True + response = br.submit() # submit current form + + return response.read() + + +def WardQueryUncompletedChart_QueryByOutDate(Start, End): + SESSION = Login() + url = "http://ihisaw.ntuh.gov.tw/WebApplication/MedicalRecordManagement/WardQueryUncompletedChart.aspx?SESSION=%s" % SESSION + response = br.open(url) + br.select_form(name="form1") +# print br.form + + br['NTUHWeb1$A']=['RBOutDate'] + response = br.submit() # submit current form + body = response.read() + + br.select_form(name="form1") +# print br.form + + br['NTUHWeb1$ddlDeptCode']=['SURG'] + br['NTUHWeb1$txtQStartDate']=Start + br['NTUHWeb1$txtQEndDate']=End + +# response = br.submit(name='NTUHWeb1$btn_QueryByDeptListAll') # submit current form + response = br.submit(name='NTUHWeb1$btn_QueryByOutDate') # submit current form + body = response.read() +# print body +# exit() + + ''' + 邱英世 + + + 1 + ''' + + pattern = '''(.*?)(\s*?)\ +(\s*?)\ +(\s*?)\ +(.*?)(\s*?)\ +''' + + matches = re.findall(pattern, body,re.DOTALL) +# print matches +# exit() + + q = {} + r = [] + + POS_DrName = 2 + POS_Count = 8 + + UNF_LIST = [] + + for match in matches: + DrName = match[POS_DrName] + Count = int(match[POS_Count]) +# print DrName, Count + r.append((DrName, Count)) + + return r + +#def WardQueryUncompletedChart_QueryByDeptListAll(Start, End): +def WardQueryUncompletedChart(Start, End): + SESSION = Login() + url = "http://ihisaw.ntuh.gov.tw/WebApplication/MedicalRecordManagement/WardQueryUncompletedChart.aspx?SESSION=%s" % SESSION + response = br.open(url) + br.select_form(name="form1") +# print br.form + + br['NTUHWeb1$A']=['RBOutDate'] + +# TIMEOUT = mechanize._sockettimeout._GLOBAL_DEFAULT_TIMEOUT +# mechanize._sockettimeout._GLOBAL_DEFAULT_TIMEOUT = 100 + response = br.submit() # submit current form +# mechanize._sockettimeout._GLOBAL_DEFAULT_TIMEOUT = TIMEOUT + + body = response.read() + + br.select_form(name="form1") +# print br.form + + br['NTUHWeb1$ddlDeptCode']=['SURG'] + br['NTUHWeb1$txtQStartDate']=Start + br['NTUHWeb1$txtQEndDate']=End + + response = br.submit(name='NTUHWeb1$btn_QueryByDeptListAll') # submit current form + body = response.read() +# print body +# exit() + + ''' + + 1504285710T02491675SURG2010/10/122010/10/13邱淑美09D 0901病摘簡雄飛黃柏誠admission note vs未蓋章  + + ''' + + pattern = '''\ +(\s*?)\ +(.*?)(.*?)(.*?)(.*?)(.*?)(.*?)(.*?)(.*?)(.*?)(.*?)(.*?)(.*?)(.*?)(\s*?)\ +\ +''' + + ''' + + 15749509 + 11T06202260 + SURG2011/08/302011/08/31黃寶蓮13PE0801 + 入院 + 王水深莊民楷 4 + + ''' + + pattern = '''(\s*?)\ +(.*?)(.*?)(\s*?)\ +(.*?)TypeLinkButton',''\)">(.*?)(\s*?)\ +(.*?)(.*?)(.*?)(.*?)(\s*?)\ +''' + matches = re.findall(pattern, body,re.DOTALL) +# print matches +# exit() + + q = {} + r = [] + + POS_C = 2 # Chart# + POS_T = 5 # Type + POS_V = 7 # VS + POS_R = 8 # R + POS_O = 10 # Over + + UNF_LIST = [] + + for match in matches: +# print match[10], match[11], match[13] +# print match[0], match[2], match[3], match[4], match[5] + if 'Y' in match[POS_C]: + # print match[POS_C], 'discarded(雲林)' + continue + else: + print (match[POS_C], match[POS_T], match[POS_V], match[POS_R], match[POS_O]) +# print match +# continue + try: + Overdue = int(match[POS_O]) + M_CHART = match[POS_C] + M_TYPE = match[POS_T] + M_VS = match[POS_V] + M_R = match[POS_R] + + if M_TYPE != '手術': + M_KEY = M_CHART+M_VS+M_R + if M_KEY in UNF_LIST: + continue + else: + UNF_LIST.append(M_KEY) + + if not q.has_key(M_VS): + q[M_VS] = 1 + else: + q[M_VS] += 1 + + if not q.has_key(M_R): + q[M_R] = 1 + else: + q[M_R] += 1 + + except: + pass + +# print q.iteritems() +# exit() + + for key,value in q.iteritems(): + r.append((key,value)) + +# print r +# exit() + + return r + + +################## os.getcwd()############## +def get_path(): + + import os,sys + + return os.path.realpath(os.path.dirname(sys.argv[0])) +############################### + +vs = {} + +def unf_byDisDate(deptcode, StartDate, EndDate): + import csv + reader = csv.reader(open(get_path()+"/vs.csv", "rb")) + for row in reader: +# print row[1], row[0] + vs[row[1]]=row[0] + + url = "http://intra.mc.ntu.edu.tw/main/Discharge/unf_byDisDate.asp" + response = br.open(url) + br.select_form(nr=0) + + br["deptcode"] = [deptcode] + br["StartDate"] = StartDate + br["EndDate"] = EndDate + + response = br.submit() + + body = response.read().decode('big5','ignore').encode('utf_8') + + pattern = """ +\s* +(.*?) +(.*?) +(.*?) +(.*?) + +

+(.*?) + + +(.*?) + +(.*?) +(.*?) +(.*?) + +(.*?) + +(.*?) +(.*?) + +""" + pattern = pattern.replace('"', '\\"') + pattern = pattern.replace('\n', '\\s*') + matches = re.findall(pattern, body) + + result = [] + for match in matches: + r = {} + r['no'] = remove_space(match[0]) + r['doctor_code'] = remove_space(match[1]) + r['doctor_name'] = remove_space(match[2]) + r['discharge_date'] = remove_space(match[3]) + r['patients_name'] = remove_space(match[4]) + r['medical_record_number'] = remove_space(match[5]) + r['account'] = remove_space(match[6]) + r['admission_date'] = remove_space(match[7]) + r['hospital_ bed'] = remove_space(match[8]) + r['category'] = remove_space(match[9]) + r['dr'] = remove_space(match[10]) + r['resident'] = remove_space(match[11]) + if vs.has_key(r['dr']): + r['division'] = vs[r['dr']] + else: + r['division'] = 'Others' + + result.append(r) + + return result + +def formatDate(sDate): + dScrap = sDate + + iDay = dScrap.day + iMon = dScrap.month + iYea = dScrap.year + + sDay = str(iDay) + sMon = str(iMon) + sYea = str(iYea - 1911) + + if len(sDay) == 1: + sDay = "0" + sDay + if len(sMon) == 1: + sMon = "0" + sMon + if len(sYea) == 2: + sYea = "0" + sYea; + sScrap = sYea + sMon + sDay; + + return sScrap; + + +def unf_sort(StartDay, EndDay): + + StartDate = datetime.date.today() + datetime.timedelta(days=StartDay) + EndDate = datetime.date.today() + datetime.timedelta(days=EndDay) + + result = unf_byDisDate('SURG', formatDate(StartDate), formatDate(EndDate)) + + dr = [] + resident = [] + division = [] + for r in result: + dr.append(r['dr']) + resident.append(r['resident']) + division.append(r['division']) + + # The count is doubled, so we div it by 2 + dr_freq = [(a, dr.count(a)/2) for a in set(dr)] + dr_sort = sorted(dr_freq, key=lambda x: -x[1]) + resident_freq = [(a, resident.count(a)/2) for a in set(resident)] + resident_sort = sorted(resident_freq, key=lambda x: -x[1]) + division_freq = [(a, division.count(a)/2) for a in set(division)] + division_sort = sorted(division_freq, key=lambda x: -x[1]) + +# print "\n主治醫師,份數" +# for dr in dr_sort: +# print "%s,%s" % dr +# +# print "\n住院醫師,份數" +# for resident in resident_sort: +# print "%s,%s" % resident + + return {'dr': dr_sort, + 'resident': resident_sort, + 'division': division_sort, + } + +def SimpleQueryOpScheduleByChartNo(ChartNo, SESSION=None): + if SESSION is None: + SESSION = Login() + url = 'http://ihisaw.ntuh.gov.tw/WebApplication/InPatient/OPManagement/SimpleQueryOpSchedule.aspx?SESSION=%s' % SESSION + response = br.open(url) + br.select_form(name="Form1") + br['NTUHWeb1$QueryOPPatListCommon1$QueryPersonIDByChartNo1$txbChartNoInput']=ChartNo + response = br.submit('NTUHWeb1$QueryOPPatListCommon1$QueryPersonIDByChartNo1$btnQuery') # submit current form + body = response.read() + pattern = ''' +_OPDateString">(?P.*?) +(.*?)_OpRoomNoShow">(?P.*?) +(.*?)_OpSeqNoshow">(?P.*?) +(.*?)_LinkPatName(.*?)(?P.*?) +(.*?)_PopupPatWardInfoWindow(.*?)(?P.*?) +(.*?)_PatChartNo(.*?)(?P.*?) +(.*?)_PatSex">(?P.*?) +(.*?)_PatAge" title="(?P.*?)">(?P.*?) +(.*?)_PatDignosis" title="(?P.*?)">(.*?) +(.*?)_MainOpMode" title="(?P.*?)">(?P.*?) +(.*?)_OpDoctorName">(?P.*?) +(.*?)_OpTypeName">(?P.*?) +(.*?)_CompleteStatueName">(?P.*?) +(.*?)_lbtPrint" title="(?P.*?)" +(.*?)_EstStartTimeShortString">(?P.*?) +(.*?)_EstSpendTime">(?P.*?) +''' + pattern = pattern.replace('"', '\\"') + pattern = pattern.replace('\n', '') + + pattern0 = '' + matches0 = re.findall(pattern0, body, re.DOTALL) + matches = [] + reco = re.compile(pattern, re.DOTALL) + for match0 in matches0: + matches1 = [m.groupdict() for m in reco.finditer(match0)] + matches.extend(matches1) + + result = [] + for match in matches: + + r = {} + r['OPDate'] = remove_space(match['OPDateString']) + r['OpRoomNo'] = remove_tags(match['OpRoomNoShow']) + r['OpSeqNo'] = remove_tags(match['OpSeqNoshow']) + r['PatName'] = remove_space(match['LinkPatName']) + r['PatWard'] = remove_space(match['PopupPatWardInfoWindow']) + r['PatChartNo'] = remove_space(match['PatChartNo']) + r['PatSex'] = remove_space(match['PatSex']) + r['PatAgeTitle'] = remove_space(match['PatAgeTitle']) + r['PatAge'] = remove_space(match['PatAge']) + r['PatDignosis'] = remove_space(match['PatDignosis']) + r['MainOpModeTitle'] = remove_space(match['MainOpModeTitle']) + r['MainOpMode'] = remove_space(match['MainOpMode']) + r['OpDoctorName'] = remove_space(match['OpDoctorName']) + r['OpTypeName'] = remove_space(match['OpTypeName']) + r['Complete'] = remove_space(match['CompleteStatueName']) + r['Anes'] = remove_space(match['lbtPrint']) + r['StartTime'] = remove_space(match['EstStartTimeShortString']) + r['SpendTime'] = remove_space(match['EstSpendTime']) + + result.append(r) + + return result + + +def SimpleQueryOpSchedule(DrCode, StartDate, EndDate): + SESSION = Login() + url = 'http://ihisaw.ntuh.gov.tw/WebApplication/InPatient/OPManagement/SimpleQueryOpSchedule.aspx?SESSION=%s' % SESSION + response = br.open(url) + br.select_form(name="Form1") + br['NTUHWeb1$QueryOPPatListCommon1$QueryDrIDInfoByDrName1$EmpNoQueryInput']=DrCode + br['NTUHWeb1$QueryOPPatListCommon1$txbStartDate']= StartDate + br['NTUHWeb1$QueryOPPatListCommon1$txbEndDate'] = EndDate + response = br.submit('NTUHWeb1$QueryOPPatListCommon1$QueryByMainDrCode') # submit current form + body = response.read() +# body = body[40000:50000] + +# print body +# exit() + + ''' + 11/07 + + 003 + + 01 + + + 薛福賜 + + + 08D_22_01 + + 5902391 + + M + + 58y6m + + + Left far-lateral approach for C1 du... + + 杜永光 + + + + 完成 + + + G + + + 09:03 + + 430 + + + + E + + ''' + + + pattern = ''' +_OPDateString">(?P.*?) +(.*?)_OpRoomNoShow">(?P.*?) +(.*?)_OpSeqNoshow">(?P.*?) +(.*?)_LinkPatName(.*?)(?P.*?) +(.*?)_PopupPatWardInfoWindow(.*?)(?P.*?) +(.*?)_PatChartNo(.*?)(?P.*?) +(.*?)_PatSex">(?P.*?) +(.*?)_PatAge" title="(?P.*?)">(?P.*?) +(.*?)_PatDignosis" title="(?P.*?)">(.*?) +(.*?)_MainOpMode" title="(?P.*?)">(?P.*?) +(.*?)_OpDoctorName">(?P.*?) +(.*?)_OpTypeName">(?P.*?) +(.*?)_CompleteStatueName">(?P.*?) +(.*?)_lbtPrint" title="(?P.*?)" +(.*?)_EstStartTimeShortString">(?P.*?) +(.*?)_EstSpendTime">(?P.*?) +''' + + +# pattern = ''' +#_OPDateString">(.*?) +#(.*?)_OpRoomNoShow">(.*?) +#(.*?)_OpSeqNoshow">(.*?) +#(.*?)_LinkPatName(.*?)(.*?) +#(.*?)(.*?) +#(.*?)(.*?) +#(.*?)_PatSex">(.*?) +#(.*?)_PatAge" title="(.*?)">(.*?) +#(.*?)_PatDignosis"(.*?)>(.*?) +#(.*?)_MainOpMode" title="(.*?)">(.*?) +#(.*?)_OpDoctorName">(.*?) +#(.*?)_OpTypeName">(.*?) +#(.*?)_CompleteStatueName">(.*?) +#(.*?)_lbtPrint" title="(.*?)" +#(.*?)_EstStartTimeShortString">(.*?) +#(.*?)_EstSpendTime">(.*?) +#''' + + + pattern = pattern.replace('"', '\\"') +# pattern = pattern.replace('\n', '\\s*') + pattern = pattern.replace('\n', '') + +# matches = re.findall(pattern, body, re.DOTALL) + + pattern0 = '' + matches0 = re.findall(pattern0, body, re.DOTALL) + matches = [] + reco = re.compile(pattern, re.DOTALL) + for match0 in matches0: +# matches1 = re.findall(pattern, match0, re.DOTALL) + matches1 = [m.groupdict() for m in reco.finditer(match0)] + matches.extend(matches1) + +# print matches +# exit() + +# for match in matches: +# print match[0], match[3], match[6], match[10], match[13], match[16], match[19], match[22], match[23], match[26], match[30], match[31], match[34], match[37], match[40], match[43], match[46], match[49] +# print match[26] + + result = [] + for match in matches: + +# print match.group('OPDateString') +# exit() + + r = {} + r['OPDate'] = remove_space(match['OPDateString']) +# r['OpRoomNo'] = remove_space(match[2]) +# r['OpSeqNo'] = remove_space(match[4]) + r['OpRoomNo'] = remove_tags(match['OpRoomNoShow']) + r['OpSeqNo'] = remove_tags(match['OpSeqNoshow']) + r['PatName'] = remove_space(match['LinkPatName']) + r['PatWard'] = remove_space(match['PopupPatWardInfoWindow']) + r['PatChartNo'] = remove_space(match['PatChartNo']) + r['PatSex'] = remove_space(match['PatSex']) + r['PatAgeTitle'] = remove_space(match['PatAgeTitle']) + r['PatAge'] = remove_space(match['PatAge']) + r['PatDignosis'] = remove_space(match['PatDignosis']) + r['MainOpModeTitle'] = remove_space(match['MainOpModeTitle']) + r['MainOpMode'] = remove_space(match['MainOpMode']) + r['OpDoctorName'] = remove_space(match['OpDoctorName']) + r['OpTypeName'] = remove_space(match['OpTypeName']) + r['Complete'] = remove_space(match['CompleteStatueName']) + r['Anes'] = remove_space(match['lbtPrint']) + r['StartTime'] = remove_space(match['EstStartTimeShortString']) + r['SpendTime'] = remove_space(match['EstSpendTime']) + +# print match +# print r +# exit() + + result.append(r) + + return result + +def PatientMedicalRecordListQuery(Chart, SESSION = None): + if SESSION is None: + SESSION = Login() + url = "http://ihisaw.ntuh.gov.tw/WebApplication/OtherIndependentProj/PatientBasicInfoEdit/PatientMedicalRecordListQuery.aspx?QueryBySelf=N&SESSION=%s" % SESSION + br.open(url) + br.select_form(name="Form1") +# br["NTUHWeb1$PersonIDInputTextBox"] = PersonID +# br["NTUHWeb1$ChartInputTextBox"] = Chart + br["NTUHWeb1$PatientBasicInfoQueryByIDAndName1$ctl01"] = Chart +# response = br.submit('NTUHWeb1$ButtonQuery') + response = br.submit('NTUHWeb1$PatientBasicInfoQueryByIDAndName1$ctl09') + html = response.read() + + patient = {} + + pattern = r'(?P.*?)\((?P.*?),(?P.*?),(?P.*?)\) \((?P.*?)\)' + r = re.compile(pattern) + d = [m.groupdict() for m in r.finditer(html)] + patient.update(d[0]) + + + pattern = ''' +InLabelWardName">(?P.*?) + +.*?_InLabelRoomName">(?P.*?) + +.*?_InLabelBedName">(?P.*?) + +.*?_InLabelMainDrName">(?P.*?) + +.*?_InLabelMainDiagnosisName">(?P.*?) +''' + pattern = pattern.strip().replace('\n','\s*') + r = re.compile(pattern) + d = [m.groupdict() for m in r.finditer(html)] + + patient['InPatRecord'] = d + + + pattern = ''' +LabelMainDrName">(?P.*?) +.*? +.*?_LabelMainDiagnosisName">(?P.*?) +''' + + pattern = pattern.strip().replace('\n','\s*') +# pattern = pattern.replace('%', '\\%') + + r = re.compile(pattern) + d = [m.groupdict() for m in r.finditer(html)] + + patient['OutPatRecord'] = d + +# print patient + + return patient + + +def operationnotelist(Chart, SESSION = None): + if SESSION is None: + SESSION = Login() + + url = "http://ihisaw.ntuh.gov.tw/WebApplication/OtherIndependentProj/PatientBasicInfoEdit/PatientMedicalRecordListQuery.aspx?QueryBySelf=N&SESSION=%s" % SESSION + br.open(url) + # br.select_form(name="Form1") + br.select_form('[name="Form1"]') +# br["NTUHWeb1$PersonIDInputTextBox"] = PersonID +# br["NTUHWeb1$ChartInputTextBox"] = Chart + br["NTUHWeb1$PatientBasicInfoQueryByIDAndName1$ctl01"] = Chart +# response = br.submit('NTUHWeb1$ButtonQuery') + # response = br.submit('NTUHWeb1$PatientBasicInfoQueryByIDAndName1$ctl09') + response = br.submit_selected('NTUHWeb1$PatientBasicInfoQueryByIDAndName1$ctl09') + # br.select_form(name="Form1") + br.select_form('[name="Form1"]') +# request = br.click("NTUHWeb1$PatAccountListRecord1$ShowOperationList") +# response = mechanize.urlopen(request) + # response = br.submit("NTUHWeb1$PatAccountListRecord1$ShowOperationList") + response = br.submit_selected("NTUHWeb1$PatAccountListRecord1$ShowOperationList") + ''' +

+ + + + + ''' +# print response.read() +# return + + # matches = re.findall("&PersonID=(.*?)&Func=OPNoteList&Seed=", response.read()) + matches = re.findall("&PersonID=(.*?)&Func=OPNoteList&Seed=", response.text) + PersonID = matches[0] + + url2 = 'http://ihisaw.ntuh.gov.tw/WebApplication/OtherIndependentProj/PatientBasicInfoEdit/SimpleInfoShowUsingPlaceHolder.aspx?SESSION=%s&PersonID=%s&Func=OPNoteList' % (SESSION, PersonID) + response = br.open(url2) +# print response.read() + + pattern ="'TreeViewItem','(.*?)'\)(.*?)>(.*?)" +# pattern = 'name="KeyCodeList" value=(.*?)>' + # matches=re.findall(pattern, response.read()) + matches=re.findall(pattern, response.text) +# print matches +# print KeyCodeList, KeyNameList + + +# br.select_form('operationnotelist') +# response = br.submit() + + KeyCode = [] + KeyName = [] + for m in matches: + KeyCode.append(m[0][1:]) + KeyName.append(m[2]) + + return (KeyCode, KeyName) + + +def ShowOperationNote(KeyCodeList, KeyNameList, SESSION=None): + # print(KeyCodeList, KeyNameList) + if SESSION is None: + SESSION = Login() + + url = 'http://ihisaw.ntuh.gov.tw/WebApplication/OtherIndependentProj/PatientBasicInfoEdit/SimpleInfoShowUsingPlaceHolder.aspx?SESSION=%s' % SESSION + + data = { + 'KeyCodeList': KeyCodeList, + 'KeyNameList': KeyNameList, + 'Func' : 'OPNoteList', + } + + # response = br.open(url, urllib.urlencode(data)) + response = br.post(url, data) + # body = response.read() + body = response.text + + pattern ='(
.*?
)\\s*?' + matches=re.findall(pattern, body, re.DOTALL) + return matches[0] + +def op_note_case(ChartNo): + + + pattern = ''' +手術日期(?P.*?) +(.*?)手術主治醫師(?P.*?) +(.*?)手術科部: (?P.*?) +(.*?)Operative Method(?P.*?)< +'''.replace('\n', '').replace('"', '\\"') + + KeyCode, KeyName = operationnotelist(ChartNo) + result = [] + for i in range(len(KeyCode)): + body = ShowOperationNote(KeyCode[i], KeyName[i]) + matches = [m.groupdict() for m in re.finditer(pattern, body, re.DOTALL)] +# print matches[0] +# return + r = {} + r['surgery_date_time'] = matches[0]['SurgeryDate'].replace('/','-') + r['division'] = remove_space(matches[0]['Division']) + r['name_surgery'] = remove_space(matches[0]['NameSurgery']) + r['surgeon'] = remove_space(matches[0]['Surgeon']) + result.append(r) + + return result