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 begin
22 # rubygems may be required or not by hpricot (used by mechanize), and may be installed or not
23 require 'rubygems'
24 rescue LoadError
25 end
26 require 'mechanize'
27 #require 'cgi'
28
29 module Alexandria
30 class BookProviders
31 class DeaStore_itProvider < GenericProvider
32 BASE_URI = "http://www.deastore.com"
33 CACHE_DIR = File.join(Alexandria::Library::DIR, '.deastore_it_cache')
34 def initialize
35 super("DeaStore_it", "DeaStore (Italy)")
36 FileUtils.mkdir_p(CACHE_DIR) unless File.exists?(CACHE_DIR)
37 # no preferences for the moment
38 at_exit { clean_cache }
39 end
40
41 def search(criterion, type)
42 criterion = criterion.convert("windows-1252", "UTF-8")
43 req = BASE_URI + "/"
44 req += case type
45 when SEARCH_BY_ISBN
46 "product.asp?isbn="
47
48 when SEARCH_BY_TITLE
49 "ricerche.asp?quick_search=ok&order_by=relevance&query_field=allbooks&query_string="
50
51 when SEARCH_BY_AUTHORS
52 "ricerche.asp?quick_search=ok&order_by=relevance&query_field=allbooks&query_string="
53
54 when SEARCH_BY_KEYWORD
55 "ricerche.asp?quick_search=ok&order_by=relevance&query_field=allbooks&query_string="
56
57 else
58 raise InvalidSearchTypeError
59
60 end
61
62 req += CGI.escape(criterion)
63 p req if $DEBUG
64
65 agent = WWW::Mechanize.new
66 agent.user_agent_alias = 'Mac Safari'
67 #data = transport.get(URI.parse(req))
68 data = agent.get(URI.parse(req)).content
69
70 if type == SEARCH_BY_ISBN
71 to_book(data) #rescue raise NoResultsError
72 else
73 begin
74 results = []
75 each_book_page(data) do |code, title|
76 agent = WWW::Mechanize.new
77 agent.user_agent_alias = 'Mac Safari'
78 results << to_book(agent.get(URI.parse(BASE_URI + "/" + code)).content)
79 end
80 return results
81 rescue
82 raise NoResultsError
83 end
84 end
85 end
86
87 def url(book)
88 BASE_URI + "/product.asp?isbn=" + book.isbn
89 end
90
91 #######
92 private
93 #######
94
95 def to_book(data)
96 raise NoResultsError if /<span class="EtichetteForms">No results\. Type new data in the previous page and try again\.<br \/>/.match(data) != nil
97 data = data.convert("UTF-8", "windows-1252")
98
99 raise "No title." unless md = /<span class="BDtitoloLibro">([^<]+)/.match(data)
100 title = CGI.unescape(md[1].strip)
101
102 authors = []
103 # this returns "Name Surname"
104 # if md = /<span class="BDauthLibro">by:([^<]+)/.match(data)
105 # this returns "Surname, Name"
106 if md = /<span class="BDEticLibro">Authorship<\/span><br \/>by:.([^<]+)/.match(data)
107 if md2 = /(.+)Translator:.+/.match(md[1])
108 md=md2
109 end
110 md[1].strip.split('- ').each { |a| authors << CGI.unescape(a.strip) }
111 end
112
113 raise "No ISBN" unless md = /<span class="BDEticLibro">ISBN 13: <\/span><span class="isbn">([^<]+)/.match(data)
114 isbn = md[1].strip.gsub!("-","")
115
116 #raise "No Publisher" unless
117 md = /<span class="BDeditoreLibro">([^<]+)/.match(data)
118 publisher = CGI.unescape(md[1].strip) or md
119
120 unless md = /<span class="BDEticLibro">More info<\/span><br \/>([^<]+)/.match(data)
121 edition = nil
122 else
123 edition = CGI.unescape(md[1].strip)
124 end
125
126 publish_year = nil
127 if md = /<span class="BDdataPubbLibro">([^<]+)/.match(data)
128 publish_year = CGI.unescape(md[1].strip)[-4 .. -1].to_i
129 publish_year = nil if publish_year == 0 or publish_year == 1900
130 end
131
132 if md = /<div class="imageLg"><a href="javascript:void\(''\);" onclick="popUpCover\('\/covers_13\/([0-9\/]+)batch/.match(data)
133 cover_url = BASE_URI + "/covers_13/" + md[1].strip + "/batch1/" + isbn + ".jpg" # use batch2 or batch3 for bigger images
134
135 cover_filename = isbn + ".tmp"
136 Dir.chdir(CACHE_DIR) do
137 File.open(cover_filename, "w") do |file|
138 agent = WWW::Mechanize.new
139 agent.user_agent_alias = 'Mac Safari'
140 file.write agent.get(URI.parse(cover_url)).content
141 end
142 end
143
144 medium_cover = CACHE_DIR + "/" + cover_filename
145 if File.size(medium_cover) > 0
146 puts medium_cover + " has non-0 size" if $DEBUG
147 return [ Book.new(title, authors, isbn, publisher, publish_year, edition),medium_cover ]
148 end
149 puts medium_cover + " has 0 size, removing ..." if $DEBUG
150 File.delete(medium_cover)
151 end
152 return [ Book.new(title, authors, isbn, publisher, publish_year, edition) ]
153 end
154
155 def each_book_page(data)
156 raise if data.scan(/<span class="BDtitoloLibro"><a href="([^"]+)/) { |a| yield a}.empty?
157 end
158
159 def clean_cache
160 #FIXME begin ... rescue ... end?
161 Dir.chdir(CACHE_DIR) do
162 Dir.glob("*.tmp") do |file|
163 puts "removing " + file if $DEBUG
164 File.delete(file)
165 end
166 end
167 end
168 end
169 end
170 end
Generated using the rcov code coverage analysis tool for Ruby version 0.8.0.