def _download_items(db, last_id):
    """
    Download items from the aleph and store them in `db`. Start from `last_id`
    if specified.

    Args:
        db (obj): Dictionary-like object used as DB.
        last_id (int): Start from this id.
    """
    MAX_RETRY = 20  # how many times to try till decision that this is an end
    MAX_DOC_ID = 10000000  # this is used for download iterator

    not_found_cnt = 0  # circuit breaker
    for doc_id in xrange(last_id, MAX_DOC_ID):
        doc_id += 1
        print "Downloading %d.." % (doc_id)

        if not_found_cnt >= MAX_RETRY:
            print "It looks like this is an end:", doc_id - MAX_RETRY
            break

        try:
            record = _download(doc_id)
        except (DocumentNotFoundException, InvalidAlephBaseException):
            print "\tnot found, skipping"
            not_found_cnt += 1
            continue

        not_found_cnt = 0
        db["item_%d" % doc_id] = record
        db["last_id"] = doc_id - MAX_RETRY if doc_id > MAX_RETRY else 1

        if doc_id % 100 == 0:
            db.commit()
Download items from the aleph and store them in `db`. Start from `last_id`
    if specified.

    Args:
        db (obj): Dictionary-like object used as DB.
        last_id (int): Start from this id.