def construct_article(entry, feed): "Safe method to transorm a feedparser entry into an article" now = datetime.utcnow() date = None for date_key in ('published', 'created', 'date'): if entry.get(date_key): try: date = dateutil.parser.parse(entry[date_key])\ .astimezone(timezone.utc) except Exception: pass else: break content = get_article_content(entry) link = get_article_link(entry) content = clean_urls(content, link) return { 'feed_id': feed['id'], 'user_id': feed['user_id'], 'entry_id': extract_id(entry).get('entry_id', None), 'link': link, 'content': content, 'title': html.unescape(entry.get('title', 'No title')), 'readed': False, 'like': False, 'retrieved_date': now, 'date': date or now }
def construct_article(entry, feed): "Safe method to transorm a feedparser entry into an article" now = datetime.utcnow() date = None for date_key in ('published', 'created', 'date'): if entry.get(date_key): try: date = dateutil.parser.parse(entry[date_key])\ .astimezone(timezone.utc) except Exception: pass else: break content = get_article_content(entry) link = get_article_link(entry) content = clean_urls(content, link) return {'feed_id': feed['id'], 'user_id': feed['user_id'], 'entry_id': extract_id(entry).get('entry_id', None), 'link': link, 'content': content, 'title': html.unescape(entry.get('title', 'No title')), 'readed': False, 'like': False, 'retrieved_date': now, 'date': date or now}
def get_article(article_id, parse=False): locale = get_locale() contr = ArticleController(current_user.id) article = contr.get(id=article_id) if not article.readed: article['readed'] = True contr.update({'id': article_id}, {'readed': True}) article['category_id'] = article.category_id or 0 feed = FeedController(current_user.id).get(id=article.feed_id) article['icon_url'] = url_for('icon.icon', url=feed.icon_url) \ if feed.icon_url else None readability_available = bool(current_user.readability_key or conf.PLUGINS_READABILITY_KEY) article['date'] = format_datetime(localize(article.date), locale=locale) article['readability_available'] = readability_available if parse or (not article.readability_parsed and feed.readability_auto_parse and readability_available): try: new_content = readability.parse(article.link, current_user.readability_key or conf.PLUGINS_READABILITY_KEY) except Exception as error: flash("Readability failed with %r" % error, "error") article['readability_parsed'] = False else: article['readability_parsed'] = True article['content'] = clean_urls(new_content, article['link']) new_attr = {'readability_parsed': True, 'content': new_content} contr.update({'id': article['id']}, new_attr) return article
def get_article(article_id, parse=False): locale = get_locale() contr = ArticleController(current_user.id) article = contr.get(id=article_id) if not article.readed: article['readed'] = True contr.update({'id': article_id}, {'readed': True}) article['category_id'] = article.category_id or 0 feed = FeedController(current_user.id).get(id=article.feed_id) article['icon_url'] = url_for('icon.icon', url=feed.icon_url) \ if feed.icon_url else None readability_available = bool(current_user.readability_key or conf.PLUGINS_READABILITY_KEY) article['date'] = format_datetime(localize(article.date), locale=locale) article['readability_available'] = readability_available if parse or (not article.readability_parsed and feed.readability_auto_parse and readability_available): try: new_content = readability.parse( article.link, current_user.readability_key or conf.PLUGINS_READABILITY_KEY) except Exception as error: flash("Readability failed with %r" % error, "error") article['readability_parsed'] = False else: article['readability_parsed'] = True article['content'] = clean_urls(new_content, article['link']) new_attr = {'readability_parsed': True, 'content': new_content} contr.update({'id': article['id']}, new_attr) return article