~cszikszoy/do-plugins/pastebin

« back to all changes in this revision

Viewing changes to SimplePlugins/src/StockQuoteAction.cs

  • Committer: David Siegel
  • Date: 2008-01-24 04:48:52 UTC
  • Revision ID: dave@ja7gggggggg9-20080124044852-9vnlo01h9kkyz12a
Added Epiphany plugin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* StockQuoteAction.cs
2
 
 *
3
 
 * GNOME Do is the legal property of its developers. Please refer to the
4
 
 * COPYRIGHT file distributed with this source distribution.
5
 
 *
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.
10
 
 *
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.
15
 
 *
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/>.
18
 
 */
19
 
 
20
 
using System;
21
 
using System.IO;
22
 
using System.Net;
23
 
using System.Text.RegularExpressions;
24
 
 
25
 
using Do.Universe;
26
 
using Do.Addins;
27
 
 
28
 
namespace Do.Plugins.Universe
29
 
{
30
 
        /// <summary>
31
 
        /// Given an ITextItem, StockQuoteAction will query Google Finance
32
 
        /// for the quote, then scrape out the price, daily movement and percent move
33
 
        /// </summary>
34
 
        public class StockQuoteAction : AbstractAction
35
 
        {
36
 
 
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>";
42
 
 
43
 
                public StockQuoteAction ()
44
 
                {
45
 
                }
46
 
                
47
 
                public override string Name {
48
 
                        get { return "Stock Quote"; }
49
 
                }
50
 
                
51
 
                public override string Description
52
 
                {
53
 
                        get { return "Get Quotes From Google Finance."; }
54
 
                }
55
 
                
56
 
                public override string Icon
57
 
                {
58
 
                        get { return "stock_chart-autoformat"; }
59
 
                }
60
 
                
61
 
                public override Type[] SupportedItemTypes
62
 
                {
63
 
                        get {
64
 
                                return new Type[] {
65
 
                                        typeof (ITextItem),
66
 
                                };
67
 
                        }
68
 
                }
69
 
 
70
 
                public override bool SupportsItem (IItem item)
71
 
                {
72
 
                        string word;
73
 
 
74
 
                        word = null;
75
 
                        if (item is ITextItem) {
76
 
                                word = (item as ITextItem).Text;
77
 
                        }
78
 
                        return !string.IsNullOrEmpty (word);
79
 
                }
80
 
                
81
 
                public override IItem[] Perform (IItem[] items, IItem[] modifierItems)
82
 
                {
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);
88
 
                        try {
89
 
                                page = GetWebpageContents (url);
90
 
                                beginPrice = page.IndexOf (BeginPrice);
91
 
                                beginMove = page.IndexOf (BeginMove);
92
 
                                beginPercent = page.IndexOf (BeginPercent);
93
 
                                
94
 
                                // Grab price
95
 
                                if (beginPrice < 0 | beginMove < 0 | beginPercent < 0)
96
 
                                        throw new Exception ();
97
 
                                pagePrice = page.Substring (beginPrice);
98
 
                                endIndex = pagePrice.IndexOf (EndResult);
99
 
                                if (endIndex < 0)
100
 
                                        throw new Exception ();
101
 
                                priceString = pagePrice.Substring (BeginPrice.Length, endIndex-BeginPrice.Length);
102
 
                                
103
 
                                // Grab daily move
104
 
                                pageMove = page.Substring (beginMove);
105
 
                                endIndex = pageMove.IndexOf (EndResult);
106
 
                                if (endIndex < 0)
107
 
                                        throw new Exception ();
108
 
                                moveString = pageMove.Substring (BeginMove.Length, endIndex-BeginMove.Length);
109
 
                                
110
 
                                // Grab percent move
111
 
                                pagePer = page.Substring (beginPercent);
112
 
                                endIndex = pagePer.IndexOf (EndResult);
113
 
                                if (endIndex < 0)
114
 
                                        throw new Exception ();
115
 
                                percentString = pagePer.Substring (BeginPercent.Length, endIndex-BeginPercent.Length);
116
 
                                
117
 
                                reply = priceString + " " + moveString + " " + percentString;
118
 
                        } catch {
119
 
                                reply = "Google Finance could not process your request";
120
 
                        }
121
 
                        return new IItem[] { new TextItem (reply) };
122
 
                }
123
 
                
124
 
                string GoogleFinanceURL (string e)
125
 
                {
126
 
                        return "http://finance.google.com/finance?q=" + (e ?? "");
127
 
                }
128
 
                
129
 
                string GetWebpageContents (string url)
130
 
                {
131
 
                        HttpWebRequest request;
132
 
                        WebResponse response;
133
 
                        Stream stream;
134
 
                        StreamReader reader;
135
 
                        string content;
136
 
                         
137
 
                        request = HttpWebRequest.Create (url) as HttpWebRequest;
138
 
                        response = request.GetResponse ();
139
 
                        stream = response.GetResponseStream ();
140
 
                        reader = new StreamReader (stream);
141
 
                        content = reader.ReadToEnd ();
142
 
                
143
 
                        reader.Close ();
144
 
                        stream.Close ();
145
 
                        response.Close ();
146
 
                 
147
 
                        return content;
148
 
                }
149
 
 
150
 
        }
151
 
}