Skip to content

Instantly share code, notes, and snippets.

@thewayiam
Last active March 1, 2016 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thewayiam/29e5e07c31b58db30a44 to your computer and use it in GitHub Desktop.
Save thewayiam/29e5e07c31b58db30a44 to your computer and use it in GitHub Desktop.
#!/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