Searching Craigslist Using C#

I'm sort of all over the place with my projects as of late. But along with the Hulu, Netflix, etc. search functions, I wanted to be able to search other sites. One of these sites is Craigslist. I don't search it that often but it is one of those sites that I hit from time to time. So I figured how hard could it be to search it? Not that difficult it turns out:

    /// <summary>
    /// Craigslist helper
    /// </summary>
    public static class Craigslist
    {
        #region Public Static Functions

        /// <summary>
        /// Searches craigslist
        /// </summary>
        /// <param name="Site">Site to search (for instance http://charlottesville.craigslist.org/ for Charlottesville, VA)</param>
        /// <param name="Category">Category to search within</param>
        /// <param name="SearchString">Search term</param>
        /// <returns>RSS feed object</returns>
        public static Document Search(string Site, Category Category, string SearchString)
        {
            return new Document(Site + "/search/" + Names[(int)Category] + "?query=" + HttpUtility.UrlEncode(SearchString) + "&catAbbreviation=" + Names[(int)Category] + "&minAsk=min&maxAsk=max&format=rss");
        }

        #endregion

        #region Private Static Variables

        /// <summary>
        /// Abbreviations for categories that Craigslist uses
        /// </summary>
        private static string[] Names ={"sss","art","pts","bab","bar","bik","boa","bks","bfs","cta","ctd",
            "cto","emd","clo","clt","sys","ele","grd","zip","fua","fud","fuo","tag","gms","for","hsh",
            "wan","jwl","mat","mcy","msg","pho","rvs","spo","tix","tls"};

        #endregion
    }

    #region Enum

    /// <summary>
    /// Category to search within
    /// </summary>
    public enum Category
    {
        All_For_Sale,
        For_Sale_Arts_Crafts,
        For_Sale_Auto_Parts,
        For_Sale_Baby_Kid_Stuff,
        For_Sale_Barter,
        For_Sale_Bicycles,
        For_Sale_Boats,
        For_Sale_Books,
        For_Sale_Business,
        For_Sale_Cars_And_Trucks_All,
        For_Sale_Cars_And_Trucks_Dealer,
        For_Sale_Cars_And_Trucks_Owner,
        For_Sale_CDs_DVDs_VHS,
        For_Sale_Clothing,
        For_Sale_Collectibles,
        For_Sale_Computers_Tech,
        For_Sale_Electronics,
        For_Sale_Farm_Garden,
        For_Sale_Free_Stuff,
        For_Sale_Furniture_All,
        For_Sale_Furniture_By_Dealer,
        For_Sale_Furniture_By_Owner,
        For_Sale_Games_Toys,
        For_Sale_Garage_Sales,
        For_Sale_General,
        For_Sale_Household,
        For_Sale_Items_Wanted,
        For_Sale_Jewelry,
        For_Sale_Materials,
        For_Sale_Motorcycles,
        For_Sale_Musical_Instruments,
        For_Sale_Photo_Video,
        For_Sale_Recreational_Vehicles,
        For_Sale_Sporting_Goods,
        For_Sale_Tickets,
        For_Sale_Tools
    }

    #endregion

Note that there are other sections to craigslist, but I really only look for things being sold (although I did manage to find my current job through the site). But basically you just need to enter in the individual site (since they have about 10,000 of them), the category you want to search in, and the search term. In return you're given a RSS Document object. Note that this is a slightly modified version of my RSS helper that can be found in my utility library). The reason it's modified is that Craigslist doesn't do a normal RSS feed. Instead they use RDF with RSS elements... The only main difference, in terms of code, from the version that is currently in the utility library is in the Document class itself. Specifically the constructor:

public Document(string Location)
        {
            try
            {
                XmlDocument Document = new XmlDocument();
                Document.Load(Location);
                foreach (XmlNode Children in Document.ChildNodes)
                {
                    if (Children.Name.Equals("rss", StringComparison.CurrentCultureIgnoreCase))
                    {
                        foreach (XmlNode Child in Children.ChildNodes)
                        {
                            if (Child.Name.Equals("channel", StringComparison.CurrentCultureIgnoreCase))
                            {
                                Channels.Add(new Channel((XmlElement)Child));
                            }
                        }
                    }
                    else if (Children.Name.Equals("rdf:rdf", StringComparison.CurrentCultureIgnoreCase))
                    {
                        List<Item> Items = new List<Item>();
                        foreach (XmlNode Child in Children.ChildNodes)
                        {
                            if (Child.Name.Equals("channel", StringComparison.CurrentCultureIgnoreCase))
                            {
                                Channels.Add(new Channel((XmlElement)Child));
                            }
                            else if (Child.Name.Equals("item", StringComparison.CurrentCultureIgnoreCase))
                            {
                                Items.Add(new Item((XmlElement)Child));
                            }
                        }
                        if (Channels.Count > 0)
                        {
                            Channels[0].Items = Items;
                        }
                    }
                }
            }
            catch { }
        }

I'm not that happy with the way that ends up but it is what it is. Other than that though, It's pretty simple to use. But if you want to use your own RSS parser, it shouldn't be too difficult to write. Especially considering it's not exactly an RSS feed. Anyway, try it out, leave feedback, and happy coding.

kick it on DotNetKicks.com   Shout it
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkListEmail

Posted by: James Craig
Posted on: 10/22/2009 at 10:53 AM
Tags: , , ,
Categories: C#
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Searching Hulu using C#

While working on my current project, I needed to be able to search a couple different media websites and use various APIs that are out there. Most of these websites make doing a simple search rather difficult. At best they use something like OAuth/REST but most of them come up with their own system that's just annoying. Then I started looking at Hulu. Hulu, for those of you that have yet to visit, is one of the greatest sites ever. Well assuming you live in the US anyway. It allows you to stream TV, including shows like Spaced which I loved, and movies (although most of the movies are pretty bad). I use it fairly often but never really paid attention to the search feature. It turns out that Hulu didn't really go with a complicated setup for outside apps to search their site. Instead they use RSS feeds and can be searched with a simple URL.

    /// <summary>
    /// Hulu service helper class
    /// </summary>
    public static class Hulu
    {
        #region Public Static Functions

        /// <summary>
        /// Searches hulu for items
        /// </summary>
        /// <param name="SearchTerm">Search term</param>
        /// <returns>An RSS document holding the information</returns>
        public static Document SearchHulu(string SearchTerm)
        {
            Document Results = new Document("http://www.hulu.com/feed/search?query=" + HttpUtility.UrlEncode(SearchTerm) + "&sort_by=relevance");
            return Results;
        }

        #endregion
    }

The url is simply http://www.hulu.com/feed/search?query=YOURQUERY&sort_by=relevance. That's it. There are other options that you can send in but that URL will give you back your search results in an RSS file. You'll notice that the code above is using a Document class. This is from my RSS feed reader code that can be found either here or in my utility library. However the feeds are pretty much a basic RSS feed, so you can parse it however you would like. There are a couple things to note though. First is that it's using a couple added formats in the feed. For example there is an Atom field giving a link to the search service, some yahoo/media tags, etc. So if you use my code to parse the RSS feed, it's going to ignore that content (unless you're viewing this after Thursday as I should have updated my utility library by then). But for the most part they're not required but if you would like, just parse those fields yourself.

Anyway, that's it to searching Hulu. It's rather simple and straightforward. So give it a try, leave feedback, and happy coding.

kick it on DotNetKicks.com   Shout it
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkListEmail

Posted by: James Craig
Posted on: 9/29/2009 at 10:24 AM
Tags: , ,
Categories: C#
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

RSS Reader

I talked in the past about creating an RSS feed and how it's a fairly easy thing to do. One thing that I didn't really cover (and it might be something that you would like to do), is reading in an RSS feed. Perhaps you want to create an aggregator or blog roll or something. In my case, I was simply creating a better code base for creating feeds and decided I might as well be able to read them as well.

RSS.zip (5.07 kb)

Anyway, the code above can read in a well formatted RSS feed (that adheres to the 2.0 specification anyway). Allows for an easy way to combine feeds, output them, etc. All you need to do is create a Document object pointing to the location of the feed to read it in and use toString to output it back out. Anyway, download the code, leave feedback, and happy coding.

kick it on DotNetKicks.com   Shout it
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkListEmail

Posted by: James Craig
Posted on: 7/31/2008 at 12:57 PM
Tags:
Categories: C# | Web Design
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed