Commits

William B  committed 91d95c8

added open browser support and fixed dumb bug I introduced tonight

  • Participants
  • Parent commits b5358f6

Comments (0)

Files changed (1)

File gumtree_rss.py

 from xml.dom import minidom
 import getopt
 import sys
+import webbrowser
 
 # 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.
 
+# version history
+# 1.0 - initial release created during a lunch break
+# 1.1 - added the option to open a single entry in a browser and fix a big bug I introduced renaming things.
+
+
 def usage():
     print """\
 *---------------------------------------*
 *                                       *
 * Author: william b                     *
 * Email: toadwarrior@gmail.com          *
-* Version: 1.0                          *
+* Version: 1.1                          *
 *---------------------------------------*\n
 
 -l or --list = List all item titles
                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
+-o or --open = open an entry in the browser
+               -o 1 = open the 1st entry in the default feed
+               -o 2918 1 = open the 1st entry in feed 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):
+def gumtree_parse(category=None, nos=None, action='view'):
+    """
+    category = the feed's id that you want to view
+    nos = the entry numbers you want to view
+    action = what you would like to do. the default is view
+             view = just output the results to the screen
+             open = open result in a browser window
+    """
+
     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."
+        error =  "Either Gumtree is down or you provided a bar category Id. Please double check your input."
 
     if error != None:
         print error
     else:
-        if nos == None:
+        if action == 'view':
+            if nos == None:
+                for index, node in enumerate(feed.getElementsByTagName('item')):
+                    print str(index) + ": " + node.childNodes[0].childNodes[0].data
+            else:
+                select_array = nos.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'
+        elif action == 'open':
             for index, node in enumerate(feed.getElementsByTagName('item')):
-                print str(index) + ": " + node.childNodes[0].childNodes[0].data
+                if str(index) == nos:
+                    webbrowser.open_new_tab(node.childNodes[3].childNodes[0].data)
+                    break
         else:
-            select_array = num.split(',')
+            print "Something has gone wrong. Double check your input."
 
-            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"])
+        opts, args = getopt.getopt(sys.argv[1:], "hg:lo", ["help", "get", "list", "open"])
     except getopt.GetoptError, err:
             print str(err)
             usage()
         elif o in ("-g", "--get"):
             option = True
             if len(sys.argv) == 4:
-                gumtree_parse(category=sys.argv[2], num=sys.argv[3])
+                #specifying a category and entry numbers
+                gumtree_parse(category=sys.argv[2], nos=sys.argv[3])
             elif len(sys.argv) == 3:
-                gumtree_parse(num=sys.argv[2])
+                #grabbing specific entries from the default feed
+                gumtree_parse(nos=sys.argv[2])
             else:
                 usage()
         elif o in ("-l", "--list"):
             option = True
             if len(sys.argv) == 3:
+                #list all entries to a specific feed
                 gumtree_parse(category=sys.argv[2])
             else:
+                #list all entries to the default feed
                 gumtree_parse()
+        elif o in ("-o", "--open"):
+            option = True
+            if len(sys.argv) == 4:
+                #providing a feed id and entry number
+                gumtree_parse(category=sys.argv[2], nos=sys.argv[3], action='open')
+            elif len(sys.argv) == 3:
+                #default feed so assuming there is just a entry number
+                gumtree_parse(nos=sys.argv[2], action='open')
+            else:
+                usage()
         else:
             assert False, "unhandled option"