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 Rene Samselnig - Modified by Linus Zetterlund
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 # TODO:
19 # fix åäö
20
21
22 require 'net/http'
23 require 'cgi'
24
25 module Alexandria
26 class BookProviders
27 class AdlibrisProvider < GenericProvider
28 BASE_URI = "http://www.adlibris.se/"
29 def initialize
30 super("Adlibris", "Adlibris (Sweden)")
31 # no preferences for the moment
32 end
33
34 def search(criterion, type)
35 criterion = criterion.convert("ISO-8859-1", "UTF-8")
36 req = BASE_URI
37 if type == SEARCH_BY_ISBN
38 req += "product.aspx?isbn="+criterion+"&checked=1"
39 else
40 search_criterions = {}
41 search_criterions[type] = CGI.escape(criterion)
42 req = "http://www.adlibris.se/shop/search_result.asp?additem=&page=search%5Fresult%2Easp&search=advanced&format=&status=&ebook=&quickvalue=&quicktype=&isbn="+ search_criterions[SEARCH_BY_ISBN] + "&titleorauthor=&title="+search_criterions[SEARCH_BY_TITLE].to_s()+"&authorlast=&authorfirst=&keyword="+search_criterions[SEARCH_BY_KEYWORD].to_s()+"&publisher=&category=&language=&inventory1=1&inventory2=2&inventory4=4&inventory8=&get=&type=&sortorder=1&author="+search_criterions[SEARCH_BY_AUTHORS].to_s()+"&checked=1"
43 end
44
45
46 results = []
47
48 if type == SEARCH_BY_ISBN
49 #puts "if type == SEARCH_BY_ISBN"
50 #puts URI.parse(req)
51 data = transport.get(URI.parse(req))
52 return to_book_isbn(data, criterion) #rescue raise NoResultsError
53 else
54 begin
55 data = transport.get(URI.parse(req+"&row=1"))
56
57 regx = /shop\/product\.asp\?isbn=([^&]+?)&[^>]+>([^<]+?)<\/a>([^>]*?>){10}([^<]+?)<\/b>[^\)]+?\);\"\)>[\s]+?([^<\s]+?)<\/a>/
58
59 begin
60 data.scan(regx) do |md| next unless md[0] != md[1]
61
62 isbn = md[0].to_s()
63
64 imageAddr = nil
65 imgAddrMatch = data.scan(isbn+'.jpg')
66 if imgAddrMatch.length() == 2
67 imageAddr = 'http://www.adlibris.se/shop/images/'+isbn+'.jpg'
68 end
69
70 results << [Book.new(md[1].to_s(), # Title
71 [md[3].to_s()], # Authors
72 isbn,
73 nil, # Publisher
74 translate_stuff_stuff(md[4].to_s())), # Edition
75 imageAddr]
76 end
77 rescue => e
78 puts e.message
79 end
80
81 return results
82 rescue
83 raise NoResultsError
84 end
85 end
86 end
87
88 def url(book)
89 #puts "debug: url(book)"
90 BASE_URI + "product.aspx?isbn=" + book.isbn
91 end
92
93 #######
94 private
95 #######
96
97 def translate_html_stuff!(r)
98 r.sub!('å','Ã¥') # å
99 r.sub!('ä','ä') # ä
100 r.sub!('ö','ö') # ö
101 r.sub!('Å','Ã.') # Å
102 r.sub!('Ä','Ã.') # Ä
103 r.sub!('Ö','Ã.') # Ö
104 return r
105 end
106 def translate_html_stuff(s)
107 r = s
108 translate_html_stuff!(r)
109 return r
110 end
111
112
113 def translate_stuff_stuff!(r)
114 #r.sub!('\å','Ã¥') # å
115 #r.sub!('\ä','ä') # ä
116 #r.sub!('\ö','ö') # ö
117 #r.sub!('\Å','Ã.') # Å
118 #r.sub!('\Ä','Ã.') # Ä
119 #r.sub!('\Ö','Ã.') # Ö
120 return r
121 end
122 def translate_stuff_stuff(s)
123 r = s
124 translate_stuff_stuff!(r)
125 return r
126 end
127
128
129 def to_book_isbn(data, isbn)
130 #puts data
131 raise NoResultsError if /Ingen titel med detta ISBN finns hos AdLibris/.match(data) != nil
132 data = data.convert("UTF-8", "ISO-8859-1")
133
134 product = {}
135
136
137 raise "Title not found" unless md = /<a id="ctl00_main_frame_ctrlproduct_linkProductTitle" class="header15">(.+)<\/a>/.match(data)
138
139 product["title"] = CGI.unescape(md[1])
140
141
142 # regx = /<tr><td colspan="2" class="text">Författare: <b>([^<]*)<\/b><\/td><\/tr>/
143 regx = /<span id="ctl00_main_frame_ctrlproduct_rptAuthor_ctl0\d+_Label2">F.rfattare<\/span>: <a [^>]+>([^<]+)<\/a>/
144 product["authors"] = []
145 data.scan(regx) do |md| next unless md[0] != md[1]
146 product["authors"] << translate_html_stuff(CGI.unescape(md[0]))
147 end
148
149 #raise "Publisher string not found, but no \"book not found\" string found\n" unless
150 md = /<span id="ctl00_main_frame_ctrlproduct_lblPublisherName">(.+)<\/span>/.match(data)
151
152 product["publisher"] = md[1] or md #i.e., or nil
153
154
155 #raise "No edition" unless
156 md = /<span id="ctl00_main_frame_ctrlproduct_lblEditionAndWeight">([^<]*)i gram: .+<\/span>/.match(data)
157
158 product["edition"] = md[1] or md
159
160
161 md = /Utgiven: (\d\d\d\d)/.match(data)
162 # FIXME
163 # publish_year = either CGI.unescape(md[1].strip).to_i or md[1].to_i
164 # publish_year = nil if publish_year == 0
165
166 product["publish_year"] = md[1] or md
167
168
169 isbn10 = Library.canonicalise_isbn(isbn)
170 img_url = "covers/" + isbn10[0 .. 0] + "/" + isbn10[1 .. 2] + "/" + isbn10 + ".jpg"
171 #puts img_url
172 #raise "No image found" unless md = data.match(img_url)
173 product["cover"] = BASE_URI + img_url
174
175 book = Book.new(
176 translate_html_stuff(product["title"]),
177 product["authors"],
178 Library.canonicalise_ean(isbn),
179 translate_html_stuff(product["publisher"]),
180 product["publish_year"],
181 translate_html_stuff(product["edition"]))
182 return [ book, product["cover"] ]
183 end
184
185 end
186 end
187 end
Generated using the rcov code coverage analysis tool for Ruby version 0.8.0.