1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
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
139 if (symbol.equals("")) {
140 symbol=portfolioPref;
141 }
142
143 symbol=symbol.replaceAll(" ", "+");
144
145 String quote= "Couldn't get stock information";
146
147
148 try {
149 String queryURL= URL + "?" + "f=" + this.options + "&s=" + symbol;
150 URL url= new URL(queryURL);
151
152
153 URLConnection connection= url.openConnection();
154 connection.setDoOutput(true);
155 connection.connect();
156
157
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
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
177 } catch (Exception e) {
178 e.printStackTrace();
179 }
180
181 super.sendMessage(buddy, "\n" + quote);
182 }
183 }
184 }
185 }