Send an Email to all your Representatives. With R.
7 minute read
I disagreed with the recent law the French government pass.
I wanted to explain my point of view to the French senators and did it manually at first. After sending two mails, it hits me that it is possible to do a script to automate it and even a R script.
The two main packages used are XML and mailR, respectively to do the HTML scraping and automatically sent mails.
The XML package is really simple to handle for this task and have the getHTMLLinks() function which extract all the link from a webpage.
Scrape the senate website
In the French senate website, we could find a personal page for each senator and a few information on our representatives, including the group, the mail provided by the senate and sometime personals mails.
The first step is to get all the personal web pages of the senators.
In the end, we get a file with all the emails of the French senators and the associated details.
Sending the emails
The package mailR allows to send email within R. It is very useful when you have really long process and you want to be averted when the process is finished.
In addition, the function send.mail send HTML formatted emails, which allows a great deal of customization.
To facilitate the debugging, the mail is written in HTML then the brew package is used to replace the names, and personalize the message looking at the group. It allows as well to keep the log of the mail.
The loop for sending the email could take a few minutes.
The file allows an analysis of the parity.
Only the CRC (communists) and the ecologists have a strict parity.