Joel Grunbaum
2022-01-13 90107c504e5b7d1bea2c93a43c7d07b640350355
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
#include "bom.hpp"
 
#include "book.hpp"
#include "ftplibpp/ftplib.h"
#include "rapidxml/rapidxml.hpp"
#include <cstddef>
#include <cstdio>
#include <fstream>
#include <iterator>
#include <string>
#include <unordered_map>
#include <vector>
 
namespace bom
{
ftplib* ftp;
 
void initialise()
{
    ftp = new ftplib();
    ftp->Connect("ftp.bom.gov.au:21");
    ftp->Login("anonymous", "");
}
 
void destroy()
{
    ftp->Quit();
    delete ftp;
}
 
void updateBom(std::unordered_map<std::string, book::Book>& bs)
{
    ftp->Get("bom_data.xml", "/anon/gen/fwo/IDN60920.xml",
             ftplib::transfermode::ascii);
    std::ifstream fs("bom_data.xml");
    std::vector<char> buffer{std::istreambuf_iterator<char>(fs),
                             istreambuf_iterator<char>()};
    buffer.push_back('\0');
    rapidxml::xml_document<> d;
    d.parse<0>(&buffer[0]);
    // Walk stations
    for (rapidxml::xml_node<>* n = d.first_node()->last_node()->first_node(); n;
         n = n->next_sibling()) {
        int bom_id = std::stoi(n->first_attribute()->next_attribute()->value());
        for (auto& i : bs) {
            if (i.second.stationId == bom_id) {
                // Should be apparent temp
                i.second.bomPrice = std::stod(n->first_node()
                                                  ->first_node()
                                                  ->first_node()
                                                  ->first_node()
                                                  ->value());
            }
        }
    }
}
} // namespace bom