File: oldreader_api.cpp

package info (click to toggle)
newsbeuter 2.9-8
  • links: PTS
  • area: main
  • in suites: buster
  • size: 4,716 kB
  • sloc: cpp: 18,663; ruby: 1,797; xml: 350; sh: 200; makefile: 171; perl: 101
file content (337 lines) | stat: -rw-r--r-- 11,111 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include <vector>
#include <cstring>
#include <iostream>
#include <wordexp.h>

#include <oldreader_api.h>
#include <config.h>
#include <utils.h>

#include <unistd.h>

#include <curl/curl.h>
#include <json.h>

#define OLDREADER_LOGIN					"https://theoldreader.com/accounts/ClientLogin"
#define OLDREADER_API_PREFIX				"http://theoldreader.com/reader/api/0/"
#define OLDREADER_FEED_PREFIX				"http://theoldreader.com/reader/atom/"

#define OLDREADER_OUTPUT_SUFFIX "?output=json"

#define OLDREADER_SUBSCRIPTION_LIST		OLDREADER_API_PREFIX "subscription/list" OLDREADER_OUTPUT_SUFFIX
#define OLDREADER_API_MARK_ALL_READ_URL	OLDREADER_API_PREFIX "mark-all-as-read"
#define OLDREADER_API_EDIT_TAG_URL		OLDREADER_API_PREFIX "edit-tag"
#define OLDREADER_API_TOKEN_URL			OLDREADER_API_PREFIX "token"

// for reference, see https://github.com/theoldreader/api

namespace newsbeuter {

oldreader_api::oldreader_api(configcontainer * c) : remote_api(c) {
	// TODO
}

oldreader_api::~oldreader_api() {
	// TODO
}

bool oldreader_api::authenticate() {
	auth = retrieve_auth();
	LOG(LOG_DEBUG, "oldreader_api::authenticate: Auth = %s", auth.c_str());
	return auth != "";
}

static size_t my_write_data(void *buffer, size_t size, size_t nmemb, void *userp) {
	std::string * pbuf = static_cast<std::string *>(userp);
	pbuf->append(static_cast<const char *>(buffer), size * nmemb);
	return size * nmemb;
}

std::string oldreader_api::retrieve_auth() {
	CURL * handle = curl_easy_init();
	std::string user = cfg->get_configvalue("oldreader-login");
	bool flushed = false;

	if (user == "") {
		std::cout << std::endl;
		std::cout.flush();
		flushed = true;
		std::cout << "Username for The Old Reader: ";
		std::cin >> user;
		if (user == "") {
			return "";
		}
	}

	std::string pass = cfg->get_configvalue("oldreader-password");
	if( pass == "" ) {
		wordexp_t exp;
		std::ifstream ifs;
		wordexp(cfg->get_configvalue("oldreader-passwordfile").c_str(),&exp,0);
		ifs.open(exp.we_wordv[0]);
		wordfree(&exp);
		if (!ifs) {
			if(!flushed) {
				std::cout << std::endl;
				std::cout.flush();
			}
			// Find a way to do this in C++ by removing cin echoing.
			pass = std::string( getpass("Password for The Old Reader: ") );
		} else {
			std::getline(ifs, pass);
			if(pass == "") {
				return "";
			}
		}
	}
	char * username = curl_easy_escape(handle, user.c_str(), 0);
	char * password = curl_easy_escape(handle, pass.c_str(), 0);

	std::string postcontent = utils::strprintf("service=reader&Email=%s&Passwd=%s&source=%s%2F%s&accountType=HOSTED_OR_GOOGLE&continue=http://www.google.com/",
	                          username, password, PROGRAM_NAME, PROGRAM_VERSION);

	curl_free(username);
	curl_free(password);

	std::string result;

	utils::set_common_curl_options(handle, cfg);
	curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, my_write_data);
	curl_easy_setopt(handle, CURLOPT_WRITEDATA, &result);
	curl_easy_setopt(handle, CURLOPT_POSTFIELDS, postcontent.c_str());
	curl_easy_setopt(handle, CURLOPT_URL, OLDREADER_LOGIN);
	curl_easy_perform(handle);
	curl_easy_cleanup(handle);

	std::vector<std::string> lines = utils::tokenize(result);
	for (auto line : lines) {
		LOG(LOG_DEBUG, "oldreader_api::retrieve_auth: line = %s", line.c_str());
		if (line.substr(0,5)=="Auth=") {
			std::string auth = line.substr(5, line.length()-5);
			return auth;
		}
	}

	return "";
}

std::vector<tagged_feedurl> oldreader_api::get_subscribed_urls() {
	std::vector<tagged_feedurl> urls;
	curl_slist* custom_headers {};

	CURL * handle = curl_easy_init();
	std::string result;
	configure_handle(&custom_headers);
	curl_easy_setopt(handle, CURLOPT_HTTPHEADER, custom_headers);

	utils::set_common_curl_options(handle, cfg);
	curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, my_write_data);
	curl_easy_setopt(handle, CURLOPT_WRITEDATA, &result);
	curl_easy_setopt(handle, CURLOPT_URL, OLDREADER_SUBSCRIPTION_LIST);
	curl_easy_perform(handle);
	curl_easy_cleanup(handle);

	LOG(LOG_DEBUG, "oldreader_api::get_subscribed_urls: document = %s", result.c_str());

	// TODO: parse result

	struct json_object * reply = json_tokener_parse(result.c_str());
	if (is_error(reply)) {
		LOG(LOG_ERROR, "oldreader_api::get_subscribed_urls: failed to parse response as JSON.");
		return urls;
	}


	struct json_object * subscription_obj {};
	json_object_object_get_ex(reply, "subscriptions", &subscription_obj);
	struct array_list * subscriptions = json_object_get_array(subscription_obj);

	int len = array_list_length(subscriptions);

	for (int i=0; i<len; i++) {
		std::vector<std::string> tags;
		struct json_object * sub = json_object_array_get_idx(subscription_obj, i);

		json_object* node {};

		json_object_object_get_ex(sub, "id", &node);
		const char * id = json_object_get_string(node);

		json_object_object_get_ex(sub, "title", &node);
		const char * title = json_object_get_string(node);

		// Ignore URLs where ID start with given prefix - those never load,
		// always returning 404 and annoying people
		const char* prefix = "tor/sponsored/";
		if(strncmp(id, prefix, strlen(prefix)) != 0) {
			tags.push_back(std::string("~") + title);

			auto url = utils::strprintf(
				"%s%s?n=%u",
				OLDREADER_FEED_PREFIX,
				id,
				cfg->get_configvalue_as_int("oldreader-min-items"));
			urls.push_back(tagged_feedurl(url, tags));
		}
	}

	json_object_put(reply);

	return urls;
}

void oldreader_api::configure_handle(curl_slist** custom_headers) {
	if(auth_header.empty()) {
		auth_header = utils::strprintf("Authorization: GoogleLogin auth=%s", auth.c_str());
	}
	LOG(LOG_DEBUG, "oldreader_api::configure_handle header = %s", auth_header.c_str());
	*custom_headers = curl_slist_append(*custom_headers, auth_header.c_str());
}

bool oldreader_api::mark_all_read(const std::string& feedurl) {
	std::string real_feedurl = feedurl.substr(strlen(OLDREADER_FEED_PREFIX), feedurl.length() - strlen(OLDREADER_FEED_PREFIX));
	std::vector<std::string> elems = utils::tokenize(real_feedurl, "?");
	real_feedurl = utils::unescape_url(elems[0]);
	std::string token = get_new_token();

	std::string postcontent = utils::strprintf("s=%s&T=%s", real_feedurl.c_str(), token.c_str());

	std::string result = post_content(OLDREADER_API_MARK_ALL_READ_URL, postcontent);

	return result == "OK";
}

std::vector<std::string> oldreader_api::bulk_mark_articles_read(const std::vector<google_replay_pair>& actions) {
	std::vector<std::string> successful_tokens;
	std::string token = get_new_token();
	for (auto action : actions) {
		bool read;
		if (action.second == GOOGLE_MARK_READ) {
			read = true;
		} else if (action.second == GOOGLE_MARK_UNREAD) {
			read = false;
		} else {
			continue;
		}
		if (mark_article_read_with_token(action.first, read, token)) {
			successful_tokens.push_back(action.first);
		}
	}
	return successful_tokens;
}

bool oldreader_api::mark_article_read(const std::string& guid, bool read) {
	std::string token = get_new_token();
	return mark_article_read_with_token(guid, read, token);
}

bool oldreader_api::mark_article_read_with_token(const std::string& guid, bool read, const std::string& token) {
	std::string postcontent;

	if (read) {
		postcontent = utils::strprintf("i=%s&a=user/-/state/com.google/read&r=user/-/state/com.google/kept-unread&ac=edit&T=%s", guid.c_str(), token.c_str());
	} else {
		postcontent = utils::strprintf("i=%s&r=user/-/state/com.google/read&a=user/-/state/com.google/kept-unread&a=user/-/state/com.google/tracking-kept-unread&ac=edit&T=%s", guid.c_str(), token.c_str());
	}

	std::string result = post_content(OLDREADER_API_EDIT_TAG_URL, postcontent);

	LOG(LOG_DEBUG, "oldreader_api::mark_article_read_with_token: postcontent = %s result = %s", postcontent.c_str(), result.c_str());

	return result == "OK";
}

std::string oldreader_api::get_new_token() {
	CURL * handle = curl_easy_init();
	std::string result;
	curl_slist* custom_headers {};

	utils::set_common_curl_options(handle, cfg);
	configure_handle(&custom_headers);
	curl_easy_setopt(handle, CURLOPT_HTTPHEADER, custom_headers);
	curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, my_write_data);
	curl_easy_setopt(handle, CURLOPT_WRITEDATA, &result);
	curl_easy_setopt(handle, CURLOPT_URL, OLDREADER_API_TOKEN_URL);
	curl_easy_perform(handle);
	curl_easy_cleanup(handle);

	LOG(LOG_DEBUG, "oldreader_api::get_new_token: token = %s", result.c_str());

	return result;
}

bool oldreader_api::update_article_flags(const std::string& oldflags, const std::string& newflags, const std::string& guid) {
	std::string star_flag = cfg->get_configvalue("oldreader-flag-star");
	std::string share_flag = cfg->get_configvalue("oldreader-flag-share");
	bool success = true;

	if (star_flag.length() > 0) {
		if (strchr(oldflags.c_str(), star_flag[0])==NULL && strchr(newflags.c_str(), star_flag[0])!=NULL) {
			success = star_article(guid, true);
		} else if (strchr(oldflags.c_str(), star_flag[0])!=NULL && strchr(newflags.c_str(), star_flag[0])==NULL) {
			success = star_article(guid, false);
		}
	}

	if (share_flag.length() > 0) {
		if (strchr(oldflags.c_str(), share_flag[0])==NULL && strchr(newflags.c_str(), share_flag[0])!=NULL) {
			success = share_article(guid, true);
		} else if (strchr(oldflags.c_str(), share_flag[0])!=NULL && strchr(newflags.c_str(), share_flag[0])==NULL) {
			success = share_article(guid, false);
		}
	}

	return success;
}

bool oldreader_api::star_article(const std::string& guid, bool star) {
	std::string token = get_new_token();
	std::string postcontent;

	if (star) {
		postcontent = utils::strprintf("i=%s&a=user/-/state/com.google/starred&ac=edit&T=%s", guid.c_str(), token.c_str());
	} else {
		postcontent = utils::strprintf("i=%s&r=user/-/state/com.google/starred&ac=edit&T=%s", guid.c_str(), token.c_str());
	}

	std::string result = post_content(OLDREADER_API_EDIT_TAG_URL, postcontent);

	return result == "OK";
}

bool oldreader_api::share_article(const std::string& guid, bool share) {
	std::string token = get_new_token();
	std::string postcontent;

	if (share) {
		postcontent = utils::strprintf("i=%s&a=user/-/state/com.google/broadcast&ac=edit&T=%s", guid.c_str(), token.c_str());
	} else {
		postcontent = utils::strprintf("i=%s&r=user/-/state/com.google/broadcast&ac=edit&T=%s", guid.c_str(), token.c_str());
	}

	std::string result = post_content(OLDREADER_API_EDIT_TAG_URL, postcontent);

	return result == "OK";
}

std::string oldreader_api::post_content(const std::string& url, const std::string& postdata) {
	std::string result;
	curl_slist* custom_headers {};

	CURL * handle = curl_easy_init();
	utils::set_common_curl_options(handle, cfg);
	configure_handle(&custom_headers);
	curl_easy_setopt(handle, CURLOPT_HTTPHEADER, custom_headers);
	curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, my_write_data);
	curl_easy_setopt(handle, CURLOPT_WRITEDATA, &result);
	curl_easy_setopt(handle, CURLOPT_POSTFIELDS, postdata.c_str());
	curl_easy_setopt(handle, CURLOPT_URL, url.c_str());
	curl_easy_perform(handle);
	curl_easy_cleanup(handle);

	LOG(LOG_DEBUG, "oldreader_api::post_content: url = %s postdata = %s result = %s", url.c_str(), postdata.c_str(), result.c_str());

	return result;
}

}