View Javadoc

1   /*------------------------------------------------------------------------------
2    * The contents of this file are subject to the Mozilla Public License Version
3    * 1.1 (the "License"); you may not use this file except in compliance with
4    * the License. You may obtain a copy of the License at
5    * http://www.mozilla.org/MPL/
6    * Software distributed under the License is distributed on an "AS IS" basis,
7    * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
8    * the specific language governing rights and limitations under the License.
9    *
10   * The Original Code is levelonelabs.com code.
11   * The Initial Developer of the Original Code is Level One Labs. Portions
12   * created by the Initial Developer are Copyright (C) 2001 the Initial
13   * Developer. All Rights Reserved.
14   *
15   *         Contributor(s):
16   *             Scott Oster      (ostersc@alum.rpi.edu)
17   *             Steve Zingelwicz (sez@po.cwru.edu)
18   *             William Gorman   (willgorman@hotmail.com)
19   *
20   * Alternatively, the contents of this file may be used under the terms of
21   * either the GNU General Public License Version 2 or later (the "GPL"), or
22   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
23   * in which case the provisions of the GPL or the LGPL are applicable
24   * instead of those above. If you wish to allow use of your version of this
25   * file only under the terms of either the GPL or the LGPL, and not to allow
26   * others to use your version of this file under the terms of the NPL, indicate
27   * your decision by deleting the provisions above and replace them with the
28   * notice and other provisions required by the GPL or the LGPL. If you do not
29   * delete the provisions above, a recipient may use your version of this file
30   * under the terms of any one of the NPL, the GPL or the LGPL.
31   *----------------------------------------------------------------------------*/
32  
33  /***
34   * StockModule.java
35   *
36   * @created Oct 26, 2002
37   */
38  package com.levelonelabs.aimbot.modules;
39  
40  import java.io.BufferedReader;
41  import java.io.InputStreamReader;
42  
43  import java.net.URL;
44  import java.net.URLConnection;
45  
46  import java.util.ArrayList;
47  import java.util.StringTokenizer;
48  
49  import com.levelonelabs.aim.AIMBuddy;
50  import com.levelonelabs.aimbot.AIMBot;
51  import com.levelonelabs.aimbot.BotModule;
52  
53  /***
54   * Handles requests to get stock quotes
55   *
56   * @author Scott Oster
57   *
58   * @created Oct 26, 2002
59   */
60  public class StockModule extends BotModule {
61      private static ArrayList services;
62      /***
63       * http://finance.yahoo.com/d/quotes.csv?f=snl1d1t1c1ohgv&s=GE (s= is the symbol to get, f= is
64       * the order of the info)  Here is list of known variables that can be passed as options for
65       * "f=" s  = Symbol                   l9 = Last Trade d1 = Date of Last Trade       t1 = Time
66       * of Last Trade c1 = Change                   c  = Change - Percent Change o  = Open Trade
67       * h  = High Trade g  = Low Trade                v  = Volume a  = Ask Price
68       * b  = Bid Price j  = 52 week low              k  = 52 week high n  = Name of company
69       * p  = Previous close x  = Name of Stock Exchange
70       */
71      private static final String URL= "http://finance.yahoo.com/d/quotes.csv";
72  
73      /***
74       * Initialize the service commands.
75       */
76      static {
77          services= new ArrayList();
78          services.add("stock");
79      }
80  
81      //May want to change options, but for now I'll just set them for the user.
82      private String options= "snd1t1l9c";
83  
84      /***
85       * Constructor for StockModule.
86       *
87       * @param bot
88       */
89      public StockModule(AIMBot bot) {
90          super(bot);
91      }
92  
93      /***
94       * @see com.levelonelabs.aimbot.BotModule#getName()
95       */
96      public String getName() {
97          return "Stock Module";
98      }
99  
100     /***
101      * @see com.levelonelabs.aimbot.BotModule#getServices()
102      */
103     public ArrayList getServices() {
104         return services;
105     }
106 
107     /***
108      * @see com.levelonelabs.aimbot.BotModule#help()
109      */
110     public String help() {
111         StringBuffer sb= new StringBuffer();
112         sb.append("<B>stock <i>SYMBOLS</i></B> (displays stock price and other info for the given space separated symbols)\n");
113         sb.append("* If the preference \"portfolio\" is set, you can omit the symbols to use those listed in the preference.");
114         sb.append("<b>\n\nEXAMPLE: stock ge ibm</b>\n");
115         return sb.toString();
116     }
117 
118     /***
119      * @see com.levelonelabs.aimbot.BotModule#performService(AIMBuddy, String)
120      */
121     public void performService(AIMBuddy buddy, String query) {
122         if (query.trim().toLowerCase().startsWith("stock")) {
123             StringTokenizer st= new StringTokenizer(query, " ");
124             String portfolioPref= buddy.getPreference("portfolio");
125             if (st.countTokens() < 2 && portfolioPref == null) {
126                 super.sendMessage(buddy, "ERROR:\n" + help());
127             } else {
128                 String imcommand= st.nextToken();
129                 String symbol= "";
130                 while (st.hasMoreElements()) {
131                     String temp= (String) st.nextElement();
132                     symbol += temp;
133                     if (st.hasMoreElements()) {
134                         symbol += "+";
135                     }
136                 }
137 
138                 //no symbols listed; use the preference
139                 if (symbol.equals("")) {
140                     symbol=portfolioPref;
141                 }
142                 
143                 symbol=symbol.replaceAll(" ", "+");
144 
145                 String quote= "Couldn't get stock information";
146 
147                 //grab the quote
148                 try {
149                     String queryURL= URL + "?" + "f=" + this.options + "&s=" + symbol;
150                     URL url= new URL(queryURL);
151 
152                     //setup the connection (do POST)
153                     URLConnection connection= url.openConnection();
154                     connection.setDoOutput(true);
155                     connection.connect();
156 
157                     //read the results page
158                     BufferedReader br= new BufferedReader(new InputStreamReader(connection.getInputStream()));
159                     String line;
160                     StringBuffer sb= new StringBuffer();
161                     while ((line= br.readLine()) != null) {
162                         sb.append(line + "\n");
163                     }
164                     br.close();
165 
166                     //extract the results
167                     st= new StringTokenizer(sb.toString(), ",");
168                     String results= "";
169                     while (st.hasMoreElements()) {
170                         results += (st.nextElement() + "\t");
171                     }
172                     if (!results.trim().equals("")) {
173                         quote= results;
174                     }
175 
176                     //error checking?
177                 } catch (Exception e) {
178                     e.printStackTrace();
179                 }
180 
181                 super.sendMessage(buddy, "\n" + quote);
182             }
183         }
184     }
185 }