# Sample Mahalo Python Class (v1)

6 September 2007 · Ray Slakinski

I started working on a Python class to support working with Mahalo data, this is by far not complete but I thought i'd get this out there to help inspire some people :)

>     1 class mhoPageOPML(object):
>     2     import xml.dom.minidom, urllib2
>     3     from xml.dom.minidom import Node
>     4
>     5     def __init__(self, url):
>     6         self.url = url
>     7
>     8         f = self.urllib2.urlopen('%s?action=opml' % url)
>     9         self.opml = f.read()
>    10
>    11     def getTop7(self):
>    12         top7 = self.getSectionLinks('The Mahalo Top 7')
>    13
>    14         return top7
>    15
>    16
>    17     def getSectionLinks(self, section):
>    18         links = []
>    19         site = {}
>    20
>    21         doc = self.xml.dom.minidom.parseString(self.opml)
>    22
>    23         for node in doc.getElementsByTagName("outline"):
>    24             text = node.getAttribute("text")
>    25             if text == section:
>    26                 for n2 in node.getElementsByTagName('outline'):
>    27                     if n2.getAttribute('type') == 'link':
>    28                         site = {'title' : n2.getAttribute("text"), 'url' : n2.getAttribute('url')}
>    29                         links.append(site)
>    30         return links
>    31
>    32     def sections(self):
>    33         sections = []
>    34
>    35         doc = self.xml.dom.minidom.parseString(self.opml)
>    36
>    37         for node in doc.getElementsByTagName('outline'):
>    38             if node.hasChildNodes() and len(node.parentNode.getAttribute('text')) == 0:
>    39                 sections.append(node.getAttribute('text'))
>    40         return sections

