summaryrefslogtreecommitdiff
path: root/noncore/todayplugins/stockticker/libstocks/history.c
blob: 61f57d1b2d76a3e79a34f9ca72cdd5915d82b37b (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* libstocks - Library to get current stock quotes from Yahoo Finance
 *
 * Copyright (C) 2000 Eric Laeuffer
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#define __HISTORY_C__

#include <string.h>
#include <stdlib.h>

#ifdef __WINDOWS__
#include <malloc.h>
#endif

#include "stocks.h"
#include "http.h"
#include "csv.h"

const char history_stocks_server[]="chart.yahoo.com";
const char url_history_beg[]="/table.csv?s=";

/*****************************************************************************/
/* Gets the stock history csv file and return in csv_file the result.        */
/* The stock is defined by its symbol.                                       */
/* The history file starts at date1 and stops at date2.                      */
/*****************************************************************************/
libstocks_return_code get_history_csv(char *symbol, 
				      char *date1, 
				      char *date2, 
				      char **csv_file)
{
  char *data;
  char url[80];

  char adate[12];

  char *y1, *y2;
  char *m1, *m2;
  char *d1, *d2;

  libstocks_return_code error;
  
  char *test;

  strcpy(url, url_history_beg);
  strcat(url, symbol);

  strcpy(adate, date1);

  y1=strtok(adate, "/");
  if(!y1) return ERRDATE;
  m1 = strtok(NULL, "/");
  if(!m1) return ERRDATE;
  d1 = strtok(NULL, "/");
  if(!m1) return ERRDATE;

  strcat(url, "&a=");
  strcat(url, m1);
  strcat(url, "&b=");
  strcat(url, d1);
  strcat(url, "&c=");
  strcat(url, y1);

  strcpy(adate, date2);

  y2=strtok(adate, "/");
  if(!y2) return ERRDATE;
  m2 = strtok(NULL, "/");
  if(!m2) return ERRDATE;
  d2 = strtok(NULL, "/");
  if(!d2) return ERRDATE;

  strcat(url, "&d=");
  strcat(url, m2);
  strcat(url, "&e=");
  strcat(url, d2);
  strcat(url, "&f=");
  strcat(url, y2);
  strcat(url, "&g=d&q=q&y=0&x=.csv");
  
  error=http_get(url, (char *)history_stocks_server, &data);
  if (error) return error;

  /* tests if data is valid */

  /* The server returns "No data available for symbol" */
  /* when the stock is not available */
  test = strstr(data, "No data available");
  if (test) 
    {
      free(data);
      return ERRDATA;
    }

  /* The server return "No Prices in this date range" */
  /* when dates have not the good range */
  test = strstr(data, "No Prices in this date range");
  if (test) 
    {
      free(data);
      return ERRRANG;
    }

  *csv_file = data;

  return(0);
}

/*****************************************************************************/
/* Gets the stock history and return a stock structure list.                 */
/* The stock is defined by its symbol.                                       */
/* The history file starts at date1 and stops at date2.                      */
/*****************************************************************************/
libstocks_return_code get_stock_history(char *symbol, 
				        char *date1, 
				        char *date2, 
				        stock **stock_history)
{
  libstocks_return_code error;
  char *csv_file;
  stock *stock_parsed;

  error = get_history_csv(symbol, date1, date2, &csv_file);
  if (error) return error;

  stock_parsed = parse_csv_history_file(csv_file);

  free(csv_file);

  if (!stock_parsed) return ERRPCSV;
  *stock_history = stock_parsed;

  return(0);
}