Ubuntu insights, Programming in groovy, java, et als!

Monday, August 22, 2011

Retrieve the RSS Feed URL of a Webpage

/* A dirty code tweak that can search the meta tags
* of a webpage for its rss feed url.
* If a feed exists for the page,
* the rss feed link portion of the page source
* will be extracted using regex.
*/

def getRSSFeedURLFor(String urlStr){
def url = urlStr.toURL()
def result = []
def str
def lookForStart = '''<link rel="alternate"'''
def lookForEnd = '''>\n'''

url.eachLine {
if(it.contains("RSS"))
result << it + "\n"
}

result.each{
if(it.contains(lookForStart)) {
strtIdx = it.indexOf(lookForStart)
lastIdx = (it.indexOf(lookForEnd)+2)
str = it.substring(strtIdx,lastIdx)
}
}

str = str.replaceAll(/(.*)(href.+)"(.*)"(.*)\n/,'$3')
str = str.trim()
return str
}

def urlStr = "http://www.ashes-phoenix.blogspot.com"
def rssURL = getRSSFeedURLFor(urlStr).toURL()
println rssURL

PS: For most of the standard websites, if an RSS feed exists for the page, it will be specified in one of the metatags of the page source that looks like:

<link rel = "alternate" type=atom/application/rss+xml href=''...>

The logic lies in extracting the link within the href attribute of this tag which is actually the rss feed of the page we are trying to retrieve


Friday, August 19, 2011

Build a Binary Tree & Perform an Inorder traversal



/*
* A simple groovy implementation to create a Binary Tree
* and to perform inorder traversal
*/

class Node{

    Node left, right
    int data
   
    Node(data){
        this.data = data
        left = null
        right = null
    }
}


class BTree{

    static def insert(Node node, val){
      if(val < node.data){
          if(node.left == null){
              node.left = new Node(val)
              println "inserting $val to the left of $node.data"
          }
         
          else insert(node.left, val)
      }
     
      else if(val > node.data){
          if(node.right == null){
              node.right = new Node(val)
              println "inserting $val to the right of $node.data"
          }
         
          else insert(node.right, val)
      }
     
    }
   
    //left, root, right
    static def printInOrder(Node node){
        if(node == null) return
       
        else{
            printInOrder(node.left)
            println node.data
            printInOrder(node.right)
        }
    }
}

//Shift to main if you aren't running this as a groovy script
def root = new Node(25)
BTree.insert(root, 10)
BTree.insert(root, 30)
BTree.insert(root, 24)
BTree.insert(root, 299)
BTree.insert(root, 266)
BTree.insert(root, 121)
BTree.insert(root, 920)

//Inorder traversal of the entered elements
BTree.printInOrder(root)