def get_news(): now = datetime.now() strnow = now.strftime('%Y-%m-%d %H:%M:%S') print(strnow) # First next run time ''' period = timedelta(days=day, hours=hour, minutes=min, seconds=second) next_time = now + period strnext_time = next_time.strftime('%Y-%m-%d %H:%M:%S') print(strnext_time) ''' pasttime = now - timedelta(days=1) paststr = pasttime.strftime('%Y-%m-%d %H:%M:%S') print(paststr) df = pro.news(src='sina', start_date=strnow, end_date=paststr) print(df) for index, row in df.iterrows(): news = News() news.timestamp = index news.title = row['title'] news.body = row['content'] news.author = 'default' db.session.add(news) db.session.commit() newss = News.query.all() for n in newss: print(n.id, n.body)
def news(id): post = News.query.filter_by(id=id).first() form = NewsForm() if form.submit_new.data and form.validate(): post = News(content=form.content.data, timestamp=datetime.now()) db.session.add(post) db.session.commit() flash('News item posted!', 'success') return redirect(url_for('main.news')) if form.submit_edit.data and form.validate() and post is not None: post.content = form.content.data post.timestamp = datetime.now() db.session.add(post) db.session.commit() flash('News item edited') return redirect(url_for('main.news')) if form.submit_delete.data and post is not None: db.session.delete(post) db.session.commit() flash('News item deleted', 'danger') return redirect(url_for('main.news')) elif request.method == 'GET' and post is not None: form.content.data = post.content page = request.args.get('page', 1, type=int) news = News.query.order_by(News.timestamp.desc()).paginate( page, current_app.config['NEWS_PER_PAGE'], False) next_url = url_for('main.news', page=news.next_num) \ if news.has_next else None prev_url = url_for('main.news', page=news.prev_num) \ if news.has_prev else None return render_template('news.html', form=form, news=news.items, next_url=next_url, prev_url=prev_url, post=post)