Last active
July 16, 2017 21:25
-
-
Save felhix/13a82961d586d949d807c90fe3ea8685 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "open-uri" | |
require "nokogiri" | |
require "json" | |
BASE_URL = "http://annuaire-des-mairies.com/" | |
def email_of_a_townhall(path) | |
html_content = open("#{BASE_URL}#{path}") | |
document = Nokogiri::HTML(html_content) | |
email = document.css("body > table > tr:nth-child(3) > td > " + | |
"table > tr:first-child > td > " + | |
"table:nth-child(8) > tr:nth-child(2) > td > " + | |
"table > tr:nth-child(4) > td:nth-child(2)").text | |
email.strip.gsub(" ", "") | |
end | |
# Check that first function behave as expected | |
# puts "- email_of_a_townhall" | |
# puts email_of_a_townhall("./95/vaureal.html") | |
def department_townhall_urls(path) | |
html_content = open("#{BASE_URL}#{path}") | |
document = Nokogiri::HTML(html_content) | |
links = document.css("table.Style20 a.lientxt") | |
links.each_with_object({}) do |node, result| | |
city = node.text | |
path = node["href"] | |
result[city] = path | |
end | |
end | |
# Check that first function behave as expected | |
# puts "- department_townhall_urls" | |
# puts department_townhall_urls("./val-d-oise.html") | |
# Putting pieces together | |
result = {} | |
department_townhall_urls("./val-d-oise.html").each do |city, path| | |
result[city] = email_of_a_townhall(path) | |
# Only do 10 of them for the example | |
break if result.size >= 10 | |
end | |
puts JSON.pretty_generate(result) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "open-uri" | |
require "nokogiri" | |
require "json" | |
ALL_CURRENCIES_URL = "https://coinmarketcap.com/all/views/all/" | |
def cryptocurrency_rates | |
html_content = open(ALL_CURRENCIES_URL) | |
document = Nokogiri::HTML(html_content) | |
lines = document.css("table#currencies-all tbody tr") | |
lines.each_with_object({}) do |node, result| | |
currency = node.css("td.currency-name a").text | |
price = node.css("td a.price").text | |
price = price.gsub(/[^0-9.]/, '').to_f | |
result[currency] = price | |
end | |
end | |
while true | |
filename = "exo2-#{Time.now.to_i}.json" | |
File.open(filename, "wb") do |file| | |
json_content = JSON.pretty_generate(cryptocurrency_rates) | |
file.write(json_content) | |
end | |
puts "Wrote #{filename}, now waiting 1 hour..." | |
sleep 60 * 60 # 1 hour = 60 seconds * 60 minutes | |
end | |
Raw |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment