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

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading