summaryrefslogtreecommitdiff
path: root/bot/src/rss.cpp
blob: a52c17634b3ed00d9fe843dd4e3e961a55cc8a57 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "rss.hpp"

#include <fmt/core.h>

#include <algorithm>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <memory>
#include <optional>
#include <pugixml.hpp>
#include <sstream>
#include <string>
#include <thread>
#include <vector>

#include "cpr/api.h"
#include "cpr/cprtypes.h"
#include "cpr/response.h"
#include "database.hpp"
#include "fmt/format.h"
#include "logger.hpp"
#include "schemas/event.hpp"
#include "schemas/stream.hpp"
#include "utils/events.hpp"

namespace bot {
  sol::table RSSChannel::as_lua_table(std::shared_ptr<sol::state> state) const {
    sol::table t = state->create_table();
    t["name"] = this->name;
    t["url"] = this->url;

    if (this->event.has_value()) {
      sol::table e = state->create_table();
      e["name"] = this->event->name;
      e["type"] = this->event->type;
      t["event"] = e;
    } else {
      t["event"] = sol::lua_nil;
    }

    sol::table ms = state->create_table();

    for (const RSSMessage &v : this->messages) {
      sol::table m = state->create_table();
      m["title"] = v.title;
      m["message"] = v.message;
      m["id"] = v.id;
      m["timestamp"] = v.timestamp;
      ms.add(m);
    }

    t["messages"] = ms;

    return t;
  }

  void RSSListener::run() {
    if (!this->configuration.url.rssbridge.has_value()) {
      log::error("RSSListener", "RSS Bridge is not set!");
      return;
    }

    while (true) {
      this->add_channels();
      this->check_channels();
      std::this_thread::sleep_for(std::chrono::seconds(30));
    }
  }

  bool RSSListener::has_channel(const std::string &url) const {
    return std::any_of(this->channels.begin(), this->channels.end(),
                       [&url](const RSSChannel &c) { return c.url == url; });
  }

  void RSSListener::add_channel(const std::string &url) {
    if (this->has_channel(url)) return;

    std::optional<RSSChannel> channel = get_rss_channel(url);
    if (channel.has_value()) {
      this->channels.push_back(channel.value());
    }
  }

  void RSSListener::remove_channel(const std::string &url) {
    if (!this->has_channel(url)) return;

    std::remove_if(this->channels.begin(), this->channels.end(),
                   [&url](const RSSChannel &c) { return c.url == url; });
  }

  void RSSListener::add_channels() {
    std::unique_ptr<db::BaseDatabase> conn =
        db::create_connection(this->configuration);

    db::DatabaseRows events = conn->exec(
        "SELECT event_type, name "
        "FROM events "
        "WHERE event_type BETWEEN $1 AND $2",
        {std::to_string(static_cast<int>(schemas::EventType::RSS)),
         std::to_string(static_cast<int>(schemas::EventType::TELEGRAM))});

    // adding new events
    for (db::DatabaseRow event : events) {
      std::string name = event.at("name");
      int type = std::stoi(event.at("event_type"));
      std::string bridge = "";
      bool useRSSBridge = true;

      switch (type) {
        case schemas::RSS:
          bridge = "RSS";
          useRSSBridge = false;
          break;
        case schemas::TWITTER:
          bridge = "FarsideNitterBridge";
          break;
        case schemas::TELEGRAM:
          bridge = "TelegramBridge";
          if (name[0] != '@') {
            name = "%40" + name;
          }
          break;
        default:
          break;
      }

      if (bridge.empty()) {
        log::warn("RSSListener",
                  "Failed to specify bridge: " + event.at("event_type"));
        continue;
      }

      if (std::any_of(this->channels.begin(), this->channels.end(),
                      [&name, &type](const RSSChannel &c) {
                        auto e = c.event;
                        if (!e.has_value()) {
                          return false;
                        }
                        return e->name == name && e->type == type;
                      })) {
        continue;
      }

      std::string url =
          useRSSBridge
              ? fmt::format(*this->configuration.url.rssbridge, bridge, name)
              : name;

      std::optional<RSSChannel> channel = get_rss_channel(url);
      if (!channel.has_value()) {
        log::warn("RSSListener", "No RSS feed on " + url);
        continue;
      }

      channel->event = {name, type};

      this->channels.push_back(*channel);
    }

    // removing old events
    auto channels = this->channels;
    for (RSSChannel c : channels) {
      if (!c.event.has_value()) {
        continue;
      }

      if (!std::any_of(events.begin(), events.end(),
                       [&c](const db::DatabaseRow &r) {
                         return r.at("name") == c.event->name &&
                                std::stoi(r.at("event_type")) == c.event->type;
                       })) {
        this->remove_channel(c.url);
      }
    }
  }

  void RSSListener::check_channels() {
    for (auto it = this->channels.begin(); it != this->channels.end(); ++it) {
      if (!it->event.has_value()) {
        continue;
      }

      std::optional<RSSChannel> channel = get_rss_channel(it->url);
      if (!channel.has_value()) {
        continue;
      }

      std::vector<RSSMessage> messages;
      for (auto mit = channel->messages.begin(); mit != channel->messages.end();
           ++mit) {
        if (!std::any_of(
                it->messages.begin(), it->messages.end(),
                [&mit](const RSSMessage &m) { return m.id == mit->id; })) {
          messages.push_back(*mit);
        }
      }

      if (messages.empty()) {
        continue;
      }

      // getting channels
      std::vector<schemas::Event> events = utils::get_events(
          db::create_connection(this->configuration), this->helix_client,
          this->irc_client.get_bot_id(), it->event->type, it->event->name);

      for (const schemas::Event &event : events) {
        int count = 0;

        for (RSSMessage message : messages) {
          if (count > 5) break;
          count++;

          std::string msg = event.message;

          int pos = msg.find("{channel_name}");
          if (pos != std::string::npos) msg.replace(pos, 14, it->name);

          pos = msg.find("{title}");
          if (pos != std::string::npos) msg.replace(pos, 7, message.title);

          pos = msg.find("{message}");
          if (pos != std::string::npos) msg.replace(pos, 9, message.message);

          pos = msg.find("{link}");
          if (pos != std::string::npos) msg.replace(pos, 6, message.id);

          this->irc_client.say(event.channel_alias_name, msg);
        }
      }
    }
  }

  std::optional<RSSChannel> get_rss_channel(const std::string &url) {
    cpr::Response response = cpr::Get(
        cpr::Url{url},
        cpr::Header{{"Accept", "application/xml"},
                    {"User-Agent", "https://github.com/ilotterytea/bot"}});

    if (response.status_code != 200) {
      return std::nullopt;
    }

    pugi::xml_document doc;
    if (!doc.load_string(response.text.c_str())) {
      return std::nullopt;
    }

    pugi::xml_node channel = doc.child("rss").child("channel");

    std::string channel_name = channel.child("title").text().as_string();
    std::string channel_url = channel.child("link").text().as_string();

    std::vector<RSSMessage> messages;
    for (pugi::xml_node item : channel.children("item")) {
      // parsing timestamp
      long timestamp = 0;
      std::string pubdate = item.child("pubDate").text().as_string();
      pubdate = pubdate.substr(0, pubdate.size() - 6);
      std::tm tm = {};
      std::istringstream ss(pubdate);
      ss >> std::get_time(&tm, "%a, %d %b %Y %H:%M:%S");
      if (!ss.fail()) {
        timestamp = timegm(&tm);
      }

      messages.push_back({item.child("title").text().as_string(),
                          item.child("guid").text().as_string(),
                          item.child("description").text().as_string(),
                          timestamp});
    }

    return (RSSChannel){channel_name, channel_url, std::nullopt, messages};
  }
}