예제 #1
0
def book_page(book_id):
    searchform = SearchForm()
    db = current_app.config["db"]
    book, author_id = db.get_book(book_id)
    reviews, users = db.get_reviews(book_id)
    if book is None:
        abort(404)
    form = ReviewForm()
    if form.validate_on_submit():
        score = form.data["score"]
        comment = form.data["comment"]
        author = db.get_user_id(current_user.username)
        review = Review(author=author,
                        book=book_id,
                        score=score,
                        comment=comment)
        review_id = db.add_review(review)
        review.id = review_id
        return redirect(url_for("book_page", book_id=book_id))
    return render_template("book.html",
                           book=book,
                           author_id=author_id,
                           form=form,
                           reviews=reviews,
                           users=users,
                           searchform=searchform)
    print 'page: ' + str(cur)
    data_dir = 'data/' + str(cur) + '/'
    paths = os.listdir(data_dir)

    paths = [relpath for relpath in paths if \
        relpath.find('index.html') == -1 and \
        relpath != "urls" and relpath.find('.arff') == -1]

    for relpath in paths:
        path = data_dir + relpath
        print path
        soup = BeautifulSoup(open(path))
        try:
            meta = soup.find('ul', {"class": "review-meta"})
            rev = Review()
            rev.id = int(re.search('\d+', relpath).group(0))
            rev.artist = meta.find('h1').find('a').get_text()
            rev.album = meta.find('h2').get_text()
            str_date = meta.find('span', {"class": "pub-date"}).get_text()
            rev.date = datetime.strptime(str_date, '%B %d, %Y')
            rev.score = float(meta.find('span', {"class": "score"}).get_text())
            rev.text = soup.find('div', {"class": "editorial"}).get_text()

            db.reviews.insert(rev.__dict__)
            count = count + 1
            print count
            #out.write(rev.arff_row())

            #print str(rev)
        except Exception as e:
            print e
    print 'page: ' + str(cur)
    data_dir = 'data/' + str(cur) + '/'
    paths = os.listdir(data_dir)

    paths = [relpath for relpath in paths if \
        relpath.find('index.html') == -1 and \
        relpath != "urls" and relpath.find('.arff') == -1]

    for relpath in paths:
        path = data_dir + relpath
        print path
        soup = BeautifulSoup(open(path))
        try:
            meta = soup.find('ul', {"class":"review-meta"})
            rev = Review()
            rev.id = int(re.search('\d+', relpath).group(0))
            rev.artist = meta.find('h1').find('a').get_text()
            rev.album = meta.find('h2').get_text()
            str_date = meta.find('span', {"class":"pub-date"}).get_text()
            rev.date = datetime.strptime(str_date, '%B %d, %Y')
            rev.score = float(meta.find('span', {"class":"score"}).get_text())
            rev.text = soup.find('div', {"class":"editorial"}).get_text()

            db.reviews.insert(rev.__dict__)
            count = count + 1
            print count 
            #out.write(rev.arff_row())

            #print str(rev)
        except Exception as e:
            print e