Branch data Line data Source code
1 : : /********************************************************************\
2 : : * gnc-quotes.hpp -- proxy for Finance::Quote *
3 : : * Copyright (C) 2021 Geert Janssens <geert@kobaltwit.be> *
4 : : * *
5 : : * This program is free software; you can redistribute it and/or *
6 : : * modify it under the terms of the GNU General Public License as *
7 : : * published by the Free Software Foundation; either version 2 of *
8 : : * the License, or (at your option) any later version. *
9 : : * *
10 : : * This program 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 *
13 : : * GNU General Public License for more details. *
14 : : * *
15 : : * You should have received a copy of the GNU General Public License*
16 : : * along with this program; if not, contact: *
17 : : * *
18 : : * Free Software Foundation Voice: +1-617-542-5942 *
19 : : * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20 : : * Boston, MA 02110-1301, USA gnu@gnu.org *
21 : : \ *******************************************************************/
22 : :
23 : : #include <boost/process/environment.hpp>
24 : : #include <config.h>
25 : : #include <qoflog.h>
26 : :
27 : : #include <algorithm>
28 : : #include <stdexcept>
29 : : #include <vector>
30 : : #include <string>
31 : : #include <iostream>
32 : : #include <boost/version.hpp>
33 : : #if BOOST_VERSION < 107600
34 : : // json_parser uses a deprecated version of bind.hpp
35 : : #define BOOST_BIND_GLOBAL_PLACEHOLDERS
36 : : #endif
37 : : #include <boost/algorithm/string.hpp>
38 : : #include <boost/filesystem.hpp>
39 : : #ifdef BOOST_WINDOWS_API
40 : : #include <boost/process/windows.hpp>
41 : : #endif
42 : : #include <boost/process.hpp>
43 : : #include <boost/regex.hpp>
44 : : #include <boost/property_tree/ptree.hpp>
45 : : #include <boost/property_tree/json_parser.hpp>
46 : : #include <boost/iostreams/device/array.hpp>
47 : : #include <boost/iostreams/stream_buffer.hpp>
48 : : #include <boost/locale.hpp>
49 : : #include <boost/asio.hpp>
50 : : #include <glib.h>
51 : : #include "gnc-commodity.hpp"
52 : : #include <gnc-datetime.hpp>
53 : : #include <gnc-numeric.hpp>
54 : : #include "gnc-quotes.hpp"
55 : :
56 : : #include <gnc-commodity.h>
57 : : #include <gnc-path.h>
58 : : #include "gnc-ui-util.h"
59 : : #include <gnc-prefs.h>
60 : : #include <gnc-session.h>
61 : : #include <regex.h>
62 : : #include <qofbook.h>
63 : :
64 : : static const QofLogModule log_module = "gnc.price-quotes";
65 : : static const char* av_api_env = "ALPHAVANTAGE_API_KEY";
66 : : static const char* av_api_key = "alphavantage-api-key";
67 : : static const char* yh_api_env = "FINANCEAPI_API_KEY";
68 : : static const char* yh_api_key = "yhfinance-api-key";
69 : :
70 : : namespace bl = boost::locale;
71 : : namespace bp = boost::process;
72 : : namespace bfs = boost::filesystem;
73 : : namespace bpt = boost::property_tree;
74 : : namespace bio = boost::iostreams;
75 : :
76 : : using QuoteResult = std::tuple<int, StrVec, StrVec>;
77 : :
78 : : struct GncQuoteSourceError : public std::runtime_error
79 : : {
80 : 0 : GncQuoteSourceError(const std::string& err) : std::runtime_error(err) {}
81 : : };
82 : :
83 : : CommVec
84 : : gnc_quotes_get_quotable_commodities(const gnc_commodity_table * table);
85 : :
86 : : class GncQuoteSource
87 : : {
88 : : public:
89 : 0 : virtual ~GncQuoteSource() = default;
90 : : virtual const StrVec& get_sources() const noexcept = 0;
91 : : virtual const std::string & get_version() const noexcept = 0;
92 : : virtual QuoteResult get_quotes(const std::string& json_str) const = 0;
93 : : };
94 : :
95 : :
96 : : class GncQuotesImpl
97 : : {
98 : : public:
99 : : // Constructor - checks for presence of Finance::Quote and import version and quote sources
100 : : GncQuotesImpl ();
101 : : explicit GncQuotesImpl (QofBook *book);
102 : : GncQuotesImpl(QofBook*, std::unique_ptr<GncQuoteSource>);
103 : :
104 : : void fetch (QofBook *book);
105 : : void fetch (CommVec& commodities);
106 : : void fetch (gnc_commodity *comm);
107 : : void report (const char* source, const StrVec& commodities, bool verbose);
108 : :
109 : 0 : const std::string& version() noexcept { return m_quotesource->get_version(); }
110 : 0 : const QuoteSources& sources() noexcept { return m_sources; }
111 : 0 : bool had_failures() noexcept { return !m_failures.empty(); }
112 : : const QFVec& failures() noexcept;
113 : : std::string report_failures() noexcept;
114 : :
115 : : private:
116 : : std::string query_fq (const char* source, const StrVec& commoditites);
117 : : std::string query_fq (const CommVec&);
118 : : bpt::ptree parse_quotes (const std::string& quote_str);
119 : : void create_quotes(const bpt::ptree& pt, const CommVec& comm_vec);
120 : : std::string comm_vec_to_json_string(const CommVec&) const;
121 : : GNCPrice* parse_one_quote(const bpt::ptree&, gnc_commodity*);
122 : :
123 : : std::unique_ptr<GncQuoteSource> m_quotesource;
124 : : QuoteSources m_sources;
125 : : QFVec m_failures;
126 : : QofBook *m_book;
127 : : gnc_commodity *m_dflt_curr;
128 : : };
129 : :
130 : : class GncFQQuoteSource final : public GncQuoteSource
131 : : {
132 : : const bfs::path c_cmd;
133 : : std::string c_fq_wrapper;
134 : : std::string m_version;
135 : : StrVec m_sources;
136 : : bp::environment m_env;
137 : : public:
138 : : GncFQQuoteSource();
139 : 0 : ~GncFQQuoteSource() = default;
140 : 0 : const std::string& get_version() const noexcept override { return m_version; }
141 : 0 : const StrVec& get_sources() const noexcept override { return m_sources; }
142 : : QuoteResult get_quotes(const std::string&) const override;
143 : : private:
144 : : QuoteResult run_cmd (const StrVec& args, const std::string& json_string) const;
145 : : void set_api_key(const char* api_pref, const char* api_env);
146 : : };
147 : :
148 : : static void show_quotes(const bpt::ptree& pt, const StrVec& commodities, bool verbose);
149 : : static void show_currency_quotes(const bpt::ptree& pt, const StrVec& commodities, bool verbose);
150 : : static std::string parse_quotesource_error(const std::string& line);
151 : :
152 : : static const std::string empty_string{};
153 : :
154 : 0 : GncFQQuoteSource::GncFQQuoteSource() :
155 : 0 : c_cmd{bp::search_path("perl")},
156 : 0 : m_version{}, m_sources{}, m_env{boost::this_process::environment()}
157 : : {
158 : 0 : char *bindir = gnc_path_get_bindir();
159 : 0 : c_fq_wrapper = std::string(bindir) + "/finance-quote-wrapper";
160 : 0 : g_free(bindir);
161 : 0 : StrVec args{"-w", c_fq_wrapper, "-v"};
162 : 0 : auto [rv, sources, errors] = run_cmd(args, empty_string);
163 : 0 : if (rv)
164 : : {
165 : 0 : std::string err{bl::translate("Failed to initialize Finance::Quote: ")};
166 : 0 : for (const auto& err_line : errors)
167 : 0 : err += err_line.empty() ? "" : err_line + "\n";
168 : 0 : throw(GncQuoteSourceError(err));
169 : 0 : }
170 : 0 : if (!errors.empty())
171 : : {
172 : 0 : std::string err{bl::translate("Finance::Quote check returned error ")};
173 : 0 : for(const auto& err_line : errors)
174 : 0 : err += err.empty() ? "" : err_line + "\n";
175 : 0 : throw(GncQuoteSourceError(err));
176 : 0 : }
177 : 0 : auto version{sources.front()};
178 : 0 : if (version.empty())
179 : : {
180 : 0 : std::string err{bl::translate("No Finance::Quote Version")};
181 : 0 : throw(GncQuoteSourceError(err));
182 : 0 : }
183 : 0 : m_version = std::move(version);
184 : 0 : sources.erase(sources.begin());
185 : 0 : m_sources = std::move(sources);
186 : 0 : std::sort (m_sources.begin(), m_sources.end());
187 : :
188 : 0 : set_api_key(av_api_key, av_api_env);
189 : 0 : set_api_key(yh_api_key, yh_api_env);
190 : 0 : }
191 : :
192 : : QuoteResult
193 : 0 : GncFQQuoteSource::get_quotes(const std::string& json_str) const
194 : : {
195 : 0 : StrVec args{"-w", c_fq_wrapper, "-f" };
196 : 0 : return run_cmd(args, json_str);
197 : 0 : }
198 : :
199 : : QuoteResult
200 : 0 : GncFQQuoteSource::run_cmd (const StrVec& args, const std::string& json_string) const
201 : : {
202 : 0 : StrVec out_vec, err_vec;
203 : : int cmd_result;
204 : :
205 : : try
206 : : {
207 : 0 : std::future<std::vector<char> > out_buf, err_buf;
208 : 0 : boost::asio::io_context svc;
209 : :
210 : 0 : auto input_buf = bp::buffer (json_string);
211 : 0 : bp::child process;
212 : 0 : process = bp::child(c_cmd, args,
213 : 0 : bp::std_out > out_buf,
214 : 0 : bp::std_err > err_buf,
215 : 0 : bp::std_in < input_buf,
216 : : #ifdef BOOST_WINDOWS_API
217 : : bp::windows::create_no_window,
218 : : #endif
219 : 0 : m_env,
220 : 0 : svc);
221 : :
222 : 0 : svc.run();
223 : 0 : process.wait();
224 : :
225 : : {
226 : 0 : auto raw = out_buf.get();
227 : 0 : std::vector<std::string> data;
228 : 0 : std::string line;
229 : 0 : bio::stream_buffer<bio::array_source> sb(raw.data(), raw.size());
230 : 0 : std::istream is(&sb);
231 : :
232 : 0 : while (std::getline(is, line) && !line.empty())
233 : : {
234 : : #ifdef __WIN32
235 : : if (line.back() == '\r')
236 : : line.pop_back();
237 : : #endif
238 : 0 : out_vec.push_back (std::move(line));
239 : : }
240 : 0 : raw = err_buf.get();
241 : 0 : bio::stream_buffer<bio::array_source> eb(raw.data(), raw.size());
242 : 0 : std::istream es(&eb);
243 : :
244 : 0 : while (std::getline(es, line) && !line.empty())
245 : 0 : err_vec.push_back (std::move(line));
246 : 0 : }
247 : 0 : cmd_result = process.exit_code();
248 : 0 : }
249 : 0 : catch (std::exception &e)
250 : : {
251 : 0 : cmd_result = -1;
252 : 0 : err_vec.push_back(e.what());
253 : 0 : };
254 : :
255 : 0 : return QuoteResult (cmd_result, std::move(out_vec), std::move(err_vec));
256 : 0 : }
257 : :
258 : : void
259 : 0 : GncFQQuoteSource::set_api_key(const char* api_key, const char* api_env)
260 : : {
261 : 0 : auto key = gnc_prefs_get_string("general.finance-quote", api_key);
262 : 0 : if (key && *key)
263 : : {
264 : 0 : m_env[api_env] = key;
265 : 0 : g_free(key);
266 : : }
267 : : else
268 : : {
269 : 0 : if (api_key == av_api_key && m_env.find(api_env) == m_env.end())
270 : 0 : PWARN("No Alpha Vantage API key set, currency quotes and other "
271 : : "AlphaVantage based quotes won't work.");
272 : 0 : g_free(key);
273 : : }
274 : 0 : }
275 : :
276 : : /* GncQuotes implementation */
277 : 0 : GncQuotesImpl::GncQuotesImpl() : m_quotesource{new GncFQQuoteSource},
278 : 0 : m_sources{}, m_failures{},
279 : 0 : m_book{qof_session_get_book(gnc_get_current_session())},
280 : 0 : m_dflt_curr{gnc_default_currency()}
281 : : {
282 : 0 : m_sources = m_quotesource->get_sources();
283 : 0 : }
284 : :
285 : 0 : GncQuotesImpl::GncQuotesImpl(QofBook* book) : m_quotesource{new GncFQQuoteSource},
286 : 0 : m_sources{}, m_book{book},
287 : 0 : m_dflt_curr{gnc_default_currency()}
288 : : {
289 : 0 : m_sources = m_quotesource->get_sources();
290 : 0 : }
291 : :
292 : 0 : GncQuotesImpl::GncQuotesImpl(QofBook* book, std::unique_ptr<GncQuoteSource> quote_source) :
293 : 0 : m_quotesource{std::move(quote_source)},
294 : 0 : m_sources{}, m_book{book}, m_dflt_curr{gnc_default_currency()}
295 : : {
296 : 0 : m_sources = m_quotesource->get_sources();
297 : 0 : }
298 : :
299 : : void
300 : 0 : GncQuotesImpl::fetch (QofBook *book)
301 : : {
302 : 0 : if (!book)
303 : 0 : throw (GncQuoteException(bl::translate("GncQuotes::Fetch called with no book.")));
304 : : auto commodities = gnc_quotes_get_quotable_commodities (
305 : 0 : gnc_commodity_table_get_table (book));
306 : 0 : fetch (commodities);
307 : 0 : }
308 : :
309 : : void
310 : 0 : GncQuotesImpl::fetch (gnc_commodity *comm)
311 : : {
312 : 0 : auto commodities = CommVec {comm};
313 : 0 : fetch (commodities);
314 : 0 : }
315 : :
316 : : void
317 : 0 : GncQuotesImpl::fetch (CommVec& commodities)
318 : : {
319 : 0 : m_failures.clear();
320 : 0 : if (commodities.empty())
321 : 0 : throw (GncQuoteException(bl::translate("GncQuotes::Fetch called with no commodities.")));
322 : 0 : auto quote_str{query_fq (commodities)};
323 : 0 : auto ptree{parse_quotes (quote_str)};
324 : 0 : create_quotes(ptree, commodities);
325 : 0 : }
326 : :
327 : : void
328 : 0 : GncQuotesImpl::report (const char* source, const StrVec& commodities,
329 : : bool verbose)
330 : : {
331 : 0 : if (!source)
332 : 0 : throw (GncQuoteException(bl::translate("GncQuotes::Report called with no source.")));
333 : :
334 : 0 : bool is_currency{strcmp(source, "currency") == 0};
335 : 0 : m_failures.clear();
336 : 0 : if (commodities.empty())
337 : : {
338 : 0 : std::cerr << _("There were no commodities for which to retrieve quotes.") << std::endl;
339 : 0 : return;
340 : : }
341 : : try
342 : : {
343 : 0 : auto quote_str{query_fq (source, commodities)};
344 : 0 : auto ptree{parse_quotes (quote_str)};
345 : 0 : auto source_pt_ai{ptree.find(source)};
346 : 0 : if (is_currency)
347 : 0 : show_currency_quotes(source_pt_ai->second, commodities, verbose);
348 : : else
349 : 0 : show_quotes(source_pt_ai->second, commodities, verbose);
350 : 0 : }
351 : 0 : catch (const GncQuoteException& err)
352 : : {
353 : 0 : std::cerr << _("Finance::Quote retrieval failed with error ") << err.what() << std::endl;
354 : 0 : }
355 : : }
356 : :
357 : : const QFVec&
358 : 0 : GncQuotesImpl::failures() noexcept
359 : : {
360 : 0 : return m_failures;
361 : : }
362 : :
363 : : static std::string
364 : 0 : explain(GncQuoteError err, const std::string& errmsg)
365 : : {
366 : 0 : std::string retval;
367 : 0 : switch (err)
368 : : {
369 : 0 : case GncQuoteError::NO_RESULT:
370 : 0 : if (errmsg.empty())
371 : 0 : retval += _("Finance::Quote returned no data and set no error.");
372 : : else
373 : 0 : retval += _("Finance::Quote returned an error: ") + errmsg;
374 : 0 : break;
375 : 0 : case GncQuoteError::QUOTE_FAILED:
376 : 0 : if (errmsg.empty())
377 : 0 : retval += _("Finance::Quote reported failure set no error.");
378 : : else
379 : 0 : retval += _("Finance::Quote reported failure with error: ") + errmsg;
380 : 0 : break;
381 : 0 : case GncQuoteError::NO_CURRENCY:
382 : 0 : retval += _("Finance::Quote returned a quote with no currency.");
383 : 0 : break;
384 : 0 : case GncQuoteError::UNKNOWN_CURRENCY:
385 : 0 : retval += _("Finance::Quote returned a quote with a currency GnuCash doesn't know about.");
386 : 0 : break;
387 : 0 : case GncQuoteError::NO_PRICE:
388 : 0 : retval += _("Finance::Quote returned a quote with no price element.");
389 : 0 : break;
390 : 0 : case GncQuoteError::PRICE_PARSE_FAILURE:
391 : 0 : retval += _("Finance::Quote returned a quote with a price that GnuCash was unable to covert to a number.");
392 : 0 : break;
393 : 0 : case GncQuoteError::SUCCESS:
394 : : default:
395 : 0 : retval += _("The quote has no error set.");
396 : 0 : break;
397 : : }
398 : 0 : return retval;
399 : 0 : }
400 : :
401 : : std::string
402 : 0 : GncQuotesImpl::report_failures() noexcept
403 : : {
404 : 0 : std::string retval{_("Quotes for the following commodities were unavailable or unusable:\n")};
405 : 0 : std::for_each(m_failures.begin(), m_failures.end(),
406 : 0 : [&retval](auto failure)
407 : : {
408 : 0 : auto [ns, sym, reason, err] = failure;
409 : 0 : retval += "* " + ns + ":" + sym + " " +
410 : : explain(reason, err) + "\n";
411 : 0 : });
412 : 0 : return retval;
413 : : }
414 : :
415 : : /* **** Private function implementations ****/
416 : :
417 : : using Path = bpt::ptree::path_type;
418 : 0 : static inline Path make_quote_path(const std::string &name_space,
419 : : const std::string &symbol)
420 : : {
421 : : using Path = bpt::ptree::path_type;
422 : 0 : Path key{name_space, '|'};
423 : 0 : key /= Path{symbol, '|'};
424 : 0 : return key;
425 : 0 : };
426 : :
427 : : std::string
428 : 0 : GncQuotesImpl::comm_vec_to_json_string(const CommVec &comm_vec) const
429 : : {
430 : 0 : bpt::ptree pt, pt_child;
431 : 0 : pt.put("defaultcurrency", gnc_commodity_get_mnemonic(m_dflt_curr));
432 : :
433 : 0 : std::for_each (comm_vec.cbegin(), comm_vec.cend(),
434 : 0 : [this, &pt] (auto comm)
435 : : {
436 : 0 : auto comm_mnemonic = gnc_commodity_get_mnemonic (comm);
437 : 0 : auto comm_ns = std::string("currency");
438 : 0 : if (gnc_commodity_is_currency (comm))
439 : : {
440 : 0 : if (gnc_commodity_equiv(comm, m_dflt_curr) ||
441 : 0 : (!comm_mnemonic || (strcmp(comm_mnemonic, "XXX") == 0)))
442 : 0 : return;
443 : : }
444 : : else
445 : 0 : comm_ns = gnc_quote_source_get_internal_name(gnc_commodity_get_quote_source(comm));
446 : :
447 : 0 : pt.put (make_quote_path(comm_ns, comm_mnemonic), "");
448 : 0 : }
449 : : );
450 : :
451 : 0 : std::ostringstream result;
452 : 0 : bpt::write_json(result, pt);
453 : 0 : return result.str();
454 : 0 : }
455 : :
456 : : static inline std::string
457 : 0 : get_quotes(const std::string& json_str, const std::unique_ptr<GncQuoteSource>& qs)
458 : : {
459 : 0 : auto [rv, quotes, errors] = qs->get_quotes(json_str);
460 : 0 : std::string answer;
461 : :
462 : 0 : if (rv == 0)
463 : : {
464 : 0 : for (const auto& line : quotes)
465 : 0 : answer.append(line + "\n");
466 : : }
467 : : else
468 : : {
469 : 0 : std::string err_str;
470 : 0 : for (const auto& line: errors)
471 : : {
472 : 0 : if (line == "invalid_json\n")
473 : 0 : PERR("Finanace Quote Wrapper was unable to parse %s",
474 : : json_str.c_str());
475 : 0 : err_str += parse_quotesource_error(line);
476 : : }
477 : 0 : throw(GncQuoteException(err_str));
478 : 0 : }
479 : :
480 : 0 : return answer;
481 : 0 : }
482 : :
483 : : std::string
484 : 0 : GncQuotesImpl::query_fq (const char* source, const StrVec& commodities)
485 : : {
486 : 0 : bpt::ptree pt;
487 : 0 : auto is_currency{strcmp(source, "currency") == 0};
488 : :
489 : 0 : if (is_currency && commodities.size() < 2)
490 : 0 : throw(GncQuoteException(_("Currency quotes requires at least two currencies")));
491 : :
492 : 0 : if (is_currency)
493 : 0 : pt.put("defaultcurrency", commodities[0].c_str());
494 : : else
495 : 0 : pt.put("defaultcurrency", gnc_commodity_get_mnemonic(m_dflt_curr));
496 : :
497 : 0 : std::for_each(is_currency ? ++commodities.cbegin() : commodities.cbegin(),
498 : : commodities.cend(),
499 : 0 : [source, &pt](auto sym)
500 : : {
501 : 0 : pt.put(make_quote_path(source, sym), "");
502 : 0 : });
503 : 0 : std::ostringstream result;
504 : 0 : bpt::write_json(result, pt);
505 : 0 : auto result_str{result.str()};
506 : 0 : PINFO("Query JSON: %s\n", result_str.c_str());
507 : 0 : return get_quotes(result.str(), m_quotesource);
508 : 0 : }
509 : :
510 : : std::string
511 : 0 : GncQuotesImpl::query_fq (const CommVec& comm_vec)
512 : : {
513 : 0 : auto json_str{comm_vec_to_json_string(comm_vec)};
514 : 0 : PINFO("Query JSON: %s\n", json_str.c_str());
515 : 0 : return get_quotes(json_str, m_quotesource);
516 : 0 : }
517 : :
518 : : struct PriceParams
519 : : {
520 : : const char* ns;
521 : : const char* mnemonic;
522 : : bool success;
523 : : std::string type;
524 : : boost::optional<std::string> price;
525 : : bool inverted;
526 : : boost::optional<std::string> date;
527 : : boost::optional<std::string> time;
528 : : boost::optional<std::string> currency;
529 : : boost::optional<std::string> errormsg;
530 : : };
531 : :
532 : : static void
533 : 0 : get_price_and_type(PriceParams& p, const bpt::ptree& comm_pt)
534 : : {
535 : 0 : p.type = "last";
536 : 0 : p.price = comm_pt.get_optional<std::string> (p.type);
537 : :
538 : 0 : if (!p.price)
539 : : {
540 : 0 : p.type = "nav";
541 : 0 : p.price = comm_pt.get_optional<std::string> (p.type);
542 : : }
543 : :
544 : 0 : if (!p.price)
545 : : {
546 : 0 : p.type = "price";
547 : 0 : p.price = comm_pt.get_optional<std::string> (p.type);
548 : : /* guile wrapper used "unknown" as price type when "price" was found,
549 : : * reproducing here to keep same result for users in the pricedb */
550 : 0 : p.type = p.price ? "unknown" : "missing";
551 : : }
552 : 0 : }
553 : :
554 : : static void
555 : 0 : parse_quote_json(PriceParams& p, const bpt::ptree& comm_pt)
556 : : {
557 : 0 : auto success = comm_pt.get_optional<bool> ("success");
558 : 0 : p.success = success && *success;
559 : 0 : if (!p.success)
560 : 0 : p.errormsg = comm_pt.get_optional<std::string> ("errormsg");
561 : 0 : get_price_and_type(p, comm_pt);
562 : 0 : auto inverted = comm_pt.get_optional<bool> ("inverted");
563 : 0 : p.inverted = inverted && *inverted;
564 : 0 : p.date = comm_pt.get_optional<std::string> ("date");
565 : 0 : p.time = comm_pt.get_optional<std::string> ("time");
566 : 0 : p.currency = comm_pt.get_optional<std::string> ("currency");
567 : :
568 : :
569 : 0 : PINFO("Commodity: %s", p.mnemonic);
570 : 0 : PINFO(" Success: %s", (p.success ? "yes" : "no"));
571 : 0 : PINFO(" Date: %s", (p.date ? p.date->c_str() : "missing"));
572 : 0 : PINFO(" Time: %s", (p.time ? p.time->c_str() : "missing"));
573 : 0 : PINFO(" Currency: %s", (p.currency ? p.currency->c_str() : "missing"));
574 : 0 : PINFO(" Price: %s", (p.price ? p.price->c_str() : "missing"));
575 : 0 : PINFO(" Inverted: %s\n", (p.inverted ? "yes" : "no"));
576 : 0 : }
577 : :
578 : : static time64
579 : 0 : calc_price_time(const PriceParams& p)
580 : : {
581 : : /* Note that as of F::Q v. 1.52 the only sources that provide
582 : : * quote times are ftfunds (aka ukfunds), morningstarch, and
583 : : * mstaruk_fund, but it's faked with a comment "Set a dummy time
584 : : * as gnucash insists on having a valid format". It's also wrong,
585 : : * as it lacks seconds. Best ignored.
586 : : */
587 : 0 : if (p.date && !p.date->empty())
588 : : {
589 : : try
590 : : {
591 : 0 : auto quote_time{GncDateTime(GncDate(*p.date, "m-d-y"))};
592 : 0 : PINFO("Quote date included, using %s for %s:%s",
593 : : quote_time.format("%Y-%m-%d %H:%M:%S %z").c_str(), p.ns, p.mnemonic);
594 : 0 : return static_cast<time64>(quote_time);
595 : 0 : }
596 : 0 : catch (const std::exception &err)
597 : : {
598 : 0 : auto now{GncDateTime()};
599 : 0 : PWARN("Warning: failed to parse quote date '%s' for %s:%s because %s - will use %s",
600 : : p.date->c_str(), p.ns, p.mnemonic, err.what(), now.format("%Y-%m-%d %H:%M:%S %z").c_str());
601 : 0 : return static_cast<time64>(now);
602 : 0 : }
603 : : }
604 : :
605 : 0 : auto now{GncDateTime()};
606 : 0 : PINFO("No date was returned for %s:%s - will use %s",
607 : : p.ns, p.mnemonic, now.format("%Y-%m-%d %H:%M:%S %z").c_str());
608 : 0 : return static_cast<time64>(now);
609 : 0 : }
610 : :
611 : : static boost::optional<GncNumeric>
612 : 0 : get_price(const PriceParams& p)
613 : : {
614 : 0 : boost::optional<GncNumeric> price;
615 : : try
616 : : {
617 : 0 : price = GncNumeric { *p.price };
618 : : }
619 : 0 : catch (...)
620 : : {
621 : 0 : PWARN("Skipped %s:%s - failed to parse returned price '%s'",
622 : : p.ns, p.mnemonic, p.price->c_str());
623 : 0 : }
624 : :
625 : 0 : if (price && p.inverted)
626 : 0 : *price = price->inv();
627 : :
628 : 0 : return price;
629 : 0 : }
630 : :
631 : : static gnc_commodity*
632 : 0 : get_currency(const PriceParams& p, QofBook* book, QFVec& failures)
633 : : {
634 : 0 : if (!p.currency)
635 : : {
636 : 0 : failures.emplace_back(p.ns, p.mnemonic, GncQuoteError::NO_CURRENCY,
637 : : empty_string);
638 : 0 : PWARN("Skipped %s:%s - Finance::Quote returned a quote with no currency",
639 : : p.ns, p.mnemonic);
640 : 0 : return nullptr;
641 : : }
642 : 0 : std::string curr_str = *p.currency;
643 : 0 : boost::to_upper (curr_str);
644 : 0 : auto commodity_table = gnc_commodity_table_get_table (book);
645 : 0 : auto currency = gnc_commodity_table_lookup (commodity_table, "ISO4217", curr_str.c_str());
646 : :
647 : 0 : if (!currency)
648 : : {
649 : 0 : failures.emplace_back(p.ns, p.mnemonic,
650 : 0 : GncQuoteError::UNKNOWN_CURRENCY, empty_string);
651 : 0 : PWARN("Skipped %s:%s - failed to parse returned currency '%s'",
652 : : p.ns, p.mnemonic, p.currency->c_str());
653 : 0 : return nullptr;
654 : : }
655 : :
656 : 0 : return currency;
657 : 0 : }
658 : :
659 : : GNCPrice*
660 : 0 : GncQuotesImpl::parse_one_quote(const bpt::ptree& pt, gnc_commodity* comm)
661 : : {
662 : 0 : PriceParams p;
663 : 0 : bpt::ptree comm_pt;
664 : :
665 : 0 : p.ns = gnc_commodity_get_namespace (comm);
666 : 0 : p.mnemonic = gnc_commodity_get_mnemonic (comm);
667 : 0 : if (gnc_commodity_equiv(comm, m_dflt_curr) ||
668 : 0 : (!p.mnemonic || (strcmp (p.mnemonic, "XXX") == 0)))
669 : 0 : return nullptr;
670 : 0 : auto source{gnc_quote_source_get_internal_name(gnc_commodity_get_quote_source(comm))};
671 : 0 : auto source_pt_ai{pt.find(source)};
672 : 0 : auto ok{source_pt_ai != pt.not_found()};
673 : 0 : if (ok)
674 : : {
675 : 0 : auto comm_pt_ai{source_pt_ai->second.find(p.mnemonic)};
676 : 0 : ok = (comm_pt_ai != pt.not_found());
677 : 0 : if (ok)
678 : 0 : comm_pt = comm_pt_ai->second;
679 : : }
680 : 0 : if (!ok)
681 : : {
682 : 0 : m_failures.emplace_back(p.ns, p.mnemonic, GncQuoteError::NO_RESULT,
683 : : empty_string);
684 : 0 : PINFO("Skipped %s:%s - Finance::Quote didn't return any data from %s.",
685 : : p.ns, p.mnemonic, source);
686 : 0 : return nullptr;
687 : : }
688 : :
689 : 0 : parse_quote_json(p, comm_pt);
690 : 0 : if (!p.success)
691 : : {
692 : 0 : m_failures.emplace_back(p.ns, p.mnemonic, GncQuoteError::QUOTE_FAILED,
693 : 0 : p.errormsg ? *p.errormsg : empty_string);
694 : 0 : PWARN("Skipped %s:%s - Finance::Quote returned fetch failure.\nReason %s",
695 : : p.ns, p.mnemonic,
696 : : (p.errormsg ? p.errormsg->c_str() : "unknown"));
697 : 0 : return nullptr;
698 : : }
699 : :
700 : 0 : if (!p.price)
701 : : {
702 : 0 : m_failures.emplace_back(p.ns, p.mnemonic,
703 : 0 : GncQuoteError::NO_PRICE, empty_string);
704 : 0 : PWARN("Skipped %s:%s - Finance::Quote didn't return a valid price",
705 : : p.ns, p.mnemonic);
706 : 0 : return nullptr;
707 : : }
708 : :
709 : 0 : auto price{get_price(p)};
710 : 0 : if (!price)
711 : : {
712 : 0 : m_failures.emplace_back(p.ns, p.mnemonic,
713 : 0 : GncQuoteError::PRICE_PARSE_FAILURE,
714 : : empty_string);
715 : 0 : return nullptr;
716 : : }
717 : :
718 : 0 : auto currency{get_currency(p, m_book, m_failures)};
719 : 0 : if (!currency)
720 : 0 : return nullptr;
721 : :
722 : 0 : auto quotedt{calc_price_time(p)};
723 : 0 : auto gnc_price = gnc_price_create (m_book);
724 : 0 : gnc_price_begin_edit (gnc_price);
725 : 0 : gnc_price_set_commodity (gnc_price, comm);
726 : 0 : gnc_price_set_currency (gnc_price, currency);
727 : 0 : gnc_price_set_time64 (gnc_price, static_cast<time64> (quotedt));
728 : 0 : gnc_price_set_source (gnc_price, PRICE_SOURCE_FQ);
729 : 0 : gnc_price_set_typestr (gnc_price, p.type.c_str());
730 : 0 : gnc_price_set_value (gnc_price, *price);
731 : 0 : gnc_price_commit_edit (gnc_price);
732 : 0 : return gnc_price;
733 : 0 : }
734 : :
735 : : bpt::ptree
736 : 0 : GncQuotesImpl::parse_quotes (const std::string& quote_str)
737 : : {
738 : 0 : bpt::ptree pt;
739 : 0 : std::istringstream ss {quote_str};
740 : 0 : std::string what;
741 : :
742 : : try
743 : : {
744 : 0 : bpt::read_json (ss, pt);
745 : : }
746 : 0 : catch (bpt::json_parser_error &e) {
747 : 0 : what = e.what();
748 : 0 : }
749 : 0 : catch (const std::runtime_error& e)
750 : : {
751 : 0 : what = e.what();
752 : 0 : }
753 : 0 : catch (const std::logic_error& e)
754 : : {
755 : 0 : what = e.what();
756 : 0 : }
757 : 0 : catch (...) {
758 : 0 : std::string error_msg{_("Failed to parse result returned by Finance::Quote.")};
759 : 0 : error_msg += "\n";
760 : : //Translators: This labels the return value of a query to Finance::Quote written in an error.
761 : 0 : error_msg += _("Result:");
762 : 0 : error_msg += "\n";
763 : 0 : error_msg += quote_str;
764 : 0 : throw(GncQuoteException(error_msg));
765 : 0 : }
766 : 0 : if (!what.empty())
767 : : {
768 : 0 : std::string error_msg{_("Failed to parse result returned by Finance::Quote.")};
769 : 0 : error_msg += "\n";
770 : : //Translators: This is the error message reported by the Online Quotes processing code.
771 : 0 : error_msg += _("Error message:");
772 : 0 : error_msg += "\n";
773 : 0 : error_msg += what;
774 : 0 : error_msg += "\n";
775 : : //Translators: This labels the return value of a query to Finance::Quote written in an error.
776 : 0 : error_msg += _("Result:");
777 : 0 : error_msg += "\n";
778 : 0 : error_msg += quote_str;
779 : 0 : throw(GncQuoteException(error_msg));
780 : 0 : }
781 : 0 : return pt;
782 : 0 : }
783 : :
784 : : void
785 : 0 : GncQuotesImpl::create_quotes (const bpt::ptree& pt, const CommVec& comm_vec)
786 : : {
787 : 0 : auto pricedb{gnc_pricedb_get_db(m_book)};
788 : 0 : for (auto comm : comm_vec)
789 : : {
790 : 0 : auto price{parse_one_quote(pt, comm)};
791 : 0 : if (!price)
792 : 0 : continue;
793 : : // See the comment at gnc_pricedb_add_price
794 : 0 : gnc_pricedb_add_price(pricedb, price);
795 : : }
796 : 0 : }
797 : :
798 : : static void
799 : 0 : show_verbose_quote(const bpt::ptree& comm_pt)
800 : : {
801 : 0 : std::for_each(comm_pt.begin(), comm_pt.end(),
802 : 0 : [](auto elem) {
803 : 0 : std::cout << std::setw(12) << std::right << elem.first << " => " <<
804 : 0 : std::left << elem.second.data() << "\n";
805 : 0 : });
806 : 0 : std::cout << std::endl;
807 : 0 : }
808 : :
809 : : static void
810 : 0 : show_gnucash_quote(const bpt::ptree& comm_pt)
811 : : {
812 : 0 : constexpr const char* ptr{"<=== "};
813 : 0 : constexpr const char* dptr{"<=\\ "};
814 : 0 : constexpr const char* uptr{"<=/ "};
815 : : //Translators: Means that the preceding element is required
816 : 0 : const char* reqd{C_("Finance::Quote", "required")};
817 : : //Translators: Means that the quote will work best if the preceding element is provided
818 : 0 : const char* rec{C_("Finance::Quote", "recommended")};
819 : : //Translators: Means that one of the indicated elements is required
820 : 0 : const char* oot{C_("Finance::Quote", "one of these")};
821 : : //Translators: Means that a required element wasn't reported. The *s are for emphasis.
822 : 0 : const char* miss{C_("Finance::Quote", "**missing**")};
823 : :
824 : 0 : const std::string miss_str{miss};
825 : 0 : auto outline{[](const char* label, std::string value, const char* pointer, const char* req) {
826 : 0 : std::cout << std::setw(12) << std::right << label << std::setw(16) << std::left <<
827 : 0 : value << pointer << req << "\n";
828 : 0 : }};
829 : 0 : std::cout << _("Finance::Quote fields GnuCash uses:") << "\n";
830 : : //Translators: The stock or Mutual Fund symbol, ISIN, CUSIP, etc.
831 : 0 : outline(C_("Finance::Quote", "symbol: "), comm_pt.get<char>("symbol", miss), ptr, reqd);
832 : : //Translators: The date of the quote.
833 : 0 : outline(C_("Finance::Quote", "date: "), comm_pt.get<char>("date", miss), ptr, rec);
834 : : //Translators: The quote currency
835 : 0 : outline(C_("Finance::Quote", "currency: "), comm_pt.get<char>("currency", miss), ptr, reqd);
836 : 0 : auto last{comm_pt.get<char>("last", "")};
837 : 0 : auto nav{comm_pt.get<char>("nav", "")};
838 : 0 : auto price{comm_pt.get<char>("nav", "")};
839 : 0 : auto no_price{last.empty() && nav.empty() && price.empty()};
840 : : //Translators: The quote is for the most recent trade on the exchange
841 : 0 : outline(C_("Finance::Quote", "last: "), no_price ? miss_str : last, dptr, "");
842 : : //Translators: The quote is for an open-ended mutual fund and represents the net asset value of one unit of the fund at the previous close of trading.
843 : 0 : outline(C_("Finance::Quote", "nav: "), no_price ? miss_str : nav, ptr, oot);
844 : : //Translators: The quote is neither a last trade nor an NAV.
845 : 0 : outline(C_("Finance::Quote", "price: "), no_price ? miss_str : price, uptr, "");
846 : 0 : std::cout << std::endl;
847 : 0 : }
848 : : static const bpt::ptree empty_tree{};
849 : :
850 : : static inline const bpt::ptree&
851 : 0 : get_commodity_data(const bpt::ptree& pt, const std::string& comm)
852 : : {
853 : 0 : auto commdata{pt.find(comm)};
854 : 0 : if (commdata == pt.not_found())
855 : : {
856 : 0 : std::cout << comm << " " << _("Finance::Quote returned no data and set no error.") << std::endl;
857 : 0 : return empty_tree;
858 : : }
859 : 0 : auto& comm_pt{commdata->second};
860 : 0 : auto success = comm_pt.get_optional<bool> ("success");
861 : 0 : if (!(success && *success))
862 : : {
863 : 0 : auto errormsg = comm_pt.get_optional<std::string> ("errormsg");
864 : 0 : if (errormsg && !errormsg->empty())
865 : 0 : std::cout << _("Finance::Quote reported a failure for symbol ") <<
866 : 0 : comm << ": " << *errormsg << std::endl;
867 : : else
868 : 0 : std::cout << _("Finance::Quote failed silently to retrieve a quote for symbol ") <<
869 : 0 : comm << std::endl;
870 : 0 : return empty_tree;
871 : 0 : }
872 : 0 : return comm_pt;
873 : : }
874 : :
875 : : static void
876 : 0 : show_quotes(const bpt::ptree& pt, const StrVec& commodities, bool verbose)
877 : : {
878 : 0 : for (const auto& comm : commodities)
879 : : {
880 : 0 : auto comm_pt{get_commodity_data(pt, comm)};
881 : :
882 : 0 : if (comm_pt == empty_tree)
883 : 0 : continue;
884 : :
885 : 0 : if (verbose)
886 : : {
887 : 0 : std::cout << comm << ":\n";
888 : 0 : show_verbose_quote(comm_pt);
889 : : }
890 : : else
891 : : {
892 : 0 : show_gnucash_quote(comm_pt);
893 : : }
894 : 0 : }
895 : 0 : }
896 : :
897 : : static void
898 : 0 : show_currency_quotes(const bpt::ptree& pt, const StrVec& commodities, bool verbose)
899 : : {
900 : 0 : auto to_cur{commodities.front()};
901 : 0 : for (const auto& comm : commodities)
902 : : {
903 : 0 : if (comm == to_cur)
904 : 0 : continue;
905 : :
906 : 0 : auto comm_pt{get_commodity_data(pt, comm)};
907 : :
908 : 0 : if (comm_pt == empty_tree)
909 : 0 : continue;
910 : :
911 : 0 : if (verbose)
912 : : {
913 : 0 : std::cout << comm << ":\n";
914 : 0 : show_verbose_quote(comm_pt);
915 : : }
916 : : else
917 : : {
918 : : std::cout << "1 " << comm << " = " <<
919 : 0 : comm_pt.get<char>("last", "Not Found") << " " << to_cur << "\n";
920 : : }
921 : 0 : std::cout << std::endl;
922 : 0 : }
923 : 0 : }
924 : :
925 : : static std::string
926 : 0 : parse_quotesource_error(const std::string& line)
927 : : {
928 : 0 : std::string err_str;
929 : 0 : if (line == "invalid_json\n")
930 : : {
931 : 0 : err_str += _("GnuCash submitted invalid json to Finance::Quote. The details were logged.");
932 : : }
933 : 0 : else if (line.substr(0, 15) == "missing_modules")
934 : : {
935 : 0 : PERR("Missing Finance::Quote Dependencies: %s",
936 : : line.substr(17).c_str());
937 : 0 : err_str += _("Perl is missing the following modules. Please see https://wiki.gnucash.org/wiki/Online_Quotes#Finance::Quote for detailed corrective action. ");
938 : 0 : err_str += line.substr(17);
939 : : }
940 : : else
941 : : {
942 : 0 : PERR("Unrecognized Finance::Quote Error %s", line.c_str());
943 : 0 : err_str +=_("Unrecognized Finance::Quote Error: ");
944 : 0 : err_str += line;
945 : : }
946 : 0 : err_str += "\n";
947 : 0 : return err_str;
948 : 0 : }
949 : :
950 : : /********************************************************************
951 : : * gnc_quotes_get_quotable_commodities
952 : : * list commodities in a given namespace that get price quotes
953 : : ********************************************************************/
954 : : /* Helper function to be passed to g_list_for_each applied to the result
955 : : * of gnc_commodity_namespace_get_commodity_list.
956 : : */
957 : : static void
958 : 0 : get_quotables_helper1 (gpointer value, gpointer data)
959 : : {
960 : 0 : auto l = static_cast<CommVec *> (data);
961 : 0 : auto comm = static_cast<gnc_commodity *> (value);
962 : 0 : auto quote_flag = gnc_commodity_get_quote_flag (comm);
963 : 0 : auto quote_source = gnc_commodity_get_quote_source (comm);
964 : 0 : auto quote_source_supported = gnc_quote_source_get_supported (quote_source);
965 : :
966 : 0 : if (!quote_flag ||
967 : 0 : !quote_source || !quote_source_supported)
968 : 0 : return;
969 : 0 : l->push_back (comm);
970 : : }
971 : :
972 : : // Helper function to be passed to gnc_commodity_table_for_each
973 : : static gboolean
974 : 0 : get_quotables_helper2 (gnc_commodity *comm, gpointer data)
975 : : {
976 : 0 : auto l = static_cast<CommVec *> (data);
977 : 0 : auto quote_flag = gnc_commodity_get_quote_flag (comm);
978 : 0 : auto quote_source = gnc_commodity_get_quote_source (comm);
979 : 0 : auto quote_source_supported = gnc_quote_source_get_supported (quote_source);
980 : :
981 : 0 : if (!quote_flag ||
982 : 0 : !quote_source || !quote_source_supported)
983 : 0 : return TRUE;
984 : 0 : l->push_back (comm);
985 : 0 : return TRUE;
986 : : }
987 : :
988 : : CommVec
989 : 0 : gnc_quotes_get_quotable_commodities (const gnc_commodity_table * table)
990 : : {
991 : 0 : gnc_commodity_namespace * ns = NULL;
992 : : const char *name_space;
993 : : GList * nslist, * tmp;
994 : 0 : CommVec l;
995 : : regex_t pattern;
996 : 0 : const char *expression = gnc_prefs_get_namespace_regexp ();
997 : :
998 : : // ENTER("table=%p, expression=%s", table, expression);
999 : 0 : if (!table)
1000 : 0 : return CommVec ();
1001 : :
1002 : 0 : if (expression && *expression)
1003 : : {
1004 : 0 : if (regcomp (&pattern, expression, REG_EXTENDED | REG_ICASE) != 0)
1005 : : {
1006 : : // LEAVE ("Cannot compile regex");
1007 : 0 : return CommVec ();
1008 : : }
1009 : :
1010 : 0 : nslist = gnc_commodity_table_get_namespaces (table);
1011 : 0 : for (tmp = nslist; tmp; tmp = tmp->next)
1012 : : {
1013 : 0 : name_space = static_cast<const char *> (tmp->data);
1014 : 0 : if (regexec (&pattern, name_space, 0, NULL, 0) == 0)
1015 : : {
1016 : : // DEBUG ("Running list of %s commodities", name_space);
1017 : 0 : ns = gnc_commodity_table_find_namespace (table, name_space);
1018 : 0 : if (ns)
1019 : : {
1020 : 0 : auto cm_list = gnc_commodity_namespace_get_commodity_list (ns);
1021 : 0 : g_list_foreach (cm_list, &get_quotables_helper1, (gpointer) &l);
1022 : 0 : g_list_free (cm_list);
1023 : : }
1024 : : }
1025 : : }
1026 : 0 : g_list_free (nslist);
1027 : 0 : regfree (&pattern);
1028 : 0 : }
1029 : : else
1030 : : {
1031 : 0 : gnc_commodity_table_foreach_commodity (table, get_quotables_helper2,
1032 : : (gpointer) &l);
1033 : : }
1034 : : //LEAVE ("list head %p", &l);
1035 : 0 : return l;
1036 : 0 : }
1037 : :
1038 : : /* Public interface functions */
1039 : : // Constructor - checks for presence of Finance::Quote and import version and quote sources
1040 : 0 : GncQuotes::GncQuotes ()
1041 : : {
1042 : : try
1043 : : {
1044 : 0 : m_impl = std::make_unique<GncQuotesImpl>();
1045 : 0 : } catch (const GncQuoteSourceError &err) {
1046 : 0 : throw(GncQuoteException(err.what()));
1047 : 0 : }
1048 : 0 : }
1049 : :
1050 : :
1051 : : void
1052 : 0 : GncQuotes::fetch (QofBook *book)
1053 : : {
1054 : 0 : m_impl->fetch (book);
1055 : 0 : }
1056 : :
1057 : 0 : void GncQuotes::fetch (CommVec& commodities)
1058 : : {
1059 : 0 : m_impl->fetch (commodities);
1060 : 0 : }
1061 : :
1062 : 0 : void GncQuotes::fetch (gnc_commodity *comm)
1063 : : {
1064 : 0 : m_impl->fetch (comm);
1065 : 0 : }
1066 : :
1067 : 0 : void GncQuotes::report (const char* source, const StrVec& commodities,
1068 : : bool verbose)
1069 : : {
1070 : 0 : m_impl->report(source, commodities, verbose);
1071 : 0 : }
1072 : :
1073 : 0 : const std::string& GncQuotes::version() noexcept
1074 : : {
1075 : 0 : return m_impl->version ();
1076 : : }
1077 : :
1078 : 0 : const QuoteSources& GncQuotes::sources() noexcept
1079 : : {
1080 : 0 : return m_impl->sources ();
1081 : : }
1082 : :
1083 : 0 : GncQuotes::~GncQuotes() = default;
1084 : :
1085 : : bool
1086 : 0 : GncQuotes::had_failures() noexcept
1087 : : {
1088 : 0 : return m_impl->had_failures();
1089 : : }
1090 : :
1091 : : const QFVec&
1092 : 0 : GncQuotes::failures() noexcept
1093 : : {
1094 : 0 : return m_impl->failures();
1095 : : }
1096 : :
1097 : : const std::string
1098 : 0 : GncQuotes::report_failures() noexcept
1099 : : {
1100 : 0 : return m_impl->report_failures();
1101 : : }
|