summaryrefslogtreecommitdiffstats
path: root/src/rttt.hpp
blob: d1efc93cd98a98b315d7aaf5a49c2fc84f386abe (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
#pragma once

#include <algorithm>
#include <chrono>
#include <iostream>
#include <optional>
#include <random>
#include <regex>
#include <sstream>
#include <string>
#include <variant>
#include <vector>

#include "rttt/config.hpp"
#include "rttt/item.hpp"
#include "rttt/logger.hpp"
#include "rttt/storage.hpp"

namespace rttt {

  // TODO: move to thing::type
  enum class SiteType : int {
    Unknown,
    HN,
    Reddit,
    RSS,
    Twitter,
  };

  // Consider moving this to view.hpp
  enum class list_mode : int {
    story,
    comment,
    feed,
  };

  // TODO: should work with constexpr, but might need newer g++/clang++
  inline std::vector<std::string> split(std::string_view strv,
                                        std::string_view delims = " ",
                                        size_t no_pieces = 0) {
    std::vector<std::string> output;
    size_t first = 0;

    while (first < strv.size()) {
      const auto second = strv.find_first_of(delims, first);

      if (first != second)
        output.emplace_back(strv.substr(first, second - first));

      if (second == std::string_view::npos) {
        break;
      } else if (no_pieces > 0 && output.size() >= no_pieces - 1) {
        output.emplace_back(strv.substr(second + 1, strv.size()));
        break;
      }

      first = second + 1;
    }

    return output;
  }

  inline void replaceAll(std::string & s, const std::string &search,
                         const std::string &replace) {
    for (size_t pos = 0;; pos += replace.length()) {
      pos = s.find(search, pos);
      if (pos == std::string::npos)
        break;
      s.erase(pos, search.length());
      s.insert(pos, replace);
    }
  }

  inline std::string parse_html(std::string str) {
    replaceAll(str, "<p>", "\n");

    std::string res;
    bool inTag = false;
    for (auto &ch : str) {
      if (ch == '<') {
        inTag = true;
      } else if (ch == '>') {
        inTag = false;
      } else {
        if (inTag == false) {
          res += ch;
        }
      }
    }

    replaceAll(res, "&#x2F;", "/");
    replaceAll(res, "&#x27;", "'");
    replaceAll(res, "&gt;", ">");
    replaceAll(res, "–", "-");
    replaceAll(res, "“", "\"");
    replaceAll(res, "”", "\"");
    replaceAll(res, "‘", "'");
    replaceAll(res, "’", "'");
    replaceAll(res, "„", "'");
    replaceAll(res, "&quot;", "\"");
    replaceAll(res, "&amp;", "&");
    replaceAll(res, "—", "-");

    return res;
  }

  inline std::tm parse_time(std::string time_string) {
    std::tm time = {};
    std::regex e("(\\d{4})-(\\d+)-(\\d+)T(\\d+):(\\d+)");
    std::smatch sm;
    std::regex_search(time_string, sm, e);
    if (sm.size() > 1) {
      time.tm_year = std::stoi(sm[1]) - 1900;
      time.tm_mon = std::stoi(sm[2]) - 1;
      time.tm_mday = std::stoi(sm[3]);
      time.tm_hour = std::stoi(sm[4]);
      time.tm_min = std::stoi(sm[5]);
    }
    return time;
  }

  // TODO: Move to text.hpp and rename
  inline std::vector<std::string> extractURL(std::string text) {
    std::vector<std::string> v;

    // Currently we assume an url always ends with alphanum, / or _
    // ([^/_[:alnum:]]) If this turns out to be false then we could also try to
    // filter out trailing punctuation marks etc specifically ([);:.!?]
    // std::regex e("(https?://[A-z0-9$–_.+!*‘(),./?=]+?)[),;:.!?]*(\\s|$)");
    // "(https?://[[:alnum:]$-_.+!*‘(),./?=;&#]+?)[^/_[:alnum:]]*(\\s|$)");
    std::regex e("(https?://[[:alnum:]$-_+!*‘,/?=;&#]+?)(\\]\\(|[^/"
                 "_[:alnum:]]*(\\s|$))");
    std::smatch sm;
    while (std::regex_search(text, sm, e)) {
      if (sm.size() > 1)
        v.push_back(sm[1]);
      text = sm.suffix().str();
    }

    return v;
  }

  struct Path {
    SiteType type = SiteType::Unknown;
    std::string name;
    std::string basename;
    list_mode mode = list_mode::story;
    std::string id;
    std::vector<std::string> parts;
  };

  inline Path parse_path(std::string_view path_name) {
    Path path;
    path.parts = split(path_name, "/");
    auto &v = path.parts;
    if (v[0] == "hn") {
      path.type = SiteType::HN;
    } else if (v[0] == "r") {
      path.type = SiteType::Reddit;
    } else if (v[0] == "rss") {
      path.type = SiteType::RSS;
      path.basename = "/rss";
      if (v.size() == 2 && v[1] != "front") {
        path.mode = list_mode::feed;
        path.id = v[1];
        path.name = path.basename + "/" + v[1];
      } else {
        path.name = path.basename;
        path.mode = list_mode::story;
      }
      return path;
    }
    if (v.size() == 4) {
      if (v[2] == "comments") {
        path.mode = list_mode::comment;
      } else {
        logger::log_ifnot(false);
      }
      path.name = "/" + v[0] + "/" + v[1] + "/" + v[2] + "/" + v[3];
      path.id = v[3];
    }
    path.basename = "/" + v[0] + "/" + v[1];
    if (path.name.empty())
      path.name = path.basename;
    return path;
  }

  inline std::string timeSince(uint64_t t) {
    auto delta = current_time() - t;
    if (delta < 60)
      return std::to_string(delta) + " seconds";
    if (delta < 3600)
      return std::to_string(delta / 60) + " minutes";
    if (delta < 24 * 3600)
      return std::to_string(delta / 3600) + " hours";
    return std::to_string(delta / 24 / 3600) + " days";
  }

  inline int openInBrowser(std::string uri) {
    auto config = rttt::config::load();
    auto base_cmd = config["open_command"].get<std::string>();
#ifdef __APPLE__
    if (base_command == "xdg-open")
      base_cmd = "open";
#endif
    // std::string cmd = "run-mailcap " + uri + " > /dev/null 2>&1";
    std::string cmd = base_cmd + " \"" + uri + "\" > /dev/null 2>&1 &";
    logger::push("EXECUTING: {}", cmd);
    return system(cmd.c_str());
  }
} // namespace rttt