C0 code coverage information
Generated on Tue Oct 16 11:40:47 -0400 2007 with rcov 0.8.0
Code reported as executed by Ruby looks like this...
and this: this line is also marked as covered.
Lines considered as run by rcov, but not reported by Ruby, look like this,
and this: these lines were inferred by rcov (using simple heuristics).
Finally, here's a line marked as not executed.
1 # Copyright (C) 2007 Marco Costantini
2 # based on ibs_it.rb by Claudio Belotti
3 #
4 # Alexandria is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 2 of the
7 # License, or (at your option) any later version.
8 #
9 # Alexandria is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public
15 # License along with Alexandria; see the file COPYING. If not,
16 # write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 # Boston, MA 02111-1307, USA.
18
19 require 'fileutils'
20 require 'net/http'
21 require 'open-uri'
22 #require 'cgi'
23
24 module Alexandria
25 class BookProviders
26 class BOL_itProvider < GenericProvider
27 BASE_URI = "http://www.bol.it"
28 CACHE_DIR = File.join(Alexandria::Library::DIR, '.bol_it_cache')
29 REFERER = BASE_URI
30 LOCALE = "libri" # possible locales are: "libri", "inglesi", "video", "musica", "choco"
31 def initialize
32 super("BOL_it", "BOL (Italy)")
33 FileUtils.mkdir_p(CACHE_DIR) unless File.exists?(CACHE_DIR)
34 # no preferences for the moment
35 at_exit { clean_cache }
36 end
37
38 def search(criterion, type)
39 criterion = criterion.convert("ISO-8859-1", "UTF-8")
40 req = BASE_URI + "/" + LOCALE + "/"
41 req += case type
42 when SEARCH_BY_ISBN
43 "scheda/"
44
45 when SEARCH_BY_TITLE
46 "risultatoricerca?action=bolrisultatoricerca&skin=bol&filtro_ricerca=BOL&quick_type=Titolo&titolo="
47
48 when SEARCH_BY_AUTHORS
49 "risultatoricerca?action=bolrisultatoricerca&skin=bol&filtro_ricerca=BOL&quick_type=Autore&titolo="
50
51 when SEARCH_BY_KEYWORD
52 "risultatoricerca?action=bolrisultatoricerca&skin=bol&filtro_ricerca=BOL&quick_type=Parola%20chiave&titolo="
53
54 else
55 raise InvalidSearchTypeError
56
57 end
58
59 ## warning: this provider uses pages like http://www.bol.it/libri/scheda/ea978888584104 with 12 numbers, without the checksum
60 criterion = "ea" + Library.canonicalise_ean(criterion)[0 .. -2] + ".html" if type == SEARCH_BY_ISBN
61 req += CGI.escape(criterion)
62 p req if $DEBUG
63 data = transport.get(URI.parse(req))
64 if type == SEARCH_BY_ISBN
65 to_book(data) #rescue raise NoResultsError
66 else
67 begin
68 results = []
69 each_book_page(data) do |code, title|
70 results << to_book(transport.get(URI.parse(BASE_URI + "/#{LOCALE}/scheda/ea" + code)))
71 end
72 return results
73 rescue
74 raise NoResultsError
75 end
76 end
77 end
78
79 def url(book)
80 BASE_URI + "/#{LOCALE}/scheda/ea" + Library.canonicalise_ean(book.isbn)[0 .. -2] + ".html"
81 end
82
83 #######
84 private
85 #######
86
87 def to_book(data)
88 raise NoResultsError if /Scheda libro non completa \(TP null\)/.match(data) != nil
89 data = data.convert("UTF-8", "ISO-8859-1")
90
91 raise "No title" unless md = /<INPUT type =hidden name ="mailTitolo" value="([^"]+)/.match(data)
92 title = CGI.unescape(md[1].strip)
93
94 authors = []
95 if md = /<INPUT type =HIDDEN name ="mailAutore" value="([^"]+)/.match(data)
96 md[1].strip.split(', ').each { |a| authors << CGI.unescape(a.strip) }
97 end
98
99 raise "No ISBN" unless md = /<INPUT type =HIDDEN name ="mailEAN" value="([^"]+)/.match(data)
100 isbn = md[1].strip
101 isbn += String( Library.ean_checksum( Library.extract_numbers( isbn ) ) )
102
103 #raise unless
104 md = /<INPUT type =HIDDEN name ="mailEditore" value="([^"]+)/.match(data)
105 publisher = CGI.unescape(md[1].strip) or md
106
107 #raise unless
108 md = /<INPUT type =HIDDEN name ="mailFormato" value="([^"]+)/.match(data)
109 edition = CGI.unescape(md[1].strip) or md
110
111 if md = /#{edition}\ \;\|\ \;(\d+)\ \;\|\ \;/.match(data)
112 nr_pages = CGI.unescape(md[1].strip)
113 elsif md = / (\d+) pagine \| /.match(data)
114 nr_pages = CGI.unescape(md[1].strip)
115 end
116 if nr_pages != "0" and nr_pages != nil
117 edition = nr_pages + " p., " + edition
118 end
119
120 publish_year = nil
121 if md = /<INPUT type =HIDDEN name ="mailAnnoPubbl" value="([^"]+)/.match(data)
122 publish_year = CGI.unescape(md[1].strip).to_i
123 publish_year = nil if publish_year == 0
124 end
125
126 cover_url = BASE_URI + "/bol/includes/tornaImmagine.jsp?cdSoc=BL&ean=" + isbn[0 .. 11] + "&tipoOggetto=PIB&cdSito=BL" # use "FRB" instead of "PIB" for smaller images
127 cover_filename = isbn + ".tmp"
128 Dir.chdir(CACHE_DIR) do
129 File.open(cover_filename, "w") do |file|
130 file.write open(cover_url, "Referer" => REFERER ).read
131 end
132 end
133
134 medium_cover = CACHE_DIR + "/" + cover_filename
135 if File.size(medium_cover) > 43 and File.size(medium_cover) != 2382 # 2382 is the size of the fake image "copertina non disponibile"
136 puts medium_cover + " has non-0 size" if $DEBUG
137 return [ Book.new(title, authors, isbn, publisher, publish_year, edition),medium_cover ]
138 end
139 puts medium_cover + " has 0 size, removing ..." if $DEBUG
140 File.delete(medium_cover)
141 return [ Book.new(title, authors, isbn, publisher, publish_year, edition) ]
142 end
143
144 def each_book_page(data)
145 raise if data.scan(/<a href="\/#{LOCALE}\/scheda\/ea(\d+)\.html;jsessionid=[^"]+">\s*Scheda completa\s*<\/a>/) { |a| yield a}.empty?
146 end
147
148 def clean_cache
149 #FIXME begin ... rescue ... end?
150 Dir.chdir(CACHE_DIR) do
151 Dir.glob("*.tmp") do |file|
152 puts "removing " + file if $DEBUG
153 File.delete(file)
154 end
155 end
156 end
157 end
158 end
159 end
Generated using the rcov code coverage analysis tool for Ruby version 0.8.0.