Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * Hasan Diwan licenses this file to you under the Apache License, Version 2.0
  3.  * (the "License"); you may not use this file except in compliance with
  4.  * the License.  
  5.  *
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *
  16.  * My only request beyond the license is that you let me know how you use this package, and,
  17.  * if you should make any improvements or find/fix bugs, to let me know. Many thanks!
  18.  * @author Hasan Diwan
  19.  */
  20. package us.d8u.ebay;
  21.  
  22. import java.awt.BorderLayout;
  23. import java.awt.HeadlessException;
  24. import java.awt.Toolkit;
  25. import java.awt.datatransfer.Clipboard;
  26. import java.awt.datatransfer.StringSelection;
  27. import java.awt.event.ActionEvent;
  28. import java.awt.event.ActionListener;
  29. import java.awt.event.InputEvent;
  30. import java.awt.event.MouseAdapter;
  31. import java.awt.event.MouseEvent;
  32. import java.io.File;
  33. import java.io.FileInputStream;
  34. import java.io.FileNotFoundException;
  35. import java.io.IOException;
  36. import java.io.UnsupportedEncodingException;
  37. import java.net.URLEncoder;
  38. import java.security.MessageDigest;
  39. import java.security.NoSuchAlgorithmException;
  40. import java.text.DecimalFormat;
  41. import java.text.ParseException;
  42. import java.util.ArrayList;
  43. import java.util.Collections;
  44. import java.util.Comparator;
  45. import java.util.HashSet;
  46. import java.util.List;
  47. import java.util.Set;
  48. import java.util.Vector;
  49.  
  50. import javax.swing.JFrame;
  51. import javax.swing.JMenuItem;
  52. import javax.swing.JOptionPane;
  53. import javax.swing.JPanel;
  54. import javax.swing.JPopupMenu;
  55. import javax.swing.JScrollPane;
  56. import javax.swing.JTable;
  57. import javax.swing.JTextArea;
  58. import javax.swing.JTextField;
  59. import javax.swing.SwingUtilities;
  60. import javax.swing.event.TableModelEvent;
  61. import javax.swing.table.DefaultTableModel;
  62. import javax.swing.table.TableColumn;
  63. import javax.xml.parsers.ParserConfigurationException;
  64. import javax.xml.parsers.SAXParser;
  65. import javax.xml.parsers.SAXParserFactory;
  66.  
  67. import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
  68. import org.apache.http.HttpEntity;
  69. import org.apache.http.HttpResponse;
  70. import org.apache.http.client.ClientProtocolException;
  71. import org.apache.http.client.methods.HttpGet;
  72. import org.apache.http.impl.client.DefaultHttpClient;
  73. import org.apache.http.util.EntityUtils;
  74. import org.apache.log4j.Logger;
  75. import org.joda.time.DateTime;
  76. import org.joda.time.format.DateTimeFormat;
  77. import org.joda.time.format.DateTimeFormatter;
  78. import org.joda.time.format.ISODateTimeFormat;
  79. import org.xml.sax.Attributes;
  80. import org.xml.sax.SAXException;
  81. import org.xml.sax.helpers.DefaultHandler;
  82.  
  83. import edu.stanford.ejalbert.BrowserLauncher;
  84. import edu.stanford.ejalbert.exception.BrowserLaunchingInitializingException;
  85. import edu.stanford.ejalbert.exception.UnsupportedOperatingSystemException;
  86.  
  87. /**
  88.  * This is the main/only class for ebay
  89.  *
  90.  */
  91. public class App {
  92.     static final String EBAY_URL = "http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.0.0&SECURITY-APPNAME=ERM089df1-6a9f-427c-813c-51c02819f1d&RESPONSE-DATA-FORMAT=XML&REST-PAYLOAD&keywords=";
  93.     static final String EBAY_QUERY_KEY = "us.d8u.ebay.queryKey";
  94.     static final DefaultHttpClient client = new DefaultHttpClient();
  95.     protected static final Logger logger = Logger.getLogger("us.d8u.ebay.App");
  96.     protected static final int LOCATION_INDEX = 2, DATE_INDEX = 3,
  97.         TITLE_INDEX = 0, PRICE_INDEX = 1;
  98.  
  99.     private static boolean updateCheck() {
  100.         return true;
  101.     }
  102.  
  103.     public static void main(final String[] args) {
  104.         try {
  105.             if ((System.getProperty("debug") == null) && !updateCheck()) {
  106.                 JOptionPane
  107.                     .showMessageDialog(null,
  108.                                        "Update available -- replace code with http://bit.ly/13MNvdl");
  109.             }
  110.         } catch (HeadlessException e2) {
  111.             logger.fatal("Need graphics, dying...");
  112.             return;
  113.         }
  114.         final DefaultTableModel model = new DefaultTableModel() {
  115.                 /**
  116.                  *
  117.                  */
  118.                 private static final long serialVersionUID = -3223921605077026L;
  119.  
  120.                 @Override
  121.                 public boolean isCellEditable(int row, int column) {
  122.                     return false;
  123.                 }
  124.             };
  125.         final Vector<Vector<String>> data = new Vector<Vector<String>>();
  126.         final Vector<String> headers = new Vector<String>();
  127.         headers.add("Item title");
  128.         headers.add("Price");
  129.         headers.add("Link");
  130.         headers.add("Ends at");
  131.         final JFrame mainFrame = new JFrame("eBay");
  132.         final Set<Double> _datasetSource = new HashSet<Double>();
  133.         _datasetSource.clear();
  134.         final JTextArea statisticsArea = new JTextArea();
  135.         statisticsArea.setEditable(false);
  136.         statisticsArea.setToolTipText(App.getStatisticsHelp());
  137.         final JTextField searchField = new JTextField();
  138.        
  139.         searchField.setEditable(true);
  140.         JPanel searchPanel = new JPanel();
  141.         searchPanel.setLayout(new BorderLayout());
  142.         searchPanel.add(searchField, BorderLayout.NORTH);
  143.         searchPanel.setSize(mainFrame.getSize());
  144.         searchField.setSize(searchPanel.getSize());
  145.         final JTable tbl = new JTable();
  146.         final JPopupMenu ctxMenu = new JPopupMenu();
  147.         final JMenuItem copyItem = new JMenuItem(
  148.                                                  "Copy data to clipboard in Excel (CSV) format");
  149.         tbl.setSize(mainFrame.getSize());
  150.         searchField.addActionListener(new ActionListener() {
  151.                 @Override
  152.                 public void actionPerformed(final ActionEvent arg0) {
  153.                    
  154.                     System.setProperty(EBAY_QUERY_KEY,
  155.                                        (String) searchField.getText());
  156.                     String query = new String();
  157.                     try {
  158.                         query = URLEncoder.encode(
  159.                                                   (String) searchField.getText(), "utf-8");
  160.                     } catch (UnsupportedEncodingException e1) {
  161.                         logger.fatal(e1.getMessage(), e1);
  162.                     }
  163.                     HttpGet getMethod = new HttpGet(String.format("%s%s", EBAY_URL,
  164.                                                                   query));
  165.                     HttpResponse response = null;
  166.                     try {
  167.                         response = client.execute(getMethod);
  168.                     } catch (ClientProtocolException e1) {
  169.                         logger.fatal(e1.getMessage(), e1);
  170.                     } catch (IOException e1) {
  171.                         logger.fatal(e1.getMessage(), e1);
  172.                     }
  173.                     final HttpEntity entity = response.getEntity();
  174.                     SAXParserFactory factory = SAXParserFactory.newInstance();
  175.                     SAXParser parser = null;
  176.                     try {
  177.                         parser = factory.newSAXParser();
  178.                     } catch (ParserConfigurationException e1) {
  179.                         logger.fatal(e1.getMessage(), e1);
  180.                     } catch (SAXException e1) {
  181.                         logger.fatal(e1.getMessage(), e1);
  182.                     }
  183.                     try {
  184.                         parser.parse(entity.getContent(), new DefaultHandler() {
  185.                                 private String relevant;
  186.                                 final int LINK_INDEX = 1, PRICE_INDEX = 2;
  187.  
  188.                                 @Override
  189.                                 public void endDocument() {
  190.                                     statisticsArea
  191.                                         .setText("Data obtained, sorting by price");
  192.                                     model.setDataVector(data, headers);
  193.                                     List<Vector<String>> dataList = new ArrayList<Vector<String>>(
  194.                                                                                                   data);
  195.                                     Collections.sort(dataList,
  196.                                                      new Comparator<Vector<String>>() {
  197.                                                          @Override
  198.                                                              public int compare(Vector<String> o1,
  199.                                                                                 Vector<String> o2) {
  200.                                                              int result = 0;
  201.                                                              DateTimeFormatter endTimeFormatter = DateTimeFormat
  202.                                                                  .forPattern("yyyy-MM-dd'T'HH:mm:ss");
  203.                                                              DecimalFormat formatter = (DecimalFormat) DecimalFormat
  204.                                                                  .getCurrencyInstance();
  205.                                                              DateTime dt1 = endTimeFormatter
  206.                                                                  .parseDateTime(o1
  207.                                                                                 .get(3)
  208.                                                                                 .substring(
  209.                                                                                            0,
  210.                                                                                            o1.get(3)
  211.                                                                                            .length() - 10));
  212.                                                              DateTime dt2 = endTimeFormatter
  213.                                                                  .parseDateTime(o2
  214.                                                                                 .get(3)
  215.                                                                                 .substring(
  216.                                                                                            0,
  217.                                                                                            o1.get(3)
  218.                                                                                            .length() - 10));
  219.                                                              result = dt1.compareTo(dt2);
  220.                                                              if (result == 0) {
  221.                                                                  Double d1 = null;
  222.                                                                  try {
  223.                                                                      d1 = (Double) formatter
  224.                                                                          .parse(o1.get(1))
  225.                                                                          .doubleValue();
  226.                                                                  } catch (ParseException e) {
  227.                                                                      logger.fatal(
  228.                                                                                   e.getMessage(), e);
  229.                                                                  }
  230.                                                                  Double d2 = null;
  231.                                                                  try {
  232.                                                                      d2 = (Double) formatter
  233.                                                                          .parse(o2.get(1))
  234.                                                                          .doubleValue();
  235.                                                                  } catch (ParseException e) {
  236.                                                                      logger.fatal(
  237.                                                                                   e.getMessage(), e);
  238.                                                                  }
  239.                                                                  result = d2.compareTo(d1);
  240.                                                              }
  241.                                                              return result;
  242.                                                          }
  243.                                                      });
  244.                                     data.removeAll(dataList);
  245.                                     data.addAll(dataList);
  246.                                     SummaryStatistics stats = new SummaryStatistics();
  247.                                     DecimalFormat decimalFormatter = (DecimalFormat) DecimalFormat
  248.                                         .getCurrencyInstance();
  249.                                     final StringBuffer csvBuffer = new StringBuffer();
  250.                                     csvBuffer.append("Auction Title, Price\n");
  251.                                     for (final Vector<String> auction : data) {
  252.                                         try {
  253.                                             final double value = decimalFormatter
  254.                                                 .parse(auction.get(1))
  255.                                                 .doubleValue();
  256.                                             stats.addValue(value);
  257.                                             csvBuffer.append(String.format(
  258.                                                                            "\"%s\", %.2f\n",
  259.                                                                            auction.get(TITLE_INDEX)
  260.                                                                            .replaceAll(",", "\\,"),
  261.                                                                            value));
  262.                                             _datasetSource.add(value);
  263.                                         } catch (ParseException e) {
  264.                                             logger.fatal(e.getMessage(), e);
  265.                                         }
  266.                                     }
  267.                                     copyItem.addActionListener(new ActionListener() {
  268.                                             @Override
  269.                                             public void actionPerformed(ActionEvent arg0) {
  270.                                                 String _csv = csvBuffer.toString();
  271.                                                 StringSelection csv = new StringSelection(
  272.                                                                                           _csv);
  273.                                                 Toolkit tk = Toolkit.getDefaultToolkit();
  274.                                                 Clipboard clipboard = tk
  275.                                                     .getSystemClipboard();
  276.                                                 clipboard.setContents(csv, null);
  277.                                             }
  278.                                         });
  279.                                     ctxMenu.add(copyItem);
  280.                                     tbl.addMouseListener(new MouseAdapter() {
  281.                                             @Override
  282.                                             public void mouseClicked(MouseEvent arg0) {
  283.                                                 if (arg0.getClickCount() == 2) {
  284.                                                     JTable target = (JTable) arg0
  285.                                                         .getSource();
  286.                                                     int row = target.getSelectedRow();
  287.                                                     DefaultTableModel ourColumnModel = (DefaultTableModel) target
  288.                                                         .getModel();
  289.                                                     logger.warn((String) ourColumnModel.getValueAt(row, LOCATION_INDEX));
  290.                                                     Toolkit tk = Toolkit
  291.                                                         .getDefaultToolkit();
  292.                                                     Clipboard pb = tk.getSystemClipboard();
  293.                                                     StringSelection toClipboard = new StringSelection(
  294.                                                                                                       (String) ourColumnModel
  295.                                                                                                       .getValueAt(row,
  296.                                                                                                                   LOCATION_INDEX));
  297.                                                     pb.setContents(toClipboard, null);
  298.                                                 }
  299.                                                 if (SwingUtilities.isRightMouseButton(arg0)
  300.                                                     || arg0.getModifiers() == InputEvent.CTRL_MASK) {
  301.  
  302.                                                     ctxMenu.show((JTable) arg0.getSource(),
  303.                                                                  arg0.getX(), arg0.getY());
  304.                                                 }
  305.                                             }
  306.  
  307.                                             private void logClick(String valueAt) {
  308.                                             }
  309.                                         });
  310.                                     statisticsArea.setText(stats.toString());
  311.                                     tbl.setModel(model);
  312.                                     tbl.getColumnModel()
  313.                                         .getColumn(TITLE_INDEX)
  314.                                         .setMaxWidth(
  315.                                                      mainFrame.getContentPane()
  316.                                                      .getWidth());
  317.                                     tbl.getColumnModel().getColumn(1).setMinWidth(0);
  318.                                     tbl.getColumnModel()
  319.                                         .getColumn(1)
  320.                                         .setMaxWidth(
  321.                                                      mainFrame.getContentPane()
  322.                                                      .getWidth() / 2);
  323.                                     resize(tbl.getColumnModel().getColumn(2));
  324.                                     resize(tbl.getColumnModel().getColumn(3));
  325.                                     model.fireTableChanged(new TableModelEvent(model,
  326.                                                                                TableModelEvent.INSERT));
  327.  
  328.                                     try {
  329.                                         EntityUtils.consume(entity);
  330.                                     } catch (IOException e) {
  331.                                         logger.fatal(e.getMessage(), e);
  332.                                     }
  333.                                 }
  334.  
  335.                                 private void resize(TableColumn column) {
  336.                                     column.setMinWidth(0);
  337.                                     column.setMaxWidth(0);
  338.                                 }
  339.  
  340.                                 @Override
  341.                                 public void startDocument() {
  342.                                     data.clear();
  343.                                     statisticsArea.setText("Parsing XML");
  344.                                 }
  345.  
  346.                                 String element;
  347.  
  348.                                 @Override
  349.                                 public void startElement(String ns, String localName,
  350.                                                          String qName, Attributes attrs) {
  351.                                     element = qName;
  352.                                 }
  353.  
  354.                                 @Override
  355.                                 public void characters(char[] ch, int begin, int end) {
  356.                                     String relevant = new String(ch, begin, end);
  357.                                     this.relevant = this.relevant + relevant;
  358.                                 }
  359.  
  360.                                 Vector<String> itemData = new Vector<String>();
  361.  
  362.                                 @Override
  363.                                 public void endElement(String ns, String localName,
  364.                                                        String name) {
  365.                                     String qName = element;
  366.                                     if (qName.equals("itemId")) {
  367.                                         try {
  368.                                             String link = itemData.get(LINK_INDEX);
  369.                                             String price = itemData.get(PRICE_INDEX);
  370.                                             itemData.set(LINK_INDEX, price);
  371.                                             itemData.set(PRICE_INDEX, link);
  372.                                             data.add(itemData);
  373.                                         } catch (ArrayIndexOutOfBoundsException e) {
  374.                                             // take care of first element, where
  375.                                             // collection is empty
  376.                                         } finally {
  377.                                             itemData = new Vector<String>();
  378.                                         }
  379.                                     } else if (qName.equals("convertedCurrentPrice")) {
  380.                                         Double price = new Double(relevant);
  381.  
  382.                                         DecimalFormat nf = (DecimalFormat) DecimalFormat
  383.                                             .getCurrencyInstance();
  384.                                         itemData.add(nf.format(price));
  385.                                     } else if (qName.equals("title")) {
  386.                                         itemData.add(relevant);
  387.                                     } else if (qName.equals("endTime")) {
  388.                                         DateTimeFormatter formatter = ISODateTimeFormat
  389.                                             .dateTime();
  390.                                         DateTime date = formatter
  391.                                             .parseDateTime(relevant);
  392.                                         itemData.add(date.toString());
  393.                                     } else if (qName.equals("viewItemURL")) {
  394.                                         itemData.add(relevant);
  395.                                     }
  396.                                     this.relevant = "";
  397.                                 }
  398.                             });
  399.  
  400.                     } catch (IllegalStateException e) {
  401.                         logger.fatal(e.getMessage(), e);
  402.                     } catch (SAXException e) {
  403.                         logger.fatal(e.getMessage(), e);
  404.                     } catch (IOException e) {
  405.                         logger.fatal(e.getMessage(), e);
  406.                     }
  407.  
  408.                 }
  409.             });
  410.         JScrollPane scrollPane = new JScrollPane(tbl);
  411.         tbl.setSize(scrollPane.getSize());
  412.         scrollPane.setSize(mainFrame.getSize());
  413.         mainFrame.setLayout(new BorderLayout());
  414.         mainFrame.add(searchPanel, BorderLayout.NORTH);
  415.         mainFrame.add(scrollPane, BorderLayout.CENTER);
  416.         mainFrame.add(statisticsArea, BorderLayout.SOUTH);
  417.         mainFrame.pack();
  418.         logger.info(mainFrame.getSize().width);
  419.         mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  420.         mainFrame.setVisible(true);
  421.     }
  422.  
  423.     private static String getIpV4Address() {
  424.         DefaultHttpClient client = new DefaultHttpClient();
  425.         HttpGet getMethod = new HttpGet("http://api.externalip.net/ip");
  426.         HttpResponse response = null;
  427.         try {
  428.             response = client.execute(getMethod);
  429.         } catch (ClientProtocolException e1) {
  430.             logger.fatal(e1.getMessage(), e1);
  431.         } catch (IOException e1) {
  432.             logger.fatal(e1.getMessage(), e1);
  433.         }
  434.         String ipv4Address = null;
  435.         try {
  436.             ipv4Address = EntityUtils.toString(response.getEntity());
  437.         } catch (org.apache.http.ParseException e) {
  438.             logger.fatal(e.getMessage(), e);
  439.         } catch (IOException e) {
  440.             logger.fatal(e.getMessage(), e);
  441.         }
  442.         return ipv4Address;
  443.     }
  444.  
  445.     private static String getStatisticsHelp() {
  446.         String ret = "<html>N - number of entries used to calculate the rest of the stuff<br>"
  447.             + "Min - minimum price<br>"
  448.             + "Max - maximum price<br>"
  449.             + "Mean - sum of prices/number of entries<br>"
  450.             + "Geometric Mean - nth root of the product of n prices<br>"
  451.             + "Variance - how far the prices are spread out<br>"
  452.             + "Sum of Squres - sum of the difference of the prices and their mean<br>"
  453.             + "Standard Deviation - another measure of the variance<br>"
  454.             + "Sum of Logs - sum of the logs to base e</html>";
  455.         return ret;
  456.     }
  457. }