-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.rb
51 lines (43 loc) · 1.25 KB
/
script.rb
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
# This script generates links for downloading free books from O'Reilly site (http://www.oreilly.com/programming/free)
# Requirements
# ruby
# httparty, nokogiri (gem install httparty nokogiri --no-ri --no-rdoc)
# Execute
# ruby script.rb > books.md
require 'httparty'
require 'nokogiri'
require 'uri'
URL = 'http://www.oreilly.com/'
THEMES = ['programming', 'iot', 'data', 'webops', 'web-platform', 'security', 'business']
FREE_PATH = 'free/'
FILE_PATH = 'files/'
FORMATS = ['.pdf', '.epub', '.mobi']
def theme_url(theme)
URL + theme+ '/' + FREE_PATH
end
def download_url(book, fmt)
theme_url(book[:theme]) + FILE_PATH + book[:file_name] + fmt
end
def get_book_info(link)
splitted_url = URI(link.attributes['href'].value).path.split('/')
{
theme: splitted_url[1],
title: link.attributes['title'].value,
file_name: splitted_url.last.split('.').first
}
end
def books(theme_url)
Nokogiri.HTML(HTTParty.get(theme_url).body)
.css("section .product-row a")
.map { |link| get_book_info(link) }
end
def main
THEMES.each do |t|
puts "## From theme: #{t.capitalize}"
books(theme_url(t)).each do |book|
puts "### #{book[:title]}"
FORMATS.each { |fmt| puts download_url(book, fmt) + "</br>" }
end
end
end
main