def update_manually(url): session = Session() # 更新文章 article = utils.db.get_or_create(session, Article, source=url) result = imgs(url) article.content = result['images'] article.title = result['title'] article.image_num = len(result['images']) # 更新记录 update_record = session.query(UpdateInfo).first() total = [] total.extend(update_record.content) if url not in total: total.append(url) update_record.content = total session.add(article) session.add(update_record) try: session.commit() except: session.rollback() raise session.close()
def init_update_info(): session = Session() record = UpdateInfo( update_id=1, content=[], ) session.add(record) session.commit() session.close()
def init_user(): session = Session() user = models.User( user_id=1, username='******', password=str(bcrypt.hashpw('admin'.encode('utf-8'), bcrypt.gensalt()), 'utf-8'), avatar='http://img.l.jifangcheng.com/Katarina.png', introduction='hello', ) session.add(user) session.commit() session.close()
def article(page): ''' page 从 1 开始 ''' page = int(page) session = Session() articles = session.query(Article).order_by( Article.create_time.desc())[(page - 1) * config.ARTICLE_PAGE_NUMBER:page * config.ARTICLE_PAGE_NUMBER] articles = [article.json for article in articles] session.commit() session.close() return articles
def jike(): ''' 即刻:知乎热门钓鱼帖 ''' JK_API = ('https://app.jike.ruguoapp.com/1.0/messages/showDetail?' 'topicId={topic_id}').format(topic_id='57281cf75f0ba71200ffde92') results = requests.get(JK_API, ) if results.status_code != 200: logging.error('[fish] jike api url failed, status_code: {}'.format( results.status_code)) return results = results.json() messages = results['messages'] messages_url = [] for message in messages: link_url = message['linkUrl'] if link_url.startswith('jike://'): link_info = message['personalUpdate']['linkInfo'] link_url = link_info.get('link') or link_info.get('linkUrl') # 把 URL 先处理了,返回整个问题的 URL(即刻返回的 URL 有些是一个具体答案的 URL) question_url = re.match(r'.*?(www.zhihu.com/question/\d+)', link_url).group(1) question_url = 'https://{}'.format(question_url) messages_url.append(question_url) session = Session() update_record = session.query(UpdateInfo).first() already_existing = update_record.content # 用这次的 URL 列表减去已经存在的 URL 列表,得到这次多出来的 URL need_update = list(set(messages_url) - set(already_existing)) for url in need_update: article = utils.db.get_instance(session, Record, source=url) result = imgs(url) article.content = result['images'] article.title = result['title'] article.image_num = len(result['images']) article.update_time = datetime.datetime.now() session.add(article) total = [] total.extend(already_existing) total.extend(need_update) update_record.content = total session.add(update_record) session.commit() session.close() return need_update
def get(self, article_id): session = Session() article = session.query(ArticleModel).filter( ArticleModel.article_id == article_id, ).first() if not article: return utils.common.raise_error(request=self, status_code=404) # 兼容以前的数据 if not article.views: article.views = 0 # 更新浏览次数 article.views += 1 session.add(article) session.commit() article_data = article.json session.close() data = {} data['article'] = article_data self.render('article.html', data=data)
def init_article(): session = Session() article = models.Article( user_id=1, title='hello', introduction='<p>hello</p>', markdown_content=''' # hello > hello hello ''', compiled_content=''' <h1 id="hello">hello</h1> <blockquote> <p>hello</p> </blockquote> <p>hello</p> ''', ) session.add(article) session.commit() session.close()
def jike(): ''' 即刻:知乎热门钓鱼帖 ''' JK_API = ( 'https://app.jike.ruguoapp.com/1.0/topics/showDetail?' 'topicId={topic_id}' ).format(topic_id='57281cf75f0ba71200ffde92') results = requests.get( JK_API, ) if results.status_code != 200: return results = results.json() messages = results['messages'] messages_url = [message['linkUrl'] for message in messages] session = Session() update_record = session.query(UpdateInfo).first() already_existing = update_record.content # 用这次的 URL 列表减去已经存在的 URL 列表,得到这次多出来的 URL need_update = list(set(messages_url) - set(already_existing)) for url in need_update: article = utils.db.get_or_create(session, Article, source=url) result = imgs(url) article.content = result['images'] article.title = result['title'] article.image_num = len(result['images']) session.add(article) total = [] total.extend(already_existing) total.extend(need_update) update_record.content = total session.add(update_record) session.commit() session.close() return need_update