f8g

Tomblooで自動タグ付加 その2

はてなブックマークからも取得、全ポストにタグ付け。いまいちだけどいいか。
del.icio.usMD5が面倒くさそう。

(function(){
	function Tag(init, args){
		this.tags = {};

		if(init && args)
			init.apply(this, args);
	}

	Tag.prototype = {
		add: function(tag, count){
			count = count || 1;
			tag = tag.toLowerCase();

			if(tag in this.tags)
				this.tags[tag] += count;
			else
				this.tags[tag] = count;

			return this.tags[tag];
		},

		toArray: function(len, threshold){
			var a = [{
				name    : name,
				count   : this.tags[name],
				toString: function(){
					return ("0000000000" + this.count.toString()).slice(-10);
				}
			} for(name in this.tags) if(typeof this.tags[name] == "number")];

			a.sort();
			a.reverse();

			if(len)
				a.length = len;

			if(threshold)
				a = [tag for each(tag in a) if(tag.count >= threshold)];

			return a;
		},

		toStrings: function(len, threshold){
			var a = this.toArray(len, threshold);
			return [tag.name for each(tag in a)];
		},

		merge: function(tag){
			if(tag) for(let name in tag.tags)
				this.add(name, tag.tags[name]);
			return this;
		}
	};

	models.LivedoorClip.getTags = function(itemUrl){
		var url = "http://api.clip.livedoor.com/json/comments?" + queryString({
			link: itemUrl,
			all : 1
		});

		return loadJSONDoc(url).addCallback(function(res){
			if(!res.isSuccess)
				return null;

			var init = function(comments){
				for each(let comment in comments) if(comment.tags){
					for each(let tag in comment.tags) if(typeof tag == "string")
						this.add(tag);
				}
			};

			return new Tag(init, [res.Comments]);
		});
	};

	models.HatenaBookmark.getTags = function(itemUrl){
		var url = "http://b.hatena.ne.jp/entry/json/?" + queryString({
			url: itemUrl
		});

		return loadJSONDoc(url).addCallback(function(res){
			if(!res)
				return null;

			var init = function(bookmarks){
				for each(let bookmark in bookmarks) if(bookmark.tags){
					for each(let tag in bookmark.tags) if(typeof tag == "string")
						this.add(tag);
				}
			};

			return new Tag(init, [res.bookmarks]);
		});
	}

	models.Delicious.getTags = function(itemUrl){
		var url = "http://feeds.delicious.com/v2/json/urlinfo/check?" + queryString({
			url: itemUrl
		});

		return loadJSONDoc(url).addCallback(function(res){
			if(!res)
				return null;

			var init = function(top_tags){
				this.tags = top_tags;
			};

			return new Tag(init, [res[0].top_tags]);
		});
	};
})();

addAround(Tombloo.Service, 'post', function(proceed, args){
	var use = {
		HatenaBookmark: true,
		LivedoorClip  : true,
		Delicious     : true
	};

	var ps = args[0];
	var res;

	for(let name in use) if(use[name]){
		if(!res){
			res = models[name].getTags(ps.itemUrl);
		}
		else{
			res.addCallback(function(t){
				if(!t)
					return models[name].getTags(ps.itemUrl);

				return models[name].getTags(ps.itemUrl).addCallback(function(tags){
					t.merge(tags);
					return t;
				});
			});
		}
	}

	if(!res)
		return proceed(args);

	return res.addCallback(function(tags){
		if(tags)
			ps.tags = tags.toStrings(3, 2);

		return proceed(args);
	});
});

修正

  • タグがないときのエラーを修正
  • 直した (明日)
  • 時にエラーなくポストされないことがあるみたいだけど、原因不明だから直してない
  • Deliciousを追加(さっき)