Commits

William B  committed b5358f6

added script

  • Participants

Comments (0)

Files changed (1)

File gumtree_rss.py

+#!/usr/bin/env python
+
+import urllib
+from xml.dom import minidom
+import getopt
+import sys
+
+# TODO
+# create the ability to save a config so you don't need to remember loads of cat ids and to allow
+# make it so you can search multiple feeds
+# option to open result in a browser
+# option to save contents to a txt file
+# It's not *really* the ultimate RSS CLI parser for gumtree until I do those things.
+
+def usage():
+    print """\
+*---------------------------------------*
+* The Ultimate Gumtree RSS CLI Parser!! *
+*                                       *
+* Author: william b                     *
+* Email: toadwarrior@gmail.com          *
+* Version: 1.0                          *
+*---------------------------------------*\n
+
+-l or --list = List all item titles
+               Examples:
+               -l = list all item titles for the default feed
+               -l 2918 = list all item titles for a specific category\n
+-g or --get  = show more information for specific items in a feed
+               Examples:
+               -g 1,2,3 = show items 1 to 3 for the default feed
+               -g 2918 1,2,3 = show items 1 to 3 for category 2918\n
+-h or --help = You are here\n
+NOTE: Providing no arguments is the the same as -l / --list without providing a category id
+"""
+
+def gumtree_parse(category=None, nos=None):
+    gumtree_feed = 'http://cambridge.gumtree.com/cgi-bin/list_postings.pl?feed=rss'
+    if category != None:
+        gumtree_feed += '&posting_cat=%s' % (str(category))
+
+    feed = None
+    error = None
+    try:
+        feed = minidom.parse(urllib.urlopen(gumtree_feed))
+    except:
+        error =  "Either Gumtree is down or you provided a bar category Id. Please check your input."
+
+    if error != None:
+        print error
+    else:
+        if nos == None:
+            for index, node in enumerate(feed.getElementsByTagName('item')):
+                print str(index) + ": " + node.childNodes[0].childNodes[0].data
+        else:
+            select_array = num.split(',')
+
+            for index, node in enumerate(feed.getElementsByTagName('item')):
+                if str(index) in select_array:
+                    print '*-------------------------------------------------------------*'
+                    print 'ITEM: ' + str(index) + '\n'
+                    print 'TITLE: ' + node.childNodes[0].childNodes[0].data + '\n'
+                    print 'DESCRIPTION: ' + node.childNodes[1].childNodes[0].data + '\n'
+                    print 'LINK: ' + node.childNodes[3].childNodes[0].data
+                    print '*-------------------------------------------------------------*\n'
+
+def main():
+    try:
+        opts, args = getopt.getopt(sys.argv[1:], "hg:l", ["help", "get", "list"])
+    except getopt.GetoptError, err:
+            print str(err)
+            usage()
+            sys.exit(2)
+    
+    option = False
+    for o, a in opts:
+        if o in ("-h", "--help"):
+            option = True
+            usage()
+        elif o in ("-g", "--get"):
+            option = True
+            if len(sys.argv) == 4:
+                gumtree_parse(category=sys.argv[2], num=sys.argv[3])
+            elif len(sys.argv) == 3:
+                gumtree_parse(num=sys.argv[2])
+            else:
+                usage()
+        elif o in ("-l", "--list"):
+            option = True
+            if len(sys.argv) == 3:
+                gumtree_parse(category=sys.argv[2])
+            else:
+                gumtree_parse()
+        else:
+            assert False, "unhandled option"
+
+    if option == False:
+        gumtree_parse()
+
+
+if __name__ == "__main__":
+    main()
+