Skip to content

Instantly share code, notes, and snippets.

@thewayiam
Last active Mar 1, 2016
Embed
What would you like to do?
#!/usr/bin/env python
#coding:UTF-8
import os
import re
import json
import requests
import time
os.environ['TZ'] = 'ROC'
output = {key: None for key in ['pending_doctor', 'pending_bed', 'pending_ward', 'pending_icu', 'full_reported', 'update_time']}
output['hospital_sn'] = '1302050014'
html = requests.get('http://www.kmuh.org.tw/KMUHWeb/Pages/P04MedService/ERStatus.aspx')
if html.status_code == 200:
for i, match in enumerate(re.finditer(u'<span .*?>\s*(?P<number>\d+)\s*</', html.text)):
for key, value in {'pending_doctor': 'WaitingVisit', 'pending_bed': 'WaitingBed', 'pending_ward': 'WaitingBeingInHospital', 'pending_icu': 'WaitingICUBed'}.items():
if re.search(value, match.group()):
output[key] = int(match.group('number'))
break
if i > 3: break
match_report = re.search(u'【滿載】|【未滿載】', html.text)
match_time = re.search(u'>(\d+/\d+/\d+\s*(?:上|下)午\s*\d+:\d+)', html.text)
if match_report:
output['full_reported'] = True if match_report.group() == u'【滿載】' else False
if match_time:
update_time = match_time.group(1).replace(u'上午','AM') if u'上午' in match_time.group(1) else match_time.group(1).replace(u'下午','PM')
update_time = re.sub(u'\s', '', update_time)
output['update_time'] = int(time.mktime(time.strptime(update_time, "%Y/%m/%d%p%I:%M")))
print json.dumps(output, ensure_ascii=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment