View Javadoc

1   package org.paneris.bibliomania.metasearch.amazon;
2   
3   import java.io.File;
4   
5   import org.melati.util.IoUtils;
6   import org.paneris.bibliomania.BookStocking;
7   import org.paneris.bibliomania.metasearch.BookStockingFactory;
8   import org.paneris.bibliomania.metasearch.util.SearchResultsBase;
9   
10  public class SearchResults extends SearchResultsBase {
11  
12    protected static final byte[]
13        resultsSortedBy = "Results sorted by".getBytes(),
14        obidosASIN = "obidos/ASIN/".getBytes(),
15        brBy = "<br>\nby ".getBytes(),
16        dotNL = ".\n".getBytes(),
17        spc3 = "   ".getBytes(),
18        spc2 = "  ".getBytes(),
19        slashTD = "</td>".getBytes(),
20        fontColor990000 = "<font color=#990000>".getBytes();
21  
22    public SearchResults(byte[] text,
23                         BookStockingFactory stockings) {
24      super(text, stockings);
25      skipTo(resultsSortedBy);
26    }
27  
28    protected Object more() {
29      try {
30        for (;;) {
31          skipTo(obidosASIN);
32          try {
33            BookStocking book = stockings.newStocking();
34  
35            book.setIsbn_unsafe(digits());
36            book.setVendorproductid_unsafe(book.getIsbn_unsafe());
37  
38            // we don't need this: it's computed from the ISBN = prod id
39            book.setDetailurl_unsafe("");
40  
41            skipTo((byte)'>');
42            book.setTitle_unsafe(plaintext());
43  
44            skipTo(brBy);
45  
46            int author = here;
47            skipTo(dotNL);
48            book.setAuthor_unsafe(new String(text, author, here - author - 2));
49  
50            skipRealSpace();
51            int format = here;
52            skipTo((byte)'\n');
53            book.setFormat_unsafe(new String(text, format, here - format - 1));
54            skipRealSpace();
55  
56            skipTo(slashTD);
57            int i = here - 7;
58            while (text[i] == '\n' || text[i] == ' ') --i;
59            book.setPublicationyear_unsafe(
60                text[i] == ')' ? new String(text, i - 4, 4) : "");
61  
62            skipTo((byte)'$');
63            --here;
64            book.setPrice_unsafe(plaintext());
65  
66            skipTo(slashTD);
67            skipTo(fontColor990000);
68            book.setDeliveryinfo_unsafe(plaintext());
69  
70            return book;
71          }
72          catch (ParseException e) {
73            // try again!
74          }
75        }
76      }
77      catch (ArrayIndexOutOfBoundsException e) {
78      }
79      catch (ParseException e) {
80      }
81  
82      return null;
83    }
84  
85    public static void main(String[] args) throws Exception {
86        BookStockingFactory dummy =
87            new BookStockingFactory() {
88              public BookStocking newStocking() {
89                return new BookStocking();
90              }
91            };
92  
93      SearchResults r = new SearchResults(IoUtils.slurp(new File(args[0]), 1000),
94                                          dummy);
95      while (r.hasMoreElements())
96        ((BookStocking)r.nextElement()).dump(System.out);
97    }
98  }