Ejemplo n.º 1
0
def task_add_book(user_info, url):
  if user_info == None:      
    return 'Error: User is not correct!'

  user = user_info['user']
  
  (book_info, parser) = BookParser.get_parser(url)

  if book_info == None:
    return 'Error: URL is not supported!'
  
  url_type = book_info['url_type']
  
  # 首先加入书页信息,获取必备的数据
  if book_info['cover_url']:
    book_info.update( BookParser.get_data(book_info['cover_url']) )
  
  # 必须有作者,标题,目录页,章节地址前缀
  # 如果没有这些就没法构造 Book 和 Catalog
  if not ( book_info.has_key('author') and 
           book_info.has_key('title') and 
           book_info.has_key('catalog_url') and 
           book_info.has_key('chapter_url_prefix') ):      
    return 'Error: The necessary information is not complete!' + '<br />' + str(book_info)
  author = book_info['author']
  title = book_info['title']
  catalog_url = book_info['catalog_url']
 

  # Book
  book_key_name = Database.generate_book_key_name(author, title)    
  book = Database.Book.get_or_insert(book_key_name)  
    
  # Catalog
  catalog_key_name = catalog_url
  catalog = Database.Catalog.get_or_insert(catalog_key_name, book_ref = book)
  last_check = Database.LastCheck.get_or_insert(catalog_url)
  # 判断是否需要重新获取目录
  if not book_info['last_url'] in catalog.chapter_url_list:    # 包括了catalog 是新生成的,即使last_url是None
    # 获取目录
    book_info.update( BookParser.get_data(catalog_url) )
    catalog.put_info(book_info)     
    last_check.put()
    
  # 用户想要添加的章节
  curr_url = None
  # 具体章节
  if url_type == 'chapter':     
    curr_url = url
  # 书页
  elif url_type == 'cover':       
    if book_info.has_key('last_url'):
      curr_url = book_info['last_url']
  # 目录
  elif url_type == 'catalog':
    curr_index = catalog.find_first_chapter_index()
    if curr_index != None:
      curr_url = catalog.chapter_url_list[curr_index]
   
  bookmark = Database.Bookmark(user_ref = user, book_ref = book, catalog_ref = catalog)
  bookmark.update_info( user_ref = user, book_ref = book, catalog_ref = catalog, curr_url = curr_url)