Example #1
0
def update_page(db_key, path):
    """Updates a page in the database

    The page is downloaded, parsed and then stored in the database as a
    JSON string. This string is also returned by the function.

    :db_key: a redis string of the key where the stories page will be stored
    :path: the HN URL path where the page will be downloaded from

    Raises NotFound when the page could not be found on the remote
    server or ServerError in case the server returned a response that we
    could not understand. (It's still the server's fault because it
    doesn't even have sensible status codes)

    """
    res = hn_get(path)
    if db_key.startswith('/pages'):
        result = parse_stories(res.text)
    elif db_key.startswith('/comments'):
        result = parse_comments(res.text)
    else:
        raise TypeError('Wrong DB Key.')

    return json.dumps(result)
Example #2
0
 def setUpClass(self):
     with open(FRONT_PAGE) as f:
         self.stories, self.more = parsers.parse_stories(f.read()).values()
Example #3
0
 def setUpClass(self):
     with open(FRONT_PAGE) as f:
         self.stories, self.more = parsers.parse_stories(f.read()).values()