-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYahoodownloader.cs
112 lines (93 loc) · 2.8 KB
/
Yahoodownloader.cs
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Net;
using System.Threading;
namespace Update
{
//new WebClient();
internal class Yahoodownloader
{
private static String initurlstring = "http://ichart.finance.yahoo.com/table.csv?s=";
private static WebClient client = new WebClient(); // web client to download data from yahoo
private string symbol;
private Thread yahoothread;
private DateTime start;
private DateTime end;
private string urlstring;
public Yahoodownloader(string sym, DateTime begin, DateTime final)
{
symbol = sym;
start = begin;
end = final;
if (!symbol.StartsWith("^"))
{
urlstring = initurlstring + symbol + "&a=";
yahoothread = new Thread(new ThreadStart(yahooDownload));
urlstring += start.Month.ToString();
urlstring += "&b=" + start.Day.ToString();
urlstring += "&c=" + start.Year.ToString();
urlstring += "&d=" + end.Month.ToString();
urlstring += "&e=" + end.Day.ToString() + "&f=";
urlstring += end.Year.ToString() + "&g=d&ignore=.csv";
}
// unless the symbol was an index indicated by starting the symbol with a ^
else
{
urlstring = "http://finance.yahoo.com/q/hp?s=" + symbol + "+Historical+Prices";
}
yahoothread.Start();
}
private void yahooDownload()
{
DateTime yCurrentDate;
float yOpen, yHigh, yLow, yClose, yAdjClose;
Int64 yVolume;
// string selcmd;
if (start.CompareTo(end) <= 0)
{
try
{
// get the data for a stock symbol from yahoo
//string csvfile = client.DownloadString(urlstring);
Uri urifrstr = new Uri(urlstring);
string csvfile = (string)client.DownloadStringAsync(urifrstr);
//if (toReturn) return;
}
catch (WebException webEx)
{
if (webEx.Status == WebExceptionStatus.ProtocolError)
{
outfile.WriteLine(symbol);
outfile.Flush();
Console.WriteLine("File not available :" + symbol);
}
else
{
Console.WriteLine("{0}", webEx.StackTrace);
Console.WriteLine("Status is: {0}", webEx.Status);
if (webEx.Status == WebExceptionStatus.ConnectFailure)
{
Console.WriteLine("Are you behind a firewall? If so, go through the proxy server.");
if (toReturn) return;
}
}
}
catch (ArgumentNullException argEx)
{
Console.WriteLine("Did you forget to initialize the url:{0}\nException is: {1}", urlstring, argEx.ToString());
if (toReturn) return;
}
catch (NotSupportedException nsEx)
{
Console.WriteLine("Not Supported Exception : {0}", nsEx.ToString());
if (toReturn) return;
}
catch (Exception exc)
{
Console.WriteLine("Exception type: {0}, Stack Trace = {1}", exc.GetType(), exc.StackTrace);
Console.WriteLine("{0} : {1}", line, symbol);
if (toReturn) return;
}
}
}
}
}