Beispiel #1
0
def create_dummy_books():
    books = []
    for x in range(N_BOOKS):
        title = "".join(random.choices(string.ascii_lowercase, k=10))
        title = title.capitalize()
        author = "".join(random.choices(string.ascii_lowercase, k=5))
        description = "".join(
            random.choices(string.ascii_lowercase + "    ", k=80))
        new_book = model.Book(title=title,
                              author=author,
                              description=description)
        books.append(new_book)

        book_1 = model.Book(title="The idiot",
                            author="Fjodor Dostojewski",
                            description="Lorem ipsum")
        books.append(book_1)
        book_2 = model.Book(
            title="It",
            author="Stephen King",
            description="Lorem ipsumsed diam nonumy eirmod tempor invidunt")
        books.append(book_2)
        book_3 = model.Book(title="Wuthering Heights",
                            author="Emily Bronte",
                            description="Lorem ipsum dolor sit amet, conse")
        books.append(book_3)

        for book in books:
            if not db.query(model.Book).filter_by(title=book.title).first():
                db.add(book)

    db.commit()
Beispiel #2
0
def get_populated_book(isbn):
    new_book = model.Book()
    new_book.owner = model.Person.by_email(
        users.get_current_user().email()).key
    new_book.isbn = isbn

    r = fetch_details_from_isbn(isbn)
    if r.ok and r.json().has_key('items'):
        book = r.json()['items'][0]['volumeInfo']
        logging.warn(book)
        # Note that the description is truncated at 1500 chars.
        new_book.populate(
            title=book.get('title', ''),
            author=book.get('authors', []),
            description=book.get('description', ''),
            isbn=isbn,
        )
        # If a subtitle is present, include it in the title.
        # This helps with disambiguation.
        if book.has_key('subtitle') and book['subtitle']:
            new_book.title = "%s: %s" % (book['title'], book['subtitle'])
    else:
        logging.error("Failed to fetch book data: %s" % r.content)
        flash("Failed to fetch book data for isbn: %s" % isbn)
        new_book.description = BOOK_DATA_FAILURE_MSG
    return new_book
Beispiel #3
0
def analy_url_page(url):
    """
    分析url对应的网址,包括如下几个方面
    1. 提取当前url所有书籍的链接
    2. 判断当前url是否有下一页,如果有, 继续步骤3
                            如果没有,继续从新的分类开始爬取,
                                    如果新的分类已经爬取完成,则爬取完成
    3. 获取当前页面所有书籍,并依次为每个书籍创建对象(进行初始化,爬取书籍的名称、作者名、书籍详情页、书籍百度网盘地址、书籍百度网盘提取码)
    4. 继续步骤2
    :param url: 网页的url地址
    :return: None
    """
    while url:
        data = get_url_content(url)
        url_links_page = get_url_book(url, data)
        url_next_page = has_next_page(url, data)
        books_name = get_url_books_name(url, data)
        for i in range(len(books_name)):
            book_name = books_name[i]
            book_author = get_book_author(url, data)[i]
            book_info_url = url_links_page[i]
            book_baidu_url = get_book_baidu_neturl(url_links_page[i])
            book_baidu_password = get_book_baidu_password(url_links_page[i])
            book = model.Book(book_name, book_info_url, book_author, book_baidu_url, book_baidu_password)
            print(book)
        if url_next_page:
            url = url_next_page
        else:
            break
Beispiel #4
0
def create_dummy_books():
    books = []
    for x in range(N_BOOKS):
        title = "".join(random.choices(string.ascii_lowercase, k=10))
        title = title.capitalize()
        author = "".join(random.choices(string.ascii_lowercase + " ", k=20))
        genre = "".join(random.choices(string.ascii_lowercase, k=5))
        books.append(model.Book(title=title, author=author, genre=genre))
    db.add_all(books)
    db.commit()
Beispiel #5
0
def create_dummy_book():
    books = []
    for x in range(N_BOOKS):
        name = "".join(random.choices(string.ascii_lowercase, k=5))
        name = name.capitalize()
        description = "".join(
            random.choices(string.ascii_lowercase + " ", k=10))
        new_book = model.Book(name=name, description=description)
        books.append(new_book)
    db.add_all(books)
    db.commit()
Beispiel #6
0
def delete_book(key):
    book_key = ndb.Key(urlsafe=key)
    for loan in book_key.get().history():
        loan.key.delete()
    book_key.get().delete_search_index()
    book_key.delete()
    flash("Book Deleted.")
    return render_template(
        'book_edit.html',
        book=model.Book(),
    )
Beispiel #7
0
 def on_quit(self, action, param):
     db = model.get_db(self.config['db_path'])
     for e in self.books.data:
         b = model.Book(isbn=e[0],
                        title=e[1],
                        author=e[2],
                        own=e[3],
                        want=e[4],
                        read=e[5],
                        location=e[6])
         db.merge(b)
     db.commit()
     self.quit()
Beispiel #8
0
def parseBible(session, url):
    tree = load_dom(session, url)
    titles = tree.xpath('//div[@class="edition-content"]//a[@href]/text()')
    urls = tree.xpath('//div[@class="edition-content"]//a/@href')
    bible = model.Bible(u'Magyar Revideált Új fordítás', url)  # type: Bible
    for i in range(0, len(titles)):
        book = model.Book(titles[i], url + urls[i])
        bible.addBook(book)

    for b in bible.books:
        parseBook(session, b)

    return bible
Beispiel #9
0
def testParseBook(session):
    bible = model.Bible('Revidealt uj forditas', base_url)
    book = model.Book('Mozes elso konyve',
                      'https://abibliamindenkie.hu//uj/GEN/')
    parseBook(session, book)

    print(book.title + " @ " + book.url)
    for c in book.chapters:
        print(c.url)
        for l in c.summary:
            print('   * ' + l)

    for v in book.chapters[0].verses:
        print(str(v.number) + ": " + v.text)

    bible.addBook(book)
    return bible
def create_dummy_books():
    books = []
    for x in range(N_BOOKS):
        title = "".join(random.choices(string.ascii_lowercase,
                                       k=10)).capitalize()
        subtitle = "".join(random.choices(string.ascii_lowercase,
                                          k=10)).capitalize()
        year = str(random.randint(1950, 2000))
        author = "".join(random.choices(string.ascii_lowercase,
                                        k=10)).capitalize()
        book = model.Book(title=title,
                          subtitle=subtitle,
                          author=author,
                          year=year)
        books.append(book)
    db.add_all(books)
    db.commit()
Beispiel #11
0
def register_books():

    current_request = flask.request

    if current_request.method == "GET":
        return flask.render_template("register-books.html")

    elif current_request.method == "POST":
        #todo: register valid user
        title = current_request.form.get("title")
        author = current_request.form.get("author")
        genre = current_request.form.get("genre")
        title_exist = db.query(model.Book).filter_by(book=books).first()
        if title_exist:
            print("Book already exists")
        else:
            new_book = model.Book(title=title, author=author, genre=genre)
            db.add(new_book)
            db.commit()
            return flask.redirect(flask.url_for("register-books"))
Beispiel #12
0
def add_from_form(isbn=None):
    new_book = model.Book()
    if users.get_current_user():
        new_owner = model.Person.by_email(users.get_current_user().email()).key
    if request.values.has_key('owner'):
        new_owner = model.Person.find_or_create_by_name(
            request.values['owner']).key
    new_book.populate(
        title=request.values.get('title', ''),
        author=request.values.get('author', '').split(','),
        description=request.values.get('description', ''),
        isbn=request.values.get('isbn', isbn),
        artist=request.values.get('artist', '').split(','),
        publisher=request.values.get('publisher', ''),
        owner=new_owner,
    )
    logging.debug("Added book: %s" % new_book)
    new_book.put()
    logging.debug("updating search index.")
    new_book.update_search_index()
    flash("Book Created!")
    return redirect("/book/%s" % new_book.key.urlsafe())
def add_book():
    current_request = flask.request
    if current_request.method == "GET":
        return flask.render_template("books/add_book.html")
    elif current_request.method == "POST":
        title = current_request.form.get("title")
        subtitle = current_request.form.get("sub_title")
        year = current_request.form.get("year")
        author = current_request.form.get("author")
        book_exists = db.query(model.Book).filter(
            and_(title=title, year=year, author=author))
        book_exists = None
        if book_exists:
            print("Book already exists in the Database")
        else:
            new_book = model.Book(title=title,
                                  subtitle=subtitle,
                                  author=author,
                                  year=year)
            db.add(new_book)
            db.commit()
            return flask.redirect(flask.url_for("books"))
Beispiel #14
0
def book():
    url = 'https://dl.dropboxusercontent.com/u/70607137/9%3A13/lds_scriptues_books.json'
    site = urllib2.urlopen(url)
    books = json.load(site)
    for v in books:
        book_db = model.Book(
            book_id=v['book_id'],
            volume_id=v['volume_id'],
            book_title=v['book_title'],
            book_title_long=v['book_title_long'],
            book_title_short=v['book_title_short'],
            book_title_jst=v['book_title_jst'],
            book_subtitle=v['book_subtitle'],
            lds_org=v['lds_org'],
            num_chapters=v['num_chapters'],
            num_verses=v['num_verses'],
        )
        book_db.put()

    string = ""
    book_dbs = util.retrieve_dbs(model.Book.query())
    print '--, ', len(book_dbs)
    return string
Beispiel #15
0
def books_add():
    current_request = flask.request

    if current_request.method == "GET":
        return flask.render_template("books_add.html")

    elif current_request.method == "POST":
        # TODO: add valid book
        title = current_request.form.get('title')
        author = current_request.form.get('author')
        description = current_request.form.get('description')
        book_exists = db.query(model.Book).filter_by(title=title,
                                                     author=author).first()
        if book_exists:
            print("This book already exists")
            return flask.redirect(flask.url_for('books'))
        else:
            new_book = model.Book(title=title,
                                  author=author,
                                  description=description)
            db.add(new_book)
            db.commit()
            return flask.redirect(flask.url_for('books_add'))
Beispiel #16
0

#create two instances of the book class and compare their properties

import model

b1 = model.Book('Coding', 200, 'Martin', 'Wiley')
book_1_name = b1.name
book_1_pages = b1.pages
book_1_authors = b1.authors
book_1_publisher = b1.publisher

b2 = model.Book('Byte', 150, 'Bob', 'Moore')
book_2_name = b2.name
book_2_pages = b2.pages
book_2_authors = b2.authors
book_2_publisher = b2.publisher

add_author_1 = b1.addAuthor('Wayne simon')
print(add_author_1)

print('book name:        ', book_1_name, '       | ',book_2_name)
print('---------------------------------------------')
print('book pages:        ', b1.pages, '         | ', b2.pages)
print('---------------------------------------------')
print('book authors:       ', b1.authors, '     | ', b2.authors)
print('---------------------------------------------')
print('book publisher:     ', b1.publisher, '     | ', b2.publisher)

'''def organize_book(*arg):
    for info in arg:
from sqlalchemy import and_, or_

engine = create_engine(config.DATABASE_URI)

# Global
Session = sessionmaker(bind=engine)


def recreate_database():
    model.Base.metadata.drop_all(engine)
    model.Base.metadata.create_all(engine)


# CREATE A ROW
book = model.Book(title='Deep Learning',
                  author='Ian Goodfellow',
                  pages=775,
                  published=datetime(2016, 11, 18))

recreate_database()
s = Session()
s.add(book)
s.commit()

# QUERYING ROWS/ READ/ SELECT
print(s.query(model.Book).first())

# Close all sessions and recreate the db
s.close_all()
recreate_database()

# Create a new session
Beispiel #18
0
    def load_new_book(self):
        import model

        self.book = model.Book()
        self.book.save()
Beispiel #19
0
def empty_book_form():
    return render_template(
        'book_edit.html',
        book=model.Book(),
    )