Example #1
0
def importdoulist():
    '''
    import from doulist
    '''
    response.view='generic.jsonp' if 'callback' in request.vars else 'generic.json'
    url=request.vars.url
    books=butil.parse_doulist(url)
    count_succeed=0
    count_failed=0
    count_existed=0
    
    for book in books:
        douban_id,douban_apiurl=butil.getid_from_url(book)
        if dalutil.get_book(douban_id):
            count_existed+=1
        else:
            try:
                jsondata=urllib2.urlopen(douban_apiurl).read()
            except urllib2.HTTPError as e:
                count_failed+=1
                continue
            if dalutil.insert_book_json(douban_id, jsondata):
                count_succeed+=1
            else:
                count_failed+=1
    return dict(ret=0 if count_failed==0 else -1,msg='succeed=%d,failed=%d,exist=%d'%(count_succeed,count_failed,count_existed))
    
Example #2
0
def add():
    '''
    add a book
    '''
    response.view='generic.jsonp' if 'callback' in request.vars else 'generic.json'
    url=request.vars.url
    douban_id,douban_apiurl=butil.getid_from_url(url)
    try:
        jsondata=urllib2.urlopen(douban_apiurl).read()
    except urllib2.HTTPError as e:
        return dict(ret=-1,msg=str(e.code)+' '+e.msg)
    if dalutil.get_book(douban_id):
        return dict(ret=-2,msg='book exists',bookid=douban_id)
    if dalutil.insert_book_json(douban_id, jsondata):
        return dict(ret=0,msg='success',bookid=douban_id)
    else:
        return dict(ret=-1,msg='unknown error')