Skip to content

Instantly share code, notes, and snippets.

@wagurano
Last active September 25, 2015 13:21
Show Gist options
  • Save wagurano/2fef3eaeddd64322f17a to your computer and use it in GitHub Desktop.
Save wagurano/2fef3eaeddd64322f17a to your computer and use it in GitHub Desktop.
당번약국을 보여주고 당번인데 문닫은 약국을 신고합니다. 구글드라이브 [CSV파일](https://drive.google.com/file/d/0B8WezMmea38UVjlxc3FHR3V1Q28/view?usp=sharing) 2014년 10월 기준, 심사평가원 + 중앙응급의료원 대한약사회에서 휴일지킴이약국을 http://www.pharm114.or.kr/ 운영합니다. 여기서는 중앙응급의료원 http://www.e-gen.or.kr/ 에서 자료를 가져옵니다. 공공데이타포털에서도 자료를 가져올 수 있는데 하루 1000 회 제한이 있습니다. 심사평가원의 데이타를 추정해보면 약 20만 여개로 추정하는데 공공API로는 수일내…
#encoding: utf-8
require 'net/http'
require 'nokogiri'
require 'open-uri'
require 'timeout'
require 'json'
TIMEOUT_CNT = 42
def egen_print_pharm_info code
retries = TIMEOUT_CNT
begin
Timeout::timeout(5) {
ret = {}
ret[:info] = {}
ret[:hour] = Array.new(7)
ret[:hour][0] = [nil, nil]
doc = Nokogiri::HTML(open("http://www.e-gen.or.kr/egen/inf.se42.do?HPID=#{code}"))
doc.xpath('//ul/li').each do |a|
header, data = a.text.gsub(/[\t]/, '').gsub(/\r\n/, ' ').split(': ')
case header
when '대표전화'
ret[:phone] = data
when '주소'
ret[:address] = data[10..-1]
when '월요일'
ret[:hour][1] = [data[0..4], data[6..11]]
when '화요일'
ret[:hour][2] = [data[0..4], data[6..11]]
when '수요일'
ret[:hour][3] = [data[0..4], data[6..11]]
when '목요일'
ret[:hour][4] = [data[0..4], data[6..11]]
when '금요일'
ret[:hour][5] = [data[0..4], data[6..11]]
when '토요일'
ret[:hour][6] = [data[0..4], data[6..11]]
when '일요일'
ret[:hour][0] = [data[0..4], data[6..11]]
when '위치설명'
ret[:address_desc] = data
end
end
ret[:coordinates] = [doc.at('input[@name="LON"]')['value'].to_f, doc.at('input[@name="LAT"]')['value'].to_f]
ret
}
rescue Timeout::Error
retries -= 1
if retries > 0
puts "sleep"
sleep 1.42
retry
else
puts "raise"
raise
end
end
end # def egen_print_info
def egen_get_pharm_list n
retries = TIMEOUT_CNT
elements = nil
begin
cnt = 0
Timeout::timeout(5) {
uri = URI("http://www.e-gen.or.kr/egen/inf.se51.do?selectedSido=&selectedSigungu=&textDong=&lon=&lat=&cnt=1&str_cnt=1&page_num=1&page_size=100000&dutydiv=081&dgid=&time=&hpname=%25&spid=&x=24&y=11&page=#{n}")
req = Net::HTTP::Get.new(uri.request_uri)
req['cookie'] = "dong=null; lat=12675299; lon=46433849; sido=%25; sigungu=%25; JSESSIONID=FA638CB737D45369ED485C5106492BB3; DWRSESSIONID=c6JMSsrbcCyyepBSnFHBm9b$Tyk"
res = Net::HTTP.start(uri.hostname, uri.port) { |http| http.request(req) }
doc = Nokogiri::HTML(res.body) # cookie sido = %25
elements = doc.xpath('//td/b/a')
}
rescue Timeout::Error
retries -= 1
if retries > 0
puts "sleep"
sleep 1.42
retry
else
puts "raise"
raise
end
end
elements
end # def egen_get_list
def get_pharm_list n
retries = TIMEOUT_CNT
list = []
begin
cnt = 0
code = ""
Timeout::timeout(10) {
doc = Nokogiri::HTML(open("http://www.hira.or.kr/rd/hosp/getHospList.do?pgmid=HIRAA030002000000&mode=02&ykiho=&isActivity=Y&isDown=N&sidoCd=&sidoCdNm=&sgguCd=&sgguCdNm=&emdongNm=&zipCd=&spclHospTpCdParent=&pageIndex=#{n}&sortOrdr=&evlCd=&yadmNm="))
num = 0; name = ""; nop = nil; contact = ""; address = ""; gps = ""; idx = 1;
doc.xpath('//table[@class="tbl_list"]/tbody/tr/td').each do |a|
num = a.text if idx == 1
contact = a.text if idx == 4
address = a.text if idx == 5
if idx == 2
a.children.each do |x|
name = x.text
gps = x['onclick'][35..114]
end
end
if idx == 8
# puts "#{num}, #{name}, #{contact}, #{address}, #{gps}" # debug
list << {num: num, name: name, contact: contact, address: address, ykiho: gps}
idx = 1
else
idx += 1
end
end
}
rescue Timeout::Error
retries -= 1
if retries > 0
puts "sleep"
sleep 0.42
retry
else
puts "raise"
raise
end
end
list
end
def get_pharm_gps ykiho
retries = TIMEOUT_CNT
x = nil; y = nil;
begin
cnt = 0
Timeout::timeout(10) {
doc = Nokogiri::HTML(open("http://www.hira.or.kr/rd/hosp/getMapInfo.do?ykiho=#{ykiho}&pgmid=HIRAA030002000000"))
pos = doc.to_s.scan(/x : (\d*\.\d*), y : (\d*\.\d*)/)
if pos.length > 0
x = pos[0][0].to_f
y = pos[0][1].to_f
end
}
rescue Timeout::Error
retries -= 1
if retries > 0
puts "sleep"
sleep 0.42
retry
else
puts "raise"
raise
end
end
[x, y]
end
DAUM_APIKEY = "..........................." # YOUR_KEY
def geocode address
url = "http://apis.daum.net/local/geo/addr2coord?apikey=#{DAUM_APIKEY}&output=json&q=#{address}"
channel = JSON.parse(open(URI.encode(url)).read)['channel']
items = channel['item']
if channel['result'].to_i > 0
unless items.nil?
item = items.first
[item['lng'].to_f, item['lat'].to_f]
end
else
[nil, nil]
end
end
# a = egen_get_pharm_list 2120
# a.each do |x|
# puts x.text
# l = x["href"].split(/'/)
# puts l[9]
# end
# puts egen_print_info "C1101399"
# e-gne: [1,2120)
begin_n = ARGV[0].to_i; thru_n = ARGV[1].to_i #2120
n = begin_n
begin
cnt = 0
puts "PHARM,FETCH,#{n}"
pr_places = egen_get_pharm_list n
unless pr_places.nil?
pr_places.each do |el|
pr = el['href']
unless pr.empty?
cnt = cnt + 1
name = el.text
sn = pr.split("'")[9]
begin
info = egen_print_pharm_info sn
print "PR|", sn, "|", name, "|", info[:phone], "|", info[:address], "|", info[:address_desc], "|"
print info[:coordinates][0], "|", info[:coordinates][1]
info[:hour].each do |h|
if h.nil?
print "||"
else
print "|#{h[0]}|#{h[1]}"
end
end
print "\n"
rescue OpenURI::HTTPError
puts "ERROR,#{n},#{cnt},NOT_FOUND,-"
next
end
end
sleep 0.42
end # pr_places
end
n = n + 1
sleep 0.42
end while 0 < cnt && n < thru_n
puts "EGEN,#{n},END"
#encoding: utf-8
require 'net/http'
require 'nokogiri'
require 'open-uri'
require 'timeout'
require 'json'
# 심사평가원
TIMEOUT_CNT = 42
def get_pharm_list n
retries = TIMEOUT_CNT
list = []
begin
cnt = 0
code = ""
Timeout::timeout(10) {
doc = Nokogiri::HTML(open("http://www.hira.or.kr/rd/hosp/getHospList.do?pgmid=HIRAA030002000000&mode=02&ykiho=&isActivity=Y&isDown=N&sidoCd=&sidoCdNm=&sgguCd=&sgguCdNm=&emdongNm=&zipCd=&spclHospTpCdParent=&pageIndex=#{n}&sortOrdr=&evlCd=&yadmNm="))
num = 0; name = ""; nop = nil; contact = ""; address = ""; gps = ""; idx = 1;
doc.xpath('//table[@class="tbl_list"]/tbody/tr/td').each do |a|
num = a.text if idx == 1
contact = a.text if idx == 4
address = a.text if idx == 5
if idx == 2
a.children.each do |x|
name = x.text
gps = x['onclick'][35..114]
end
end
if idx == 8
# puts "#{num}, #{name}, #{contact}, #{address}, #{gps}" # debug
list << {num: num, name: name, contact: contact, address: address, ykiho: gps}
idx = 1
else
idx += 1
end
end
}
rescue Timeout::Error
retries -= 1
if retries > 0
puts "sleep"
sleep 0.42
retry
else
puts "raise"
raise
end
end
list
end
def get_pharm_gps ykiho
retries = TIMEOUT_CNT
x = nil; y = nil;
begin
cnt = 0
Timeout::timeout(10) {
doc = Nokogiri::HTML(open("http://www.hira.or.kr/rd/hosp/getMapInfo.do?ykiho=#{ykiho}&pgmid=HIRAA030002000000"))
pos = doc.to_s.scan(/x : (\d*\.\d*), y : (\d*\.\d*)/)
if pos.length > 0
x = pos[0][0].to_f
y = pos[0][1].to_f
end
}
rescue Timeout::Error
retries -= 1
if retries > 0
puts "sleep"
sleep 0.42
retry
else
puts "raise"
raise
end
end
[x, y]
end
DAUM_APIKEY = "................" # YOUR_KEY
def geocode address
url = "http://apis.daum.net/local/geo/addr2coord?apikey=#{DAUM_APIKEY}&output=json&q=#{address}"
channel = JSON.parse(open(URI.encode(url)).read)['channel']
items = channel['item']
if channel['result'].to_i > 0
unless items.nil?
item = items.first
[item['lng'].to_f, item['lat'].to_f]
end
else
[nil, nil]
end
end
n = 1023 # 1
cnt = 0
thru = 2102 # 2101
begin
cnt = 0
get_pharm_list(n).each do |x|
a = get_pharm_gps x[:ykiho]
x[:coordinates] = [a[0], a[1]]
if a[0].nil?
a = geocode(x[:address])
x[:coordinates] = [a[0], a[1]]
end
print "#{n}|#{cnt}"
x.each do |k, v|
if k == :coordinates
print "|#{v[0]}|#{v[1]}"
else
print "|#{v}"
end
end
if x[:coordinates][0].nil?
print "|ERROR\n"
else
print "\n"
end
cnt += 1
end
n += 1
end while cnt > 0 && n < thru
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment