Browse Source

added tag clouds

pull/2/head
asiekierka 6 years ago
parent
commit
847e297a6f
5 changed files with 138 additions and 10 deletions
  1. 15
    3
      app.js
  2. 35
    0
      cloudgen.js
  3. 73
    7
      imagedb.js
  4. 14
    0
      templates/tagcloud.html
  5. 1
    0
      util.js

+ 15
- 3
app.js View File

@@ -16,7 +16,8 @@ var express = require('express')
, queryParser = require('./queryparser.js').QueryParser
, imageHandler = require('./image.js')
, tempurl = require('./tempurl.js')
, util = require('./util.js');
, util = require('./util.js')
, tagCloud = require('./cloudgen.js');

_.str = require('underscore.string');
_.mixin(_.str.exports());
@@ -322,6 +323,15 @@ function listImages(req,res,images1,options,defConfig,maxVal) {
});
}

app.get("/cloud/*", parse, function(req,res) {
if(req.params.length < 1 || !imageDB.isCloudTag(req.params[0])) { error(req,res,"No tag found!",404); return; }
imageDB.getCloud(req.params[0], function(cloudData) {
console.log(cloudData);
var cloudTag = tagCloud.generate(cloudData, {});
var opts = {title: "All "+req.params[0]+"s", tagCloud: cloudTag, req: req};
res.send(makeTemplate("tagcloud",opts,req.query["mode"] || "",false));
});
});
app.get("/*", function(req,res) {
var p = qs.unescape(req.path).split("/");
console.log("Request: " + JSON.stringify(p));
@@ -362,8 +372,10 @@ userDB.exists("admin", function(err, exists) {
});

function start() {
app.listen(config.port);
console.log("Working on port " + config.port);
imageDB.updateDatabase(function() {
app.listen(config.port);
console.log("Working on port " + config.port);
});
}

if(argv.r || argv.regen) {

+ 35
- 0
cloudgen.js View File

@@ -0,0 +1,35 @@
var _ = require("underscore");

var defOptions = {
"minFontSize": 10,
"maxFontSize": 30,
"template": "<a href='$TARGET' style='font-size: $SIZEpx' title='$COUNT times'>$TEXT</a>, ",
"targetTemplate": "/tag/$TEXT",
"alphaSort": true,
"useLog": true
}

exports.generate = function(array, userOptions) {
var nums = _.values(array)
, tags = _.keys(array)
, maxn = _.max(nums)
, minn = _.min(nums)
, options = _.defaults(userOptions, defOptions)
, cloud = "";
var multiplier = 0;
if(options.useLog)
multiplier = (options.maxFontSize - options.minFontSize) / (Math.log(maxn) - Math.log(minn));
else
multiplier = (options.maxFontSize - options.minFontSize) / (maxn - minn);
if(options.alphaSort) tags = tags.sort();
_.each(tags, function(tag) {
var val = (options.useLog ? Math.log(array[tag]) : array[tag]);
var target = options.targetTemplate.replace("$TEXT",tag);
var tag = options.template.replace("$TEXT",tag)
.replace("$COUNT",array[tag])
.replace("$SIZE",(options.minFontSize + (val*multiplier)))
.replace("$TARGET",target);
cloud += tag;
});
return cloud;
}

+ 73
- 7
imagedb.js View File

@@ -1,11 +1,76 @@
var ImageDB = {}
, redis = require("redis")
, _ = require("underscore")
, async = require("async");
, async = require("async")
, util = require("./util.js");

var client = null;
var client = null
, CURRENT_VERSION = 2
, cloudTags = ["tag", "author", "uploader"];

ImageDB.connect = function(cli) { client = cli; }
ImageDB.connect = function(cli) {
client = cli;
}
ImageDB.log = function(t) { console.log("[ImageDB] "+t); }
ImageDB.getVersion = function(cb) {
client.get("db_version",function(err, out) {
if(err) cb(1);
else cb((parseInt(out)>0) ? parseInt(out) : 1);
});
}
ImageDB.updateDatabase = function(callback) {
var self = this;
self.getVersion(function(version) {
if(version == 1) {
version = 2;
self.log("Updating DB from version 1 to 2");
self.updateCloud(function(){
self.log("Done updating!");
client.set("db_version", 2, _.bind(self.updateDatabase, self));
});
}
else if(version == CURRENT_VERSION) { self.log("Latest DB version, nothing to do..."); if(_.isFunction(callback)) callback(); }
});
}

// CLOUD CODE

ImageDB.isCloudTag = function(text) { return _(cloudTags).contains(text); }
ImageDB.updateCloud = function(callback) {
var self = this;
async.each(cloudTags, function(item, cb) {
self.generateCloud(item, cb);
}, callback);
}
ImageDB.getCloud = function(name, cb) {
client.zrange("cloud:"+name,0,-1,'WITHSCORES',function(err, data) {
if(err) throw err;
var fixedData = {};
while(data.length > 0) {
var name = data.shift();
fixedData[name] = parseInt(data.shift());
}
cb(fixedData);
});
}
ImageDB.generateCloud = function(name, cb) {
this.log("(Re)generating tag cloud "+name+"...");
client.smembers(name+"s", function(err, out) {
if(err) throw err;
var counts = async.map(out, function(tagName, callback) {
client.scard(name+":"+tagName, function(err, out) { // Count
callback(err, {"name": tagName, "count": out});
});
}, function(err, out) {
if(err) throw err;
var output = {};
async.eachSeries(out, function(tag, callback) {
if(tag.count == 0 || tag.name == "") callback();
else client.zadd("cloud:"+name, tag.count, tag.name, callback);
}, cb);
});
});
}

ImageDB.get = function(id,callback) {
client.get("data:"+id,function(err,out) {
@@ -144,7 +209,7 @@ ImageDB.unsetSearchData = function(id, data, callback) {
async.series([
_.bind(client.srem,client,"hashes",data.hash),
_.bind(self.unsetTags,self,id,data.tags),
function(cb) { console.log("dbg"); cb(); },
function(cb) { cb(); },
_.bind(self.delField,self,id,"author",data.author),
_.bind(self.delField,self,id,"uploader",data.uploader),
_.bind(self.delSize,self,id,"width"),
@@ -172,7 +237,8 @@ ImageDB.set = function(id,data,callback,noHashCheck) {
async.parallel([
_.bind(client.set,client,"data:"+id,JSON.stringify(data)),
_.bind(client.sadd,client,"images",id),
_.bind(self.setSearchData,self,id,data)
_.bind(self.setSearchData,self,id,data),
_.bind(self.updateCloud,self)
], callback);
};
if(exists) self.get(id,function(data) { self.unset(id,s,true); });
@@ -189,10 +255,10 @@ ImageDB.unset = function(id,callback,dontTouchData) {
async.parallel([
function(callback) { if(!dontTouchData) client.del("data:"+id,callback); else callback(); },
function(callback) { if(!dontTouchData) client.srem("images",id,callback); else callback(); },
_.bind(self.unsetSearchData,self,id,data)
_.bind(self.unsetSearchData,self,id,data),
_.bind(self.updateCloud,self)
],callback);
});
}


exports.ImageDB = ImageDB;

+ 14
- 0
templates/tagcloud.html View File

@@ -0,0 +1,14 @@
<div class="row padded-row">
<div class="span12" style="text-align: center;">
<h3><%= title %></h3>
</div>
</div>
<div class="row">
<div class="offset2 span8" style="text-align: center;">
<%= tagCloud %>
</div>
</div>
<script type="text/javascript">
function pageLoad() { }
function pageUnload() { }
</script>

+ 1
- 0
util.js View File

@@ -17,3 +17,4 @@ exports.copyFile = function(src,dest) {
exports.filesize = function(name) {
return fs.statSync(name).size;
}
exports.time = function(){ return Math.round(new Date().getTime() / 1000); }

Loading…
Cancel
Save