File: newsblur_api.cpp

package info (click to toggle)
newsbeuter 2.9-5+deb9u2
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 4,692 kB
  • ctags: 3,693
  • sloc: cpp: 18,663; ruby: 1,797; xml: 350; sh: 199; makefile: 171; perl: 101
file content (235 lines) | stat: -rw-r--r-- 6,793 bytes parent folder | download | duplicates (2)
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
#include <rsspp.h>
#include <json.h>
#include <utils.h>
#include <remote_api.h>
#include <newsblur_api.h>
#include <algorithm>
#include <string.h>

#define NEWSBLUR_ITEMS_PER_PAGE 6

namespace newsbeuter {

newsblur_api::newsblur_api(configcontainer * c) : remote_api(c) {
	auth_info = utils::strprintf("username=%s&password=%s", cfg->get_configvalue("newsblur-login").c_str(), cfg->get_configvalue("newsblur-password").c_str());
	api_location = cfg->get_configvalue("newsblur-url");
	min_pages = (cfg->get_configvalue_as_int("newsblur-min-items") + (NEWSBLUR_ITEMS_PER_PAGE + 1)) / NEWSBLUR_ITEMS_PER_PAGE;

	if(cfg->get_configvalue("cookie-cache").empty()) {
		LOG(LOG_CRITICAL, "newsblur_api::newsblur_api: No cookie-cache has been configured the login won't work.");
	}
}

newsblur_api::~newsblur_api() {
}

bool newsblur_api::authenticate() {
	json_object * response {};
	json_object * status {};

	response = newsblur_api::query_api("/api/login", &auth_info);
	json_object_object_get_ex(response, "authenticated", &status);
	bool result = json_object_get_boolean(status);
	LOG(LOG_INFO, "newsblur_api::authenticate: authentication resulted in %u, cached in %s", result, cfg->get_configvalue("cookie-cache").c_str());
	return result;
}

std::vector<tagged_feedurl> newsblur_api::get_subscribed_urls() {
	std::vector<tagged_feedurl> result;

	json_object * response = query_api("/reader/feeds", NULL);

	json_object * feeds {};
	json_object_object_get_ex(response, "feeds", &feeds);

	json_object_iterator it = json_object_iter_begin(feeds);
	json_object_iterator itEnd = json_object_iter_end(feeds);

	while (!json_object_iter_equal(&it, &itEnd)) {
		const char * feed_id = json_object_iter_peek_name(&it);
		json_object * node {};
		rsspp::feed current_feed;

		current_feed.rss_version = rsspp::NEWSBLUR_JSON;

		json_object * feed_json = json_object_iter_peek_value(&it);
		json_object_object_get_ex(feed_json, "feed_title", &node);
		current_feed.title = json_object_get_string(node);
		json_object_object_get_ex(feed_json, "feed_link", &node);
		current_feed.link = json_object_get_string(node);

		known_feeds[feed_id] = current_feed;

		std::vector<std::string> tags = std::vector<std::string>();
		result.push_back(tagged_feedurl(std::string(feed_id), tags));

		json_object_iter_next(&it);
	}

	return result;
}

void newsblur_api::configure_handle(curl_slist** /* custom_headers */) {
	// nothing required
}

bool request_successfull(json_object * payload) {
	json_object * result {};
	if(json_object_object_get_ex(payload, "result", &result) == FALSE) {
		return false;
	} else {
		return !strcmp("ok", json_object_get_string(result));
	}
}

bool newsblur_api::mark_all_read(const std::string& feed_url) {
	std::string post_data = utils::strprintf("feed_id=%s", feed_url.c_str());
	json_object * query_result = query_api("/reader/mark_feed_as_read", &post_data);
	return request_successfull(query_result);
}

bool newsblur_api::mark_article_read(const std::string& guid, bool read) {
	std::string endpoint;
	int separator = guid.find(ID_SEPARATOR);
	std::string feed_id = guid.substr(0, separator);
	std::string article_id = guid.substr(separator + sizeof(ID_SEPARATOR) - 1);

	std::string post_data = "feed_id=" + feed_id + "&" + "story_id=" + article_id;
	if(read) {
		endpoint = "/reader/mark_story_as_read";
	} else {
		endpoint = "/reader/mark_story_as_unread";
	}

	json_object * query_result = query_api(endpoint, &post_data);
	return request_successfull(query_result);
}

bool newsblur_api::update_article_flags(const std::string& oldflags, const std::string& newflags, const std::string& guid) {
	(void)oldflags;
	(void)newflags;
	(void)guid;
	return false;
}

time_t parse_date(const char * raw) {
	struct tm tm;
	memset(&tm, 0, sizeof(tm));

	strptime(raw, "%Y-%m-%d %H:%M:%S", &tm);
	return mktime(&tm);
}

rsspp::feed newsblur_api::fetch_feed(const std::string& id) {
	rsspp::feed f = known_feeds[id];

	LOG(LOG_INFO, "newsblur_api::fetch_feed: about to fetch %u pages of feed %s", min_pages, id.c_str());

	for(unsigned int i = 1; i <= min_pages; i++) {

		std::string page = utils::to_string(i);

		json_object * query_result = query_api("/reader/feed/" + id + "?page=" + page, NULL);

		if (!query_result)
			return f;

		json_object * stories {};
		if(json_object_object_get_ex(query_result, "stories", &stories)
				== FALSE)
		{
			LOG(LOG_ERROR, "newsblur_api::fetch_feed: request returned no stories");
			return f;
		}

		if (json_object_get_type(stories) != json_type_array) {
			LOG(LOG_ERROR, "newsblur_api::fetch_feed: content is not an array");
			return f;
		}

		struct array_list * items = json_object_get_array(stories);
		int items_size = array_list_length(items);
		LOG(LOG_DEBUG, "newsblur_api::fetch_feed: %d items", items_size);

		for (int i = 0; i < items_size; i++) {
			struct json_object * item_obj = (struct json_object *)array_list_get_idx(items, i);

			rsspp::item item;

			json_object* node {};

			if(json_object_object_get_ex(item_obj, "story_title", &node)
					== TRUE)
			{
				item.title = json_object_get_string(node);
			}

			if(json_object_object_get_ex(item_obj, "story_permalink", &node)
					== TRUE)
			{
				item.link = json_object_get_string(node);
			}

			if(json_object_object_get_ex(item_obj, "story_content", &node)
					== TRUE)
			{
				item.content_encoded = json_object_get_string(node);
			}

			const char * article_id {};
			if(json_object_object_get_ex(item_obj, "id", &node) == TRUE) {
				article_id = json_object_get_string(node);
			}
			item.guid = id + ID_SEPARATOR + article_id;

			if(json_object_object_get_ex(item_obj, "read_status", &node)
					== TRUE)
			{
				if (! static_cast<bool>(json_object_get_int(node))) {
					item.labels.push_back("newsblur:unread");
				} else {
					item.labels.push_back("newsblur:read");
				}
			}

			if(json_object_object_get_ex(item_obj, "story_date", &node)
					== TRUE)
			{
				const char* pub_date = json_object_get_string(node);
				item.pubDate_ts = parse_date(pub_date);

				char rfc822_date[128];
				strftime(
						rfc822_date,
						sizeof(rfc822_date),
						"%a, %d %b %Y %H:%M:%S %z",
						gmtime(&item.pubDate_ts));
				item.pubDate = rfc822_date;
			}

			f.items.push_back(item);
		}
	}

	std::sort(
		f.items.begin(),
		f.items.end(),
		[](const rsspp::item& a, const rsspp::item& b) {
			return a.pubDate_ts > b.pubDate_ts;
	});

	return f;
}

json_object * newsblur_api::query_api(const std::string& endpoint, const std::string* postdata) {

	const char * url = (api_location + endpoint).c_str();
	std::string data = utils::retrieve_url(url, cfg, NULL, postdata);

	json_object * result =  json_tokener_parse(data.c_str());
	if(!result)
		LOG(LOG_WARN, "newsblur_api::query_api: request to %s failed", url);
	return result;
}

}