Joel Grunbaum
2022-01-12 e8c910e52d1807e2fcca3b43d80a9df6acac5387
commit | author | age
0594ac 1 #include "bom.hpp"
JG 2
3 #include "book.hpp"
611ad7 4 #include "ftplibpp/ftplib.h"
ae7d00 5 #include "rapidxml/rapidxml.hpp"
0594ac 6 #include <cstddef>
JG 7 #include <cstdio>
ae7d00 8 #include <fstream>
JG 9 #include <iterator>
0594ac 10 #include <string>
JG 11 #include <unordered_map>
ae7d00 12 #include <vector>
0594ac 13
JG 14 namespace bom
15 {
611ad7 16 ftplib* ftp;
ae7d00 17
JG 18 void initialise()
0594ac 19 {
611ad7 20     ftp = new ftplib();
JG 21     ftp->Connect("ftp.bom.gov.au:21");
22     ftp->Login("anonymous", "");
ae7d00 23 }
JG 24
611ad7 25 void destroy()
JG 26 {
27     ftp->Quit();
28     delete ftp;
29 }
ae7d00 30
JG 31 void updateBom(std::unordered_map<std::string, book::Book>& bs)
32 {
611ad7 33     ftp->Get("bom_data.xml", "/anon/gen/fwo/IDN60920.xml",
JG 34              ftplib::transfermode::ascii);
35     std::ifstream fs("bom_data.xml");
36     std::vector<char> buffer{std::istreambuf_iterator<char>(fs),
37                              istreambuf_iterator<char>()};
e8c910 38     buffer.push_back('\0');
ae7d00 39     rapidxml::xml_document<> d;
611ad7 40     d.parse<0>(&buffer[0]);
e8c910 41     // Walk stations
611ad7 42     for (rapidxml::xml_node<>* n = d.first_node()->last_node()->first_node(); n;
JG 43          n = n->next_sibling()) {
e8c910 44         int bom_id = std::stoi(n->first_attribute()->next_attribute()->value());
JG 45         for (auto& i : bs) {
46             if (i.second.stationId == bom_id) {
47                 // Should be apparent temp
48                 i.second.bomPrice = std::stod(n->first_node()
49                                                   ->first_node()
50                                                   ->first_node()
51                                                   ->first_node()
52                                                   ->value());
53             }
54         }
55     }
0594ac 56 }
JG 57 } // namespace bom