3
* GNOME Do is the legal property of its developers. Please refer to the
4
* COPYRIGHT file distributed with this source distribution.
6
* This program is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23
using System.Text.RegularExpressions;
28
namespace Do.Plugins.Universe
31
/// Given an ITextItem, StockQuoteAction will query Google Finance
32
/// for the quote, then scrape out the price, daily movement and percent move
34
public class StockQuoteAction : AbstractAction
37
// String indicating the tags surronding the pertinent info
38
const string BeginPrice = "_l\">";
39
const string BeginMove = "_c\">";
40
const string BeginPercent = "_cp\">";
41
const string EndResult = "</span>";
43
public StockQuoteAction ()
47
public override string Name {
48
get { return "Stock Quote"; }
51
public override string Description
53
get { return "Get Quotes From Google Finance."; }
56
public override string Icon
58
get { return "stock_chart-autoformat"; }
61
public override Type[] SupportedItemTypes
70
public override bool SupportsItem (IItem item)
75
if (item is ITextItem) {
76
word = (item as ITextItem).Text;
78
return !string.IsNullOrEmpty (word);
81
public override IItem[] Perform (IItem[] items, IItem[] modifierItems)
83
string expression, url, reply, priceString, moveString, percentString, page;
84
string pagePrice, pageMove, pagePer;
85
int beginPrice, beginMove, beginPercent, endIndex;
86
expression = (items[0] as ITextItem).Text;
87
url = GoogleFinanceURL (expression);
89
page = GetWebpageContents (url);
90
beginPrice = page.IndexOf (BeginPrice);
91
beginMove = page.IndexOf (BeginMove);
92
beginPercent = page.IndexOf (BeginPercent);
95
if (beginPrice < 0 | beginMove < 0 | beginPercent < 0)
96
throw new Exception ();
97
pagePrice = page.Substring (beginPrice);
98
endIndex = pagePrice.IndexOf (EndResult);
100
throw new Exception ();
101
priceString = pagePrice.Substring (BeginPrice.Length, endIndex-BeginPrice.Length);
104
pageMove = page.Substring (beginMove);
105
endIndex = pageMove.IndexOf (EndResult);
107
throw new Exception ();
108
moveString = pageMove.Substring (BeginMove.Length, endIndex-BeginMove.Length);
111
pagePer = page.Substring (beginPercent);
112
endIndex = pagePer.IndexOf (EndResult);
114
throw new Exception ();
115
percentString = pagePer.Substring (BeginPercent.Length, endIndex-BeginPercent.Length);
117
reply = priceString + " " + moveString + " " + percentString;
119
reply = "Google Finance could not process your request";
121
return new IItem[] { new TextItem (reply) };
124
string GoogleFinanceURL (string e)
126
return "http://finance.google.com/finance?q=" + (e ?? "");
129
string GetWebpageContents (string url)
131
HttpWebRequest request;
132
WebResponse response;
137
request = HttpWebRequest.Create (url) as HttpWebRequest;
138
response = request.GetResponse ();
139
stream = response.GetResponseStream ();
140
reader = new StreamReader (stream);
141
content = reader.ReadToEnd ();