1 module quandl;
2 import std.stdio;
3 import std..string;
4 import std.net.curl;
5 
6 /**
7 	quandl.d
8 
9 	Based on quandl.h by Zhiwei Fu [Created on: 30/09/2013 (Updated on 10/09/2014)]
10 	Hacked on by Laeeth Isharc 2014 to port to D
11 	Not pretty but it works (I hope).  Use at your own risk.
12 
13 
14  	This programme is free software. It is developed by Dr Zhiwei Fu as a product
15 	contributing to quandl.com.
16 	This programme is distributed in the hope that it will be useful,
17 	but WITHOUT ANY WARRANTY, without even the implied warranty of
18 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 */
20 
21 // original imported iostream,fstream (perror),string,string.h,sys/socket,netinet/in.h, arpa/inet.g,netdb.h,netinet/tcp.h,time.h
22 
23 struct quandlapi_t {
24 // To store the token in "AuthCode", which is a public variable in the class.
25 	string AuthCode="";
26 	this(string authcode)
27 	{
28 		AuthCode=authcode;
29 	}
30 }
31 
32 quandlapi_t quandlapi;
33 
34 /**
35 	To download file from the website defined by the first argument.
36 	To determine the website address by the token stored in "code"
37 	and call the function "download"
38 	*/
39 
40 void get(string code, string type)
41 {
42 	type=toLower(type);
43 	string order = "asc";
44 	string website = "https://www.quandl.com/api/v1/datasets/" ~ code ~ "." ~ type ~ "?sort_order=" ~ order;
45 	if (quandlapi.AuthCode.length == 0){
46 		writefln("It would appear you are not using an authentication"
47 				 " token. Please visit http://www.quandl.com/help/c++"
48 				" or your usage may be limited.\n");
49 	}
50 	else {
51 		website ~= "&auth_token=" ~ quandlapi.AuthCode;
52 	}
53 
54 	string FileName;
55 	auto iLength = code.length;
56 	foreach(i;0 .. iLength)
57 	{
58 		if (code[i] == '/')
59 		{
60 			FileName = code[i+1.. iLength];
61 			break;
62 		}
63 	}
64 	debug{
65 		writefln("%s,%s", iLength,FileName);
66 		writefln(website);
67 	}
68 	download(website, FileName ~ "." ~ type);
69 	return;
70 }
71 // All parameters are prescribed by users.
72 // 1. Quandl code;
73 // 2. Ascending/descending order;
74 // 3. Start date;
75 // 4. End date;
76 // 5. Transformation;
77 // 6. collapse;
78 // 7. Rows;
79 // 8. Output type
80 // There are 7 optional arguments compared to the one above.
81 void get(string code, string order, string StartDate, string EndDate, string transformation, string collapse, string rows, string filetype)
82 {
83 	filetype=toLower(filetype);
84 	order = toLower(order);
85 	if (order.length==0)
86 		order="asc";
87 	if (filetype.length==0)
88 		filetype="json";
89 
90 	string website = "http://www.quandl.com/api/v1/datasets/" ~ code ~ "." ~ filetype ~ "?sort_order=" ~ order;
91 	if (quandlapi.AuthCode.length == 0) {
92 		writefln(	"It appear you are not using an authentication"
93 					" token. Please visit http://www.quandl.com/help/api for getting one"
94 					" ; otherwise your usage may be limited.");
95 	}
96 	else {
97 		website ~= "&auth_token=" ~ quandlapi.AuthCode;
98 	}
99 	if (StartDate.length>0)
100 		website ~= "&trim_start=" ~ StartDate;
101 	if (EndDate.length>0)
102 		website ~= "&trim_end=" ~ EndDate;
103 	if (transformation.length>0)
104 		website ~= "&transformation=" ~ transformation;
105 	if (collapse.length>0)
106 		website ~= "&collapse=" ~ collapse;
107 	if (rows.length>0)
108 		website ~= "&rows=" ~ rows;
109 	string FileName;
110 	auto iLength = code.length;
111 	foreach(i;0 .. iLength)
112 	{
113 		if(code[i] == '/')
114 		{
115 			FileName = code[i+1 .. iLength ];
116 			break;
117 		}
118 	}
119 	debug{writefln(website);}
120 	debug{writefln(FileName);}
121 	download(website, FileName ~ "." ~ filetype);
122 }
123 
124 unittest
125 {
126 	quandlapi.AuthCode="";
127 	get("BAVERAGE/ANX_HKUSD","csv");
128 	get("BAVERAGE/ANX_HKUSD","asc","1990-01-01","2015-01-01","","","","json");
129 }