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 = "<div id=\"Results\" class=\"n2\">".getBytes(),
14        obidosASIN = "/dp/".getBytes(),
15        brBy = "</span></a>\n  \n  by ".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          skipTo(obidosASIN);
33          try {
34            BookStocking bookStocking = stockings.newStocking();
35  
36            bookStocking.setIsbn_unsafe(digits());
37            bookStocking.setVendorproductid_unsafe(bookStocking.getIsbn_unsafe());
38  
39            // we don't need this: it's computed from the ISBN = prod id
40            bookStocking.setDetailurl_unsafe("");
41  
42            skipTo("\"srTitle\">".getBytes());
43            bookStocking.setTitle_unsafe(plaintext());
44            /*
45  
46            skipTo(brBy);
47  
48            int author = here;
49            skipTo(dotNL);
50            bookStocking.setAuthor_unsafe(new String(text, author, here - author - 2));
51  
52            skipTo("\"binding\">".getBytes());
53            int format = here;
54            skipTo("</span>".getBytes());
55            bookStocking.setFormat_unsafe(new String(text, format, here - format - 1));
56            skipRealSpace();
57            skipTo(")</span>".getBytes());
58            int i = here - 7;
59            while (text[i] == '\n' || text[i] == ' ') --i;
60            bookStocking.setPublicationyear_unsafe(
61                text[i] == ')' ? new String(text, i - 4, 4) : "");
62  
63            skipTo((byte)'$');
64            --here;
65            bookStocking.setPrice_unsafe(plaintext());
66  
67            skipTo(slashTD);
68            skipTo(fontColor990000);
69            bookStocking.setDeliveryinfo_unsafe(plaintext());
70            */
71            System.err.println("Returning " + bookStocking);
72            return bookStocking;
73          }
74          catch (ParseException e) {
75            System.err.println(e);
76            // try again!
77          }
78        }
79      }
80      catch (ArrayIndexOutOfBoundsException e) {
81        System.err.println(e);
82      }
83      catch (ParseException e) {
84        System.err.println(e);
85      }
86  
87      return null;
88    }
89  
90    public static void main(String[] args) throws Exception {
91        BookStockingFactory dummy =
92            new BookStockingFactory() {
93              public BookStocking newStocking() {
94                return new BookStocking();
95              }
96            };
97  
98      SearchResults r = new SearchResults(IoUtils.slurp(new File(args[0]), 1000),
99                                          dummy);
100     while (r.hasMoreElements())
101       ((BookStocking)r.nextElement()).dump(System.out);
102   }
103 }