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) 2005-2006 Claudio Belotti
2 #
3 # Alexandria is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License as
5 # published by the Free Software Foundation; either version 2 of the
6 # License, or (at your option) any later version.
7 #
8 # Alexandria is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public
14 # License along with Alexandria; see the file COPYING. If not,
15 # write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 # Boston, MA 02111-1307, USA.
17
18 require 'fileutils'
19 require 'net/http'
20 require 'open-uri'
21 #require 'cgi'
22
23 module Alexandria
24 class BookProviders
25 class IBS_itProvider < GenericProvider
26 BASE_URI = "http://www.internetbookshop.it"
27 CACHE_DIR = File.join(Alexandria::Library::DIR, '.ibs_it_cache')
28 REFERER = BASE_URI
29 def initialize
30 super("IBS_it", "Internet Bookshop (Italy)")
31 FileUtils.mkdir_p(CACHE_DIR) unless File.exists?(CACHE_DIR)
32 # no preferences for the moment
33 at_exit { clean_cache }
34 end
35
36 def search(criterion, type)
37 criterion = criterion.convert("ISO-8859-1", "UTF-8")
38 req = BASE_URI + "/ser/"
39 req += case type
40 when SEARCH_BY_ISBN
41 "serdsp.asp?isbn="
42
43 when SEARCH_BY_TITLE
44 "serpge.asp?Type=keyword&T="
45
46 when SEARCH_BY_AUTHORS
47 "serpge.asp?Type=keyword&A="
48
49 when SEARCH_BY_KEYWORD
50 "serpge.asp?Type=keyword&S="
51
52 else
53 raise InvalidSearchTypeError
54
55 end
56
57 req += CGI.escape(criterion)
58 p req if $DEBUG
59 data = transport.get(URI.parse(req))
60 if type == SEARCH_BY_ISBN
61 to_book(data) #rescue raise NoResultsError
62 else
63 begin
64 results = []
65 each_book_page(data) do |code, title|
66 results << to_book(transport.get(URI.parse("http://www.internetbookshop.it/ser/serdsp.asp?cc=" + code)))
67 end
68 return results
69 rescue
70 raise NoResultsError
71 end
72 end
73 end
74
75 def url(book)
76 "http://www.internetbookshop.it/ser/serdsp.asp?isbn=" + book.isbn
77 end
78
79 #######
80 private
81 #######
82
83 def to_book(data)
84 raise NoResultsError if /<b>Il libro che hai cercato non è presente nel nostro catalogo<\/b><br>/.match(data) != nil
85 data = data.convert("UTF-8", "ISO-8859-1")
86
87 raise "No title" unless md = />Titolo<\/td><td valign="top" class="lbarrasup">([^<]+)/.match(data)
88 title = CGI.unescape(md[1].strip)
89
90 authors = []
91 if md = /<b>Autore<\/b><\/td>.+<b>([^<]+)/.match(data)
92 md[0].strip.gsub(/<.*?>|Autore/m, ' ').split('; ').each { |a| authors << CGI.unescape(a.strip) }
93 end
94
95 raise "No ISBN" unless md = /<input type=\"hidden\" name=\"isbn\" value=\"([^"]+)\">/i.match(data)
96 isbn = md[1].strip
97
98 #raise "No publisher" unless
99 md = /<b>Editore<\/b><\/td>.+<b>([^<]+)/.match(data)
100 publisher = CGI.unescape(md[1].strip) or md
101
102 #raise "No edition" unless
103 md = /Dati<\/b><\/td><td valign="top">([^<]+)/.match(data)
104 edition = CGI.unescape(md[1].strip) or md
105
106 publish_year = nil
107 if md = /Anno<\/b><\/td><td valign="top">([^<]+)/.match(data)
108 publish_year = CGI.unescape(md[1].strip).to_i
109 publish_year = nil if publish_year == 0
110 end
111
112 md = /src="http:\/\/giotto.ibs.it\/cop\/copt13.asp\?f=(\d+)">/.match(data)
113 cover_url = "http://giotto.ibs.it/cop/copt13.asp?f=" + md[1] # use copa13.asp, copt13.asp, copj13.asp, for small, medium, big image
114 cover_filename = isbn + ".tmp"
115 Dir.chdir(CACHE_DIR) do
116 File.open(cover_filename, "w") do |file|
117 file.write open(cover_url, "Referer" => REFERER ).read
118 end
119 end
120
121 medium_cover = CACHE_DIR + "/" + cover_filename
122 if File.size(medium_cover) > 0 and File.size(medium_cover) != 1822 # 1822 is the size of the fake image "copertina non disponibile"
123 puts medium_cover + " has non-0 size" if $DEBUG
124 return [ Book.new(title, authors, isbn, publisher, publish_year, edition),medium_cover ]
125 end
126 puts medium_cover + " has 0 size, removing ..." if $DEBUG
127 File.delete(medium_cover)
128 return [ Book.new(title, authors, isbn, publisher, publish_year, edition) ]
129 end
130
131 def each_book_page(data)
132 raise if data.scan(/<a href="http:\/\/www.internetbookshop.it\/ser\/serdsp.asp\?shop=1&cc=([\w\d]+)"><b>([^<]+)/) { |a| yield a}.empty?
133 end
134
135 def clean_cache
136 #FIXME begin ... rescue ... end?
137 Dir.chdir(CACHE_DIR) do
138 Dir.glob("*.tmp") do |file|
139 puts "removing " + file if $DEBUG
140 File.delete(file)
141 end
142 end
143 end
144 end
145 end
146 end
Generated using the rcov code coverage analysis tool for Ruby version 0.8.0.