Ejemplo n.º 1
0
def upd_all_book(book_parser):
    """更新所有小说信息"""
    #global g_books
    #bt = time.time()
    for l in book_parser.book_list:
        l['Alias'] = l['Name'] + '_' + l['Author']
        l['Alias_Host'] = l['Alias'] + '_' + book_parser.host_name
        l['Alias_Author'] = l['Author'] + '_' + book_parser.host_name
        l['is_new_book'] = config.Yes
        l['is_new_bookinfo'] = config.Yes
        l['is_new_contentinfo'] = config.Yes
        l['is_new_author'] = config.Yes
        for b in config.g_books:
            if (l['Alias'].decode(config.SYS_ENCODING)==b['Alias']):
                l['book_id'] = b['id']
                l['is_new_book'] = config.No
                break

        if (l['is_new_book']):
            for a in config.g_authors:
                if (l['Alias_Author'].decode(config.SYS_ENCODING)==a['Alias']):
                    #print l['Author'].decode(config.SYS_ENCODING), a['Name']
                    l['author_id'] = a['id']
                    l['is_new_author'] = config.No
                    break
        for bi in config.g_bookinfos:
            if (l['Alias_Host'].decode(config.SYS_ENCODING)==bi['Alias']):
                l['bookinfo_id'] = bi['id']
                l['is_new_bookinfo'] = config.No
                break
        #if (l.has_key('bookinfo_id')) and (config.g_contentinfos.has_key(l['bookinfo_id'])):
        try:
            if (config.g_contentinfos[l['bookinfo_id']] == l['Content_Url']):
                l['is_new_contentinfo'] = config.No
            #else:
                #config.g_contentinfos[l['bookinfo_id']] = l['Content_Url']
        except KeyError:
            pass

    #print "step 1 use time: %f" % (time.time()-bt)
    #bt = time.time()
    k = 0
    for b in book_parser.book_list:
        if (book_parser.is_origin):
          if (b.has_key('is_new_book') and (b['is_new_book'])):
            if (b.has_key('is_new_author') and (b['is_new_author'])):
                author = Author()
                author.Name = b['Author'].decode(config.SYS_ENCODING)
                author.Alias = b['Alias_Author'].decode(config.SYS_ENCODING)
                author.save()
                b['author_id'] = author.id
                config.g_authors.append(model_to_dict(author))
            book = Book()
            book.Name = b['Name'].decode(config.SYS_ENCODING)
            book.Alias = b['Alias'].decode(config.SYS_ENCODING)
            book.author_id = b['author_id']
            book.save()
            config.g_books.append(model_to_dict(book))
            b['book_id'] = book.id
            b['is_new_book'] = config.No
            b['is_new_author'] = config.No
        if (b.has_key('is_new_bookinfo') and b.has_key('is_new_book')):
          if ((b['is_new_bookinfo']) and (not b['is_new_book'])):
            bookinfo = BookInfo()
            bookinfo.book_id = b['book_id']
            bookinfo.HostName = book_parser.host_name 
            bookinfo.BookUrl = b['Book_Url']
            bookinfo.Alias = b['Alias_Host'].decode(config.SYS_ENCODING)
            bookinfo.LastContent = b['Content'].decode(config.SYS_ENCODING)
            bookinfo.ContentUrl = b['Content_Url']
            bookinfo.LastUpdated = time.time()
            bookinfo.save()
            config.g_bookinfos.append(model_to_dict(bookinfo))
            b['bookinfo_id'] = bookinfo.id
            b['is_new_bookinfo'] = config.No
          """
          else:
            bookinfo = BookInfo.objects.get(id=b['bookinfo_id'])
            bookinfo.LastContent = b['Content'].decode(config.SYS_ENCODING)
            bookinfo.ContentUrl = b['Content_Url']
            bookinfo.LastUpdated = time.strftime('%Y-%m-%d %H:%M',
                                                 time.localtime())
            bookinfo.save()
          """
        if (b.has_key('is_new_contentinfo') and b.has_key('is_new_book')):
          if ((b['is_new_contentinfo']) and (not b['is_new_book'])):
            k = k + 1
            cinfo = ContentInfo()
            cinfo.bookinfo_id = b['bookinfo_id']
            cinfo.LastContent = b['Content'].decode(config.SYS_ENCODING)
            cinfo.ContentUrl = b['Content_Url']
            cinfo.LastUpdated = time.strftime('%Y-%m-%d %H:%M',
                                                 time.localtime())
            cinfo.save()
            config.g_contentinfos[b['bookinfo_id']] = b['Content_Url']
            b['is_new_contentinfo'] = config.No