Article Image
read

My buddy Will has a pretty neat piece of software that combines all of his social feeds up on github.  I decided to fork it and add a new module for last.fm.   Its super easy to add a new feed type (see code below):

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace CodersBlock.SuperFeed.Modules {
    public class LastFMFeedModule : FeedModuleXml {
        private string _username;
        private string _apiKey;

        public override string SourceName {
            get { return "Last.fm"; }
        }

        public override string SourceUri {
            get { return string.Format("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user={0}&api_key={1}&page=1&limit={2}", _username, _apiKey, _totalLimit.ToString()); }
        }

        public LastFMFeedModule(int totalLimit,string apiKey, string username)
            : base(totalLimit) {
            _username = username;
            _apiKey = apiKey;
        }

        protected override List ParseDocument(XDocument doc) {
            var items = new List(_totalLimit);
            if (doc != null) {

                items = (
                    from track in doc.Descendants("track")
                    select new FeedItem(this) {
                        Published = DateTime.Parse(track.Element("date").Value),
                        Title = track.Element("artist").Value + " - " + track.Element("name").Value,
                        Snippet = _username + " listened to " + track.Element("name").Value + " by " + track.Element("artist").Value + " from thier album " + track.Element("album").Value,
                        ImagePreviewUri = (
                            from image in track.Elements("image")
                            where image.Attribute("size").Value == "large"
                            select image.Value
                        ).Single(),
                        ImageThumbnailUri = (
                            from image in track.Elements("image")
                            where image.Attribute("size").Value == "medium"
                            select image.Value
                        ).Single(),
                        ViewUri = track.Element("url").Value
                    }
                ).Take(_totalLimit).ToList();
            }

            return items;
        }
    }
}
Blog Logo

Jeffery Yeary


Published

Image

Debug Ninja

This is the portfolio / blog / space on the internet of Jeffery Yeary, a software architect with a passion for music, coding, fitness and art.

Back to Overview All Posts