2008年 02月 18日

Subscription::LivedoorReader + OpenFL でレートを同期する。

追記

LDR をフィード管理とメインのフィードクローラをつかっていこうとおもうのですが、レートをつけるのがめんどうなので LDR のレートを OpenFL に反映させます。

// require JSDeferred
unsafeWindow.LDR.register_hook('before_printfeed', function (e) {
	next(function () {
		console.log(e);
		var url = e.channel.feedlink;
		return parallel({
			api_key: getApiKey(),
			id: xhttp.get("http://reader.livedoor.com/subscribe/"+url).next(function (res) {
				var m = res.responseText.match(/button class="subs_edit" rel="edit:(\d+)"/);
				return m ? m[1] : null;
			})
		}).
		next(function (data) {
			console.log("Getting rate:"+uneval(data));
			if (data.id) {
				return xhttp.post(
					"http://reader.livedoor.com/api/feed/subscribed",
					"subscribe_id="+data.id+"&ApiKey="+data.api_key
				).
				next(function (res) {
					res = eval("("+res.responseText+")");
					console.log("rate:"+res.rate);
					unsafeWindow.set_rate(e.subscribe_id, res.rate);
				})
			} else {
				// not rated
				console.log("unrated");
			}
		});
	}).
	error(function (e) {
		alert(e);
	});
});

function getApiKey () {
	var callee = arguments.callee;
	return callee._cache ? next(function () callee._cache) : xhttp.get("http://reader.livedoor.com/reader/").next(function (res) {
		api_key = res.responseText.match(/var ApiKey = "([^"]+)";/)[1];
		callee._cache = api_key;
		return api_key;
	});
}

OpenFL 側でフィードをすすめると LDR からレート設定をもってきて反映させます。(一気にやるやつじゃない)

(あとでちゃんと GM のファイルにする)

OpenFL 側の UI から LDR へレートを反映させたいけどまた今度でいいや……

やっぱフィードがロードされたら subs.model.list をなめて一気にやったほうがいいかもなぁ。OpenFL -> LDR の伝播をどうするかだなぁ。

というか GM でやる必要はないよなぁ