Example #1
0
 def test_update_page_comments(self):
     RESPONSE_TEXT = '<html>good stories</html>'
     mock_get = mock.Mock(return_value=mock.Mock(text=RESPONSE_TEXT))
     with mock.patch.object(backend.requests, "get", mock_get) as get:
         with mock.patch.object(backend, "parse_comments",
                                mock.Mock(return_value=COMMENTS)) as parse:
             coms_json = backend.update_page("/comments/test_key",
                                             "test_url")
             get.assert_called_with(config.HN + "test_url")
             parse.assert_called_with(RESPONSE_TEXT)
             self.assertEqual(coms_json, COMMENTS_JSON)
Example #2
0
def update(db_key, page):
    try:
        with redis_lock(rdb, '/lock' + db_key):
            if too_old(db_key):
                stories = update_page(db_key, page)
                pipe = rdb.pipeline(True)
                pipe[db_key] = stories
                pipe[db_key + '/updated'] = time.time()
                pipe.execute()
    except LockException:
        pass
Example #3
0
 def test_update_page_comments(self):
     RESPONSE_TEXT = '<html>good stories</html>'
     mock_get = mock.Mock(return_value=mock.Mock(
             text=RESPONSE_TEXT))
     with mock.patch.object(backend.requests, "get", mock_get) as get:
         with mock.patch.object(backend, "parse_comments",
                                mock.Mock(return_value=COMMENTS)) as parse:
             coms_json = backend.update_page("/comments/test_key",
                                             "test_url")
             get.assert_called_with(config.HN + "test_url")
             parse.assert_called_with(RESPONSE_TEXT)
             self.assertEqual(coms_json, COMMENTS_JSON)
Example #4
0
def _get_cache(db_key, page):
    """Retrieves an item from HN with caching

    :db_key: string - the database key where the item is stored
    :page: string - the path after the HN root from where the item
    is downloaded

    Returns a JSON document representing the resource.

    """
    try:
        stories = rdb[db_key]
    except KeyError:
        stories = update_page(db_key, page)
        rdb[db_key] = stories
        rdb[db_key + '/updated'] = time.time()
        return stories

    tasks.update.delay(db_key, page)

    return stories