Diffstat (limited to 'noncore/todayplugins/stockticker/libstocks/history.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/todayplugins/stockticker/libstocks/history.c | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/noncore/todayplugins/stockticker/libstocks/history.c b/noncore/todayplugins/stockticker/libstocks/history.c new file mode 100644 index 0000000..61f57d1 --- a/dev/null +++ b/noncore/todayplugins/stockticker/libstocks/history.c | |||
@@ -0,0 +1,149 @@ | |||
1 | /* libstocks - Library to get current stock quotes from Yahoo Finance | ||
2 | * | ||
3 | * Copyright (C) 2000 Eric Laeuffer | ||
4 | * | ||
5 | * This library is free software; you can redistribute it and/or | ||
6 | * modify it under the terms of the GNU Library General Public | ||
7 | * License as published by the Free Software Foundation; either | ||
8 | * version 2 of the License, or (at your option) any later version. | ||
9 | * | ||
10 | * This library is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | * Library General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU Library General Public | ||
16 | * License along with this library; if not, write to the | ||
17 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | * Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | #define __HISTORY_C__ | ||
22 | |||
23 | #include <string.h> | ||
24 | #include <stdlib.h> | ||
25 | |||
26 | #ifdef __WINDOWS__ | ||
27 | #include <malloc.h> | ||
28 | #endif | ||
29 | |||
30 | #include "stocks.h" | ||
31 | #include "http.h" | ||
32 | #include "csv.h" | ||
33 | |||
34 | const char history_stocks_server[]="chart.yahoo.com"; | ||
35 | const char url_history_beg[]="/table.csv?s="; | ||
36 | |||
37 | /*****************************************************************************/ | ||
38 | /* Gets the stock history csv file and return in csv_file the result. */ | ||
39 | /* The stock is defined by its symbol. */ | ||
40 | /* The history file starts at date1 and stops at date2. */ | ||
41 | /*****************************************************************************/ | ||
42 | libstocks_return_code get_history_csv(char *symbol, | ||
43 | char *date1, | ||
44 | char *date2, | ||
45 | char **csv_file) | ||
46 | { | ||
47 | char *data; | ||
48 | char url[80]; | ||
49 | |||
50 | char adate[12]; | ||
51 | |||
52 | char *y1, *y2; | ||
53 | char *m1, *m2; | ||
54 | char *d1, *d2; | ||
55 | |||
56 | libstocks_return_code error; | ||
57 | |||
58 | char *test; | ||
59 | |||
60 | strcpy(url, url_history_beg); | ||
61 | strcat(url, symbol); | ||
62 | |||
63 | strcpy(adate, date1); | ||
64 | |||
65 | y1=strtok(adate, "/"); | ||
66 | if(!y1) return ERRDATE; | ||
67 | m1 = strtok(NULL, "/"); | ||
68 | if(!m1) return ERRDATE; | ||
69 | d1 = strtok(NULL, "/"); | ||
70 | if(!m1) return ERRDATE; | ||
71 | |||
72 | strcat(url, "&a="); | ||
73 | strcat(url, m1); | ||
74 | strcat(url, "&b="); | ||
75 | strcat(url, d1); | ||
76 | strcat(url, "&c="); | ||
77 | strcat(url, y1); | ||
78 | |||
79 | strcpy(adate, date2); | ||
80 | |||
81 | y2=strtok(adate, "/"); | ||
82 | if(!y2) return ERRDATE; | ||
83 | m2 = strtok(NULL, "/"); | ||
84 | if(!m2) return ERRDATE; | ||
85 | d2 = strtok(NULL, "/"); | ||
86 | if(!d2) return ERRDATE; | ||
87 | |||
88 | strcat(url, "&d="); | ||
89 | strcat(url, m2); | ||
90 | strcat(url, "&e="); | ||
91 | strcat(url, d2); | ||
92 | strcat(url, "&f="); | ||
93 | strcat(url, y2); | ||
94 | strcat(url, "&g=d&q=q&y=0&x=.csv"); | ||
95 | |||
96 | error=http_get(url, (char *)history_stocks_server, &data); | ||
97 | if (error) return error; | ||
98 | |||
99 | /* tests if data is valid */ | ||
100 | |||
101 | /* The server returns "No data available for symbol" */ | ||
102 | /* when the stock is not available */ | ||
103 | test = strstr(data, "No data available"); | ||
104 | if (test) | ||
105 | { | ||
106 | free(data); | ||
107 | return ERRDATA; | ||
108 | } | ||
109 | |||
110 | /* The server return "No Prices in this date range" */ | ||
111 | /* when dates have not the good range */ | ||
112 | test = strstr(data, "No Prices in this date range"); | ||
113 | if (test) | ||
114 | { | ||
115 | free(data); | ||
116 | return ERRRANG; | ||
117 | } | ||
118 | |||
119 | *csv_file = data; | ||
120 | |||
121 | return(0); | ||
122 | } | ||
123 | |||
124 | /*****************************************************************************/ | ||
125 | /* Gets the stock history and return a stock structure list. */ | ||
126 | /* The stock is defined by its symbol. */ | ||
127 | /* The history file starts at date1 and stops at date2. */ | ||
128 | /*****************************************************************************/ | ||
129 | libstocks_return_code get_stock_history(char *symbol, | ||
130 | char *date1, | ||
131 | char *date2, | ||
132 | stock **stock_history) | ||
133 | { | ||
134 | libstocks_return_code error; | ||
135 | char *csv_file; | ||
136 | stock *stock_parsed; | ||
137 | |||
138 | error = get_history_csv(symbol, date1, date2, &csv_file); | ||
139 | if (error) return error; | ||
140 | |||
141 | stock_parsed = parse_csv_history_file(csv_file); | ||
142 | |||
143 | free(csv_file); | ||
144 | |||
145 | if (!stock_parsed) return ERRPCSV; | ||
146 | *stock_history = stock_parsed; | ||
147 | |||
148 | return(0); | ||
149 | } | ||