Building an extensible news feed reader

We built a news reader client for Android using Titanium called hackurls which shows the top news from some of the best technology news sharing websites. How long it took us? Less than a day. Although, I wouldn’t call the UI for the client to be very good, I find the app in itself to be very useful. Building this was fun and it wasn’t overly complicated. We built simple parsers for the feeds to get them to a common format so that our rendering could be done easily. This also leaves us a lot of space to improve the user experience while keeping most of our code for the backend un touched. Here’s an example for the service that retrieves news from Hackernews.

function HackerNews() {

    var STR_HN_URL = 'https://hndroidapi.appspot.com/news/format/json/page/?appid=YOUR_APP_ID';
    var STR_HN_BASE = 'https://news.ycombinator.com/';

    /**
     * Gets the domain of a url
     */
    function getDomain(url) {
        var match = url.match(/:\/\/(.[^/]+)/);
        if(match === null) {
            return ' ';
        }
        return (match[1]).replace('www.', '');
    }


    /**
     * Gets the post from Hackernews.
     */
    this.getPosts = function(callback) {
        var xhr = Titanium.Network.createHTTPClient();

        xhr.onload = function() {
            Ti.API.info(this.responseText);
            var parsedJson = JSON.parse(this.responseText);

            if(parsedJson !== undefined && parsedJson.items !== undefined) {
                var items = [];
                for(var i = 0, j = parsedJson.items.length; i < j; i++) {
                    var post = parsedJson.items[i];
                    var domain = getDomain(post.url);
                    items.push({
                        title : post.title,
                        url : (domain == ' ') ? STR_HN_BASE + post.url : post.url,
                        id : post.item_id,
                        commentCount : post.comments,
                        time : post.time,
                        timetype : 'pretty',
                        details : domain,
                        user : post.user,
                        points : post.score,
                        description : post.description
                    });
                };
                callback.call(this, items);
            }
        };
        // open the client
        xhr.open('GET', STR_HN_URL);

        // send the data
        xhr.send();

    };
};

module.exports = HackerNews;

Here’s another module that parses data for posts from Reddit and Proggit.

var STR_REDDIT_URL = 'https://www.reddit.com/hot.json';
var STR_PROGGIT_URL = 'https://www.reddit.com/r/programming/hot.json';

function Reddit(url, type) {
	var STR_REDDITBASE = 'https://www.reddit.com'
	this.getPosts = function(callback) {
		var xhr = Titanium.Network.createHTTPClient();

		xhr.onload = function() {
			var parsedJson = JSON.parse(this.responseText);
			if(parsedJson && parsedJson.data && parsedJson.data.children) {
				var items = [];
				for(var i = 0, j = parsedJson.data.children.length; i < j; i++) {
					var postData = parsedJson.data.children[i].data;
					items.push({
						title : postData.title,
						url : STR_REDDITBASE + postData.permalink,
						id : postData.id,
						commentCount : postData.num_comments,
						points : postData.score,
						time : postData.created_utc,
						timetype : 'timestamp',
						user : postData.author,
						thumbnail : postData.thumbnail,
						details : (type==='proggit')?postData.domain:postData.subreddit
					});
				};
				Ti.API.log(items);
				callback.call(this, items);
			}
		};
		// open the client
		xhr.open('GET', url);

		// send the data
		xhr.send();

	};
};

module.exports = Reddit;

Once we have all the posts with the details that we have here, building an amazing UI is the only thing that remains which I will leave up to you. There are similar parsers that we made for Slashdot, Techmeme, Wired and Dzone. If you want to give this a try, feel free to fork Hackurls on github or get Hackurls on Play store.

Published 23 Nov 2012

I build mobile and web applications. Full Stack, Rails, React, Typescript, Kotlin, Swift
Pulkit Goyal on Twitter