-
Notifications
You must be signed in to change notification settings - Fork 2
/
0. download hfcs data.R
134 lines (100 loc) · 4.77 KB
/
0. download hfcs data.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
packages.available = installed.packages()
if (!"httr" %in% row.names(packages.available)) install.packages("httr")
if (!"rvest" %in% row.names(packages.available)) install.packages("rvest")
if (!"haven" %in% row.names(packages.available)) install.packages("haven")
if (!"tcltk2" %in% row.names(packages.available)) install.packages("tcltk2")
library(httr)
library(rvest)
library(haven)
library(tcltk2)
default_path = getwd()
set_config( config( ssl_verifypeer = 0L ) )
# the function to download the data - once you have your logon id (the email address), your password and the CAPTCHA
download.hfcs = function(info,cookie) {
# Log in
url_intra = POST('https://www.ecb.europa.eu/secure/hfcs/login.html',body=info,set_cookies(.cookies=cookie),encode="multipart")
url_intra_form = read_html(url_intra$content) %>% html_form()
print(url_intra_form)
cook2 = url_intra$cookies$value
names(cook2) = url_intra$cookies$name
#submit the form back
url_final = POST(url_intra_form[[1]]$url, body = list(RelayState=url_intra_form[[1]]$fields$RelayState$value,
SAMLResponse=url_intra_form[[1]]$fields$SAMLResponse$value),
set_cookies(.cookies=cook2),
encode="form")
#write.table(url_intra,file="page.html")
#tf = tempfile(fileext=".zip")
#download.file('https://www.ecb.europa.eu/secure/hfcs/download.html?src=HFCS_User_DataBase_1_1_SAS.zip&type=all&sid=057269199b920422b335cfaf9f73d650',destfile = tf)
# url_logged = getURL('https://www.ecb.europa.eu/secure/hfcs/page1.html')
#windows.save_and_unzip_file(tf)
}
# the function to decide the path where to store the data + unzip the archive downloaded from the website
windows.save_and_unzip_file = function(file) {
path_store = tk_choose.dir(getwd(),"Choose where you want to store HFCS data")
setwd(path_store)
file.copy(file,"HFCS_User_DataBase_1_1_SAS.zip")
unzip("HFCS_User_DataBase_1_1_SAS.zip")
}
# Tcl/Tk code for creating the GUI
# The logon window - first window to collect logon information from the user
Logon_window = function() {
# connect to the login page to download the CAPTCHA picture
url = GET('https://www.ecb.europa.eu/secure/hfcs/login.html',verbose(info=TRUE))
cook = url$cookies$value
names(cook) = url$cookies$name
tf = tempfile(fileext = ".png")
# extract the `captcha` location string
#url = gsub('"',"",url)
captcha = as.character(sub(
'.*src="([:alphanum:/+])"*' ,
'\\1' ,
url
))
captcha = sub('"',"",captcha)
x = gregexpr(" alt=",captcha)
captcha = substr(captcha,1,x[[1]][1]-1)
download.file(paste0('https://www.ecb.europa.eu/secure',captcha),destfile=tf)
image = tclVar()
tkimage.create("photo",image,file=tf)
window_log = tktoplevel()
tktitle(window_log) = "Identification elements for logging on the HFCS intranet"
Email_address = tclVar("")
Psswrd = tclVar("")
Captcha = tclVar("")
# Email section
window_log$env$entName <-tk2entry(window_log, width = "60", textvariable = Email_address)
tkgrid(tk2label(window_log, text = "Please enter your email address:", justify = "left"),
padx = 10, pady = c(15, 5), sticky = "w")
tkgrid(window_log$env$entName, padx = 10, pady = c(0, 15))
# Password section
window_log$env$entName <-tk2entry(window_log, width = "60", textvariable = Psswrd, show = "*")
tkgrid(tk2label(window_log, text = "Please enter your password:", justify = "left"),
padx = 10, pady = c(15, 5), sticky = "w")
tkgrid(window_log$env$entName, padx = 10, pady = c(0, 15))
# Captcha section
tkgrid(tk2label(window_log, text = "Please enter code (see picture below):", justify = "left"),
padx = 10, pady = c(15, 5), sticky = "w")
window_log$env$label <- tk2label(window_log, image = image)
tkgrid(window_log$env$label)
window_log$env$entName <-tk2entry(window_log, width = "60", textvariable = Captcha)
tkgrid(window_log$env$entName, padx = 10, pady = c(0, 15))
# OK button
onOK <- function() {
tkdestroy(window_log)
Email_address.Value = tclvalue(Email_address)
Psswrd.Value = tclvalue(Psswrd)
Captcha.Value = tclvalue(Captcha)
info_logon = list('userid'=Email_address.Value,
'password'=Psswrd.Value,
'ct_captcha'=Captcha.Value,
'Login'="Login")
download.hfcs(info=info_logon,cookie=cook)
}
window_log$env$butOK <-tk2button(window_log, text = "OK", width = -6, command = onOK)
tkgrid(window_log$env$butOK, padx = 10, pady = c(5, 15))
tkbind(window_log$env$entName, "<Return>", onOK)
tkfocus(window_log)
}
Logon_window()
#setwd(default_path)
#if (file.exists("cookies.txt")) file.remove("cookies.txt")