Commits

Jakub Zalewski committed 1cac142

arguments passing change: Namespace to dumped kwargs; to make using imported module functions easier.
json dump instead of printing dict

  • Participants
  • Parent commits 611b769

Comments (0)

Files changed (1)

 import argparse, os, pickle, re, requests
 from pyquery import PyQuery as pq
 import pystache
+import json
 
 BASE_PATH = os.path.dirname(__file__)
 COOKIE = os.path.join(BASE_PATH, 'hackernews.cookie')
         # We haven't established a login request.
 
         # If we're using cookies, try to return those instead.
-        if not kwargs['args'].no_cookies:
+        if not kwargs.get('no_cookies'):
 
             # If the cookie doesn't exist, create it.
             try:
         # Build the login POST data and make the login request.
         payload = {
             'fnid': fnid,
-            'u': kwargs['args'].username,
-            'p': kwargs['args'].password,
+            'u': kwargs.get('username'),
+            'p': kwargs.get('password'),
         }
         r = requests.post('https://news.ycombinator.com/y', data=payload)
 
         cookies = kwargs['r'].cookies
 
     # Set the cookie
-    if not kwargs['args'].no_cookies:
+    if not kwargs.get('no_cookies'):
         cookie = open(COOKIE, 'w+')
         pickle.dump(cookies, cookie)
         cookie.close()
 
 def _get_saved_stories(**kwargs):
     """Returns a sorted list of the user's saved stories."""
-
+    #print kwargs
     # Log in to get cookies.
     cookies = _login(**kwargs)
 
     if 'r' not in kwargs:
         # This is the first saved items request.
         # Make the saved items request and set an empty list.
-        kwargs['r'] = requests.get('https://news.ycombinator.com/saved?id=%s' % kwargs['args'].username,
+        kwargs['r'] = requests.get('https://news.ycombinator.com/saved?id=%s' % kwargs.get('username'),
                                    cookies=cookies)
 
         # Check to make sure we have a good response.
             })
 
     # If we're getting all saved stories.
-    if kwargs['args'].all:
+    if kwargs.get('all'):
 
         # Find the 'More' link and load it.
         last = J('a', J('table table tr td.title:last'))
     if 'r' not in kwargs:
         # This is the first comments request.
         # Make the comments request and set an empty list.
-        kwargs['r'] = requests.get('https://news.ycombinator.com/threads?id=%s' % kwargs['args'].username,
+        kwargs['r'] = requests.get('https://news.ycombinator.com/threads?id=%s' % kwargs.get('username'),
                                    cookies=cookies)
 
         # Check to make sure we have a good response.
 
         comment = _sanitize_comment(J, c)
 
-        if kwargs['args'].no_owner and comment['user'] == kwargs['args'].username:
+        if kwargs.get('no_owner') and comment['user'] == kwargs.get('username'):
             continue
 
         # Add the comment to the saved list.
         })
 
     # If we're getting all comments.
-    if kwargs['args'].all:
+    if kwargs.get('all'):
 
         # Find the 'More' link and load it.
         last = J('a', J('table table tr td.title:last'))
     return kwargs['comments']
 
 
-def saved(args):
-    """Returns a formatted list of the logged-in user's saved stories."""
+def saved(**kwargs):
+    """Returns a formatted list of the logged-in user's saved stories."""    
+    stories = _get_saved_stories(**kwargs)
 
-    stories = _get_saved_stories(args=args)
-
-    if args.export == 'json':
-        return stories
-    elif args.export == 'xml':
+    if kwargs.get('export', 'json') == 'json':
+        return json.dumps(stories)
+    elif kwargs.get('export') == 'xml':
         return pystache.render("""<?xml version="1.0" encoding="utf-8"?>
             <feed xmlns="http://www.w3.org/2005/Atom">
                 <title>Saved stories on Hacker News</title>
                 {{/stories}}
             </feed>""", {'stories': stories})
 
-def comments(args):
+def comments(**kwargs):
     """Returns a formatted list of the logged-in user's comments."""
 
-    comments = _get_comments(args=args)
+    comments = _get_comments(**kwargs)
 
     if args.export == 'json':
-        return comments
+        return json.dumps(comments)
     elif args.export == 'xml':
         return pystache.render("""<?xml version="1.0" encoding="utf-8"?>
             <feed xmlns="http://www.w3.org/2005/Atom">
 
     # Args
     args = parser.parse_args()
-    print args.func(args)
+    print args.func(**vars(args))