             if let error = error {
                 println("Error : \(error)")
             } else {
-                let html = myHTMLString
                 var err : NSError?
-                var parser = HTMLParser(html: html!, error: &err)
+                var parser = HTMLParser(html: myHTMLString!, error: &err)
                 if err != nil {
                     println(err)
                     exit(1)
                 }
                 
-                var allArticle = parser.body
-                if let inputAllArticleNodes = allArticle?.xpath("//div[@class='items-leading']/div[@class='items-leading'] | //div[@class='items-leading']/div[@class='leading-0']") {
-                    for node in inputAllArticleNodes {
-                        var bodyHTML = node.rawContents
-                        var parser = HTMLParser(html: bodyHTML, error: &err)
-                        var bodyNode   = parser.body
-                        var author = String()
-                        var article_url = String()
-                        var article_header = String()
-                        var datetime = String()
-                        
-                        
-                        //Get link and title of the article
-                        if let inputNodes = bodyNode?.xpath("//h2[@class='article-header']") {
-                            for node in inputNodes {
-                                var eachArticle = node.rawContents
-                                var parserArticle = HTMLParser(html: eachArticle, error: &err)
-                                var articleBody   = parserArticle.body
-                                if let inputArticle = articleBody?.findChildTags("a") {
-                                    for node in inputArticle {
-                                        article_header = node.contents
-                                        article_url = node.getAttributeNamed("href")
-                                        article_url = "http://krlx.org/"+article_url
-                                        
-                                    }
-                                }
-                            }
-                        }
-                        
-                        //Get author
-                        if let inputNodes = bodyNode?.xpath("//dl[@class='article-info']/dd") {
-                            for node in inputNodes {
-                                author = node.contents
-                                
-                            }
-                        }
-                        
-                        if let inputNodes = bodyNode?.xpath("//aside/time") {
-                            for node in inputNodes {
-                                datetime = node.getAttributeNamed("datetime")
-                                
-                            }
-                        }
-                        let article = ArticleHeader(authorString: author, titleString: article_header, dateString: datetime, urlString: article_url)
-                            articles.append(article)
-                        
-                    }
-    
-                    
-
-                }
-            }
-        } else {
-            println("Error: \(myURLString) doesn't seem to be a valid URL")
-        }
-        self.appDelegate.loadedArticleHeaders = NSMutableArray(array: articles)
-    }
-
-    //This function scrape KRLX for list of recently heard
-    func scrapeRecentlyHeard() {
-        var songs = [SongHeader]()
-        let myURLString = "http://www.krlx.org/"
-        if let myURL = NSURL(string: myURLString) {
-            var error: NSError?
-            let myHTMLString = String(contentsOfURL: myURL, encoding: NSUTF8StringEncoding, error: &error)
-            if let error = error {
-                println("Error : \(error)")
-            } else {
-                let html = myHTMLString
-                var err : NSError?
-                var parser = HTMLParser(html: html!, error: &err)
-                if err != nil {
-                    println(err)
-                    exit(1)
-                }
+                var htmlBody = parser.body
                 
-                var allArticle = parser.body
-                if let inputAllArticleNodes = allArticle?.xpath("//div[@class='custom']") {
-                    // Recently Heard div is the 3rd div with class custom
+                // Get the div that contain the recently heard items
+                if let inputAllArticleNodes = htmlBody?.xpath("//div[@class='custom']") {
+                    // Recently Heard div is the 3rd div with class "custom"
                     var divRecentHeard = inputAllArticleNodes[2].rawContents
+                    // Parse HTML passage
                     var parser = HTMLParser(html: divRecentHeard, error: &err)
-                    
                     var recentHeardContent   = parser.body
                     
                     var imageURL = String()
                     var title = String()
                     var singer = String()
-
-                    // Get first item
-                    if let inputNodes = recentHeardContent?.xpath("//div[@id='info']/p/a") {
-                        println("have href tag case")
-                        title = inputNodes[0].contents //first para is title, second para is singer
-                        singer = inputNodes[1].contents
-                    }else if let inputNodes = recentHeardContent?.xpath("//div[@id='info']/p") {
-                        println("no a tag case")
-                        title = inputNodes[0].contents //first para is title, second para is singer
-                        singer = inputNodes[1].contents
-                    }
                     
-                    // Get image of first item
-                    if let imageTag = recentHeardContent?.findChildTags("img") {
-                        for node in imageTag {
-                            imageURL = node.getAttributeNamed("src")
-                        }
-                        let song = SongHeader(titleString: title, singerString: singer,urlString: imageURL)
+                    //get first item
+                    var mostRecentSong = getFirstSong_Image(recentHeardContent)
+                    songs.append(mostRecentSong)
+                    
+                    // Get next 4 items
+                    var next4Songs = getNext4Songs(recentHeardContent)
+                    for song in next4Songs {
                         songs.append(song)
-                        
-                    }
-
-                    // Get next 4 items
-                    if let otherRecentlyHeard = recentHeardContent?.xpath("//p[not(ancestor::div[@id='info'])]") {
-                        for node in otherRecentlyHeard {
-                            var titleNsinger = (node.rawContents.componentsSeparatedByString("<b>"))[1]
-                            title = (titleNsinger.componentsSeparatedByString("</b> - "))[0]
-                            singer = (titleNsinger.componentsSeparatedByString("</b> - "))[1].componentsSeparatedByString("</p>")[0]
-                            let song = SongHeader(titleString: title, singerString: singer)
-                            songs.append(song)
-                            
-                        }
                     }
                 }
             }

//  KRLX
//
//  Created by Phuong Dinh and Josie Bealle on 22/05/2015.
//  Copyright (c) 2015 KRLXpert. All rights reserved.
//

class ScrapeAssistant {
    var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
   
    init(){
    }
    
    //does scraping for an article content
    func scrapeArticle(url: String) -> String {
        let myURLString = url
        var article_content = String()
        if let myURL = NSURL(string: myURLString) {
            var error: NSError?
            let myHTMLString = String(contentsOfURL: myURL, encoding: NSUTF8StringEncoding, error: &error)
            if let error = error {
                println("Error : \(error)")
            } else {
                let html = myHTMLString
                var err : NSError?
                var parser = HTMLParser(html: html!, error: &err)
                if err != nil {
                    exit(1)
                }
                var allArticle = parser.body
                if let inputAllArticleNodes = allArticle?.xpath("//div[@class='gk-article']") {
                    for node in inputAllArticleNodes {
                        article_content = node.rawContents
                        
                    }
                }
                else {
                    //println("Error: \(myURLString) doesn't seem to be a valid URL")
                }
            }
        }
        return String(article_content)
    }
    
    // does scraping for article title, date, author
    func scrapeArticleInfo(){
        var articles = [ArticleHeader]()
        let myURLString = "http://www.krlx.org/"
        if let myURL = NSURL(string: myURLString) {
            var error: NSError?

            self.appDelegate.loadedSongHeaders = NSMutableArray(array: songs)
        }
    }
    
    // This function take in articleInfo html (in div), parse and return article URL and title
    func getArticleTitle_URL(articleInfo: HTMLNode?) -> (article_header: String, article_url: String) {
        var article_url = String()
        var article_header = String()
        var err : NSError?
        if let inputNodes = articleInfo?.xpath("//h2[@class='article-header']") {
            for node in inputNodes {
                var eachArticle = node.rawContents
                // parse the h2 tag content
                var parserArticle = HTMLParser(html: eachArticle, error: &err)
                var articleBody   = parserArticle.body
                if let inputArticle = articleBody?.findChildTags("a") {
                    for node in inputArticle {
                        article_header = node.contents //article header
                        article_url = node.getAttributeNamed("href") //article href
                        article_url = "http://krlx.org/"+article_url
                    }
                }
            }
        }
        return (article_header,article_url)
    }
    
    // This function take in articleInfo html (in div), parse and return article author
    func getArticleAuthor(articleInfo: HTMLNode?) -> String {
        var author = String()
        var err : NSError?
        if let inputNodes = articleInfo?.xpath("//dl[@class='article-info']/dd") {
            for node in inputNodes {
                author = node.contents //author
            }
        }
        return author
    }
    
    // This function take in articleInfo html (in div), parse and return Date of publish
    func getArticleDate(articleInfo: HTMLNode?) -> String {
        var datetime = String()
        var err : NSError?
        if let inputNodes = articleInfo?.xpath("//aside/time") {
            for node in inputNodes {
                datetime = node.getAttributeNamed("datetime") //date of publish
                
            }
        }
        return datetime
    }
    
    // This function take in Recently Heard Songs html (in div), parse and return first item (most recent)
    func getFirstSong_Image(recentHeardContent: HTMLNode?)-> SongHeader{
        var imageURL = String()
        var title = String()
        var singer = String()
        // Get first item title and artist
        // 2 cases with different HTML format:
        // With real image, there is a hyper link attached to artist name
        if let inputNodes = recentHeardContent?.xpath("//div[@id='info']/p/a") {
            title = inputNodes[0].contents //first para is title, second para is artist
            singer = inputNodes[1].contents
        }//Without real picture, no hyper link
        else if let inputNodes = recentHeardContent?.xpath("//div[@id='info']/p") {
            title = inputNodes[0].contents
            singer = inputNodes[1].contents
        }
        
        // Get image of first item
        if let imageTag = recentHeardContent?.findChildTags("img") {
            for node in imageTag {
                imageURL = node.getAttributeNamed("src")//image url is in src tag
            }
        }
        let song = SongHeader(titleString: title, singerString: singer,urlString: imageURL)
        return song
    }
    
    // This function take in Recently Heard Songs html (in div), parse and return next 4 items in array
    func getNext4Songs(recentHeardContent: HTMLNode?)-> [SongHeader] {
        var title = String()
        var singer = String()
        var next4Songs = [SongHeader]()
        // the next 4 songs are in paragraph format (but different from the first item that is is not in "info" div
        if let otherRecentlyHeard = recentHeardContent?.xpath("//p[not(ancestor::div[@id='info'])]") {
            for node in otherRecentlyHeard {
                //manually format tricky html string
                var titleNsinger = (node.rawContents.componentsSeparatedByString("<b>"))[1]
                title = (titleNsinger.componentsSeparatedByString("</b> - "))[0]
                singer = (titleNsinger.componentsSeparatedByString("</b> - "))[1].componentsSeparatedByString("</p>")[0]
                let song = SongHeader(titleString: title, singerString: singer)
                next4Songs.append(song)
            }
        }
        return next4Songs
    }
}
