close

  
<p> // mql2rss - generate an RSS feed given any MQL query
// Author: Arthur van Hoff

var utils = acre.require('utils');

var default_args = {
    title: "MQL Query",
    query: "{'type':'/people/person'}",
    service: "http://www.freebase.com",
    cursor: true,
    image: "optional",
    article: "omit",
    location: "optional",
};

//
// Parse url parameters and return feed.
//
function feedquery(params) {
    // set parameter defaults
    for (var arg in default_args) {
        if (typeof params[arg] == 'undefined' || params[arg] == '') {
            params[arg] = default_args[arg];
        }
    }

    // create the feed
    return feed(params['title'], params['query'], params['cursor'], params['service'], params['image'], params['article'], params['location']);
}

//
// Create a feed instance given a MQL query.
//
function feed(title, querystr, cursor, service, image, article, location) {
    var items = [];
    var feed = {items: items};
    feed.title = title;
    feed.icon = service + "/resources/images/chrome/freebase-mini-logo.gif";
    feed.link = service;


    // parse the query
    var query = null;
    try {
        query = utils.decode(querystr);
        if (utils.isArray(query)) {
            query = query[0];
        }
    } catch (e) {
        setError(service, feed, "Failed to Parse Query", querystr + ", e=" + e);
        return feed;
    }
    var querytype = query['type'];
    var resulttype = null;

    // add additional fields to the query
    query['m2r:name'] = null;
    query['m2r:type'] = [{id:null, name:null}];
    query['m2r:id'] = null;
    query['m2r:guid'] = null;
    query['m2r:/common/topic/image'] = {
        id: null,
        '/type/content/media_type': {'name':null, optional:true},
        size: {type: '/measurement_unit/rect_size', x:null, y:null, optional:true},
        limit: 1,
        optional: image != "required", 
    };
    query['m2r:/common/topic/article'] = {
        id: null,
        limit: 1,
        optional: article != 'required',
    };
    query['m2r:/location/location/geolocation'] = {
        latitude: {value: null},
        longitude: {value: null},
        optional: location != 'required',
    };
    if (location == "optional") {
        query['m2r:/type/reflect/any_master'] = {
            optional: true,
            limit: 1,
            link : {
                target: {
                    type: "/location/location",
                    geolocation: {
                        latitude: {value: null},
                        longitude: {value: null},
                    }
                }
            }
        }
    }
    
    // execute the query
    mjt.freebase.set_service_url(service);    
    var task = mjt.freebase.MqlRead([query], {cursor:cursor});
    if (task.state != 'ready') {
        setError(service, feed, "Query Failed", "service=" + service + ",message=" + JSON.stringify(task.messages) + ", query=" + JSON.stringify(query));
        return feed;
    }
    var q = task.enqueue();
    var nextcursor = task.next_cursor;

    // next link
    if (nextcursor != null) {
        feed.next = {
            rel:"next",
            href: acre.environ.server_protocol + "://" + acre.environ.server_name + "/feed?"
               + acre.formencode({query:querystr, cursor:nextcursor, title:title, service:service, image:image, article:article, location:location})
        }
    }

    // enumerate items
    for (var i = 0 ; i  img.size.y) {
                       item.thumbnail.width = 100;
                       item.thumbnail.height = Math.floor((100 * img.size.y) / img.size.x);
                   } else {
                       item.thumbnail.width = Math.floor((100 * img.size.x) / img.size.y);
                       item.thumbnail.height= 100;
                   }
                }
                if (img['/type/content/media_type'] != null) {
                    var type = img['/type/content/media_type'].name;
                    item.thumbnail.type = type;
                    item.image.type = type;
                }
            } else if (image == "fill") {
                item.thumbnail = {
                    url: service + "/api/trans/image_thumb/guid/9202a8c04000641f80000000086b6808?maxwidth=100&amp;maxheight=100",
                    width:100, height:100, type:"image/jpeg"
                };
                item.image = {
                    url: service + "/api/trans/raw/guid/9202a8c04000641f80000000086b6808",
                    width:300, height:300, type:"image/jpeg"
                };
            }
        }

        // location
        if (location != 'omit') {
            var geo = result['m2r:/location/location/geolocation'];
            if (geo == null &amp;&amp; result['m2r:/type/reflect/any_master'] != null) {
                geo = result['m2r:/type/reflect/any_master'].link.target.geolocation;
            }
            if (geo != null) {
                item.location = {latitude: geo.latitude.value, longitude: geo.longitude.value};
            }
        }

        // article
        var art = result['m2r:/common/topic/article'];
        if (art != null) {
            if (article != 'omit') {
                var txt = acre.urlfetch(service + "/api/trans/blurb" + art['id']);
                item.description = txt.body;
            } else {
                var description = "";
                for (var j = 0 ; j  0) {
                            description += ", ";
                        }
                        description += t.name;
                    }
                }
                item.description = description;
                item.article = {
                    url: service + "/api/trans/blurb" + art['id'],
                    type: "text/html"
                };
            }
        }


        // find the display name of the result
        if (querytype != null &amp;&amp; resulttype == null) {
            for (var j = 0 ; j </p>

Comments

Hide