- #!/usr/bin/python
- """
- GivesMeHope API v1.0a
- A Python implementation of the givesmehope.com API, which can be found here: http://www.givesmehope.com/apidoc.php
- Released under the GPL (GNU Public License) - http://www.gnu.org/licenses/gpl-3.0.txt
- Written by Tim Davies (a.k.a. Dotty) - Nothing to do with GivesMeHope - I'm just a random Python coder :)
- Email/Bug reports to: timdavies3@gmail.com
- Dedicated to Becca, who is awesome in every single way. <3
- This aims to be a simple module for writing scripts that tie-in with GivesMeHope.com.
- Example of use:
- import givesmehope
- api = givesmehope.API()
- gmh = gmh.random_gmh()
- item = gmh.getnextitem()
- print item.title()+"\\n"+item.text()
- This would fetch a random 'GivesMeHope' from the website and display it, along with the title.
- A simple way to iterate over a number of items is like so:
- item = gmh.getnextitem()
- while item != None:
- print item.text()+"\\n\\n" // or whatever you want to do.
- item = gmh.getnextitem()
- """
- __author__ = "Tim Davies"
- __license__ = "GPL"
- __version__ = "1.0a"
- __maintainer__ = "Tim Davies"
- __email__ = "timdavies3@gmail.com"
- __status__ = "First Release, Incomplete but working"
- from xml.dom import minidom
- import urllib2
- class Item:
- """A class to hold a particular GivesMeHope and associated data, for example, title, author, comments, date, etc."""
- def __init__(self, xml):
- """Initialise and parse XML."""
- self._xml = xml
- self._item_id = self._xml.attributes['id'].nodeValue
- self._author = self._xml.getElementsByTagName("author")[0].firstChild.nodeValue
- self._category = self._xml.getElementsByTagName("category")[0].firstChild.nodeValue
- self._yes = self._xml.getElementsByTagName("yes")[0].firstChild.nodeValue
- self._no = self._xml.getElementsByTagName("no")[0].firstChild.nodeValue
- self._numcomments = self._xml.getElementsByTagName("comments")[0].firstChild.nodeValue
- self._text = self._xml.getElementsByTagName("text")[0].firstChild.nodeValue
- self._commentsflag = self._xml.getElementsByTagName("comments_flag")[0].firstChild.nodeValue
- self._date = self._xml.getElementsByTagName("date")[0].firstChild.nodeValue
- def __str__(self):
- return self._text
- def id(self):
- """Get item ID."""
- return self._item_id
- def author(self):
- return self._author
- def category(self):
- return self._category
- def date(self):
- return self._date
- def yes(self):
- return self._yes
- def no(self):
- return self._no
- def numcomments(self):
- return self._numcomments
- def text(self):
- return self._text.replace(""", "'")
- def commentsflag(self):
- return self._commentsflag
- class GMH:
- """The class provided by a request. Holds the XML for the entire page, not just the individual bits of text, comments, etc.
- You should use GMH.getnextitem() to retrieve the class (of type Item) for an individual GMH (including comments, etc)."""
- def __init__(self, rawdata):
- """Initialise and parse XML."""
- self.xmldoc = minidom.parseString(rawdata)
- self.current_item = 0
- self.language = str(self.xmldoc.getElementsByTagName("language")[0].firstChild.nodeValue)
- self.pubdate = str(self.xmldoc.getElementsByTagName("pubdate")[0].firstChild.nodeValue)
- self.errorcode = str(self.xmldoc.getElementsByTagName("code")[0].firstChild.nodeValue)
- if self.geterrorcode == 0:
- self.errors = None
- else:
- errors = self.xmldoc.getElementsByTagName("errors")
- errors = self.xmldoc.getElementsByTagName("error")
- foo = []
- for i in range(0, len(errors)):
- foo.append(str(errors[i].firstChild.nodeValue))
- self.errors = foo
- self.activekey = str(self.xmldoc.getElementsByTagName("active_key")[0].firstChild.nodeValue)
- self.items = []
- for i in range(0, self.getnumitems()):
- self.items.append(Item(self.xmldoc.getElementsByTagName("item")[i]))
- def __str__(self):
- """Returns the raw XML."""
- return self.xmldoc.toxml()
- def getlanguage(self):
- """Returns the value of the language tags"""
- return self.language
- def getpubdate(self):
- """Returns the date on which the item was published."""
- return self.pubdate
- def geterrorcode(self):
- """Returns the value of the request. 0 means there was a massive error."""
- return self.errorcode
- def geterrors(self):
- """Returns list of errors. There always seems to be an API key problem, not sure why."""
- return self.errors
- def getactivekey(self):
- """No idea what this is for, but was part of the API so added in."""
- return self.activekey
- def getnumitems(self):
- """Return number of 'items', i.e. GMHs."""
- return self.xmldoc.getElementsByTagName("item").length
- def getnextitem(self):
- """Gets the next 'item', i.e. GMH. Returns an 'item' class."""
- try:
- return_value = self.items[self.current_item]
- except:
- return None
- self.current_item += 1
- return return_value
- def getnumcomments(self):
- """Return number of comments."""
- return self.xmldoc.getElementsByTagName("comments").length
- class API:
- """Class to hold the API functions - the first class to be used. For example, API.random_gmh() returns a random GMH (of class GMH).
- Also to be used for logging in, posting new GMHs etc."""
- def random_gmh(self):
- """Uses the API to choose a random GMH. (GET /view/random)"""
- return GMH(urllib2.urlopen("http://api.givesmehope.com/view/random").read())
- def get_top(self, page=1):
- """Uses the API to return the top 15 GMHs. (GET /view/top)"""
- return GMH(urllib2.urlopen("http://api.givesmehope.com/view/top/"+str(page)).read())
