def _clean_story_html(text, *, readability=False): text = story_html_clean(text) if readability: text = story_readability(text) url = 'https://rss.anyant.com/' text = process_story_links(text, url) return text
async def do_fetch_story( ctx: ActorContext, story_id: T.int, url: T.url, use_proxy: T.bool.default(False), num_sub_sentences: T.int.optional, ): LOG.info(f'fetch story#{story_id} url={unquote(url)} begin') options = _get_proxy_options() options.update(allow_private_address=CONFIG.allow_private_address) if DNS_SERVICE.is_resolved_url(url): use_proxy = False async with AsyncFeedReader(**options) as reader: use_proxy = use_proxy and reader.has_rss_proxy url_content = await _fetch_story(reader, story_id, url, use_proxy=use_proxy) if not url_content: return url, content = url_content if len(content) >= _MAX_STORY_HTML_LENGTH: content = story_html_clean(content) if len(content) >= _MAX_STORY_HTML_LENGTH: msg = 'too large story#%s size=%s url=%r' LOG.warning(msg, story_id, len(content), url) content = story_html_to_text(content)[:_MAX_STORY_HTML_LENGTH] await ctx.hope( 'worker_rss.process_story_webpage', dict( story_id=story_id, url=url, text=content, num_sub_sentences=num_sub_sentences, ))
async def do_fetch_story( ctx: ActorContext, story_id: T.int, url: T.url, use_proxy: T.bool.default(False), ): LOG.info(f'fetch story#{story_id} url={unquote(url)} begin') async with AsyncFeedReader(**_get_proxy_options()) as reader: use_proxy = use_proxy and reader.has_rss_proxy status, response = await reader.read(url, use_proxy=use_proxy) if response and response.url: url = str(response.url) LOG.info( f'fetch story#{story_id} url={unquote(url)} status={status} finished') if not (response and status == 200): return if not response.rssant_text: msg = 'story#%s url=%s response text is empty!' LOG.error(msg, story_id, unquote(url)) return content = response.rssant_text if len(content) >= 1024 * 1024: content = story_html_clean(content) if len(content) >= 1024 * 1024: msg = 'too large story#%s size=%s url=%r' LOG.warning(msg, story_id, len(content), url) content = story_html_to_text(content) await ctx.hope('worker_rss.process_story_webpage', dict( story_id=story_id, url=url, text=content, ))
async def do_fetch_story( ctx: ActorContext, story_id: T.int, url: T.url, ): LOG.info(f'fetch story#{story_id} url={unquote(url)} begin') async with AsyncFeedReader() as reader: status, response = await reader.read(url) if response and response.url: url = str(response.url) LOG.info(f'fetch story#{story_id} url={unquote(url)} status={status} finished') if not (response and status == 200): return if not response.rssant_text: LOG.error(f'story#{story_id} url={unquote(url)} response text is empty!') return content = response.rssant_text if len(content) >= 1024 * 1024: content = story_html_clean(content) if len(content) >= 1024 * 1024: LOG.error(f'too large story#{story_id} size={len(content)} url={url}') await ctx.hope('worker_rss.process_story_webpage', dict( story_id=story_id, url=url, text=content, ))
def do_process_story_webpage( ctx: ActorContext, feed_id: T.int, offset: T.int, url: T.url, text: T.str.maxlen(_MAX_STORY_HTML_LENGTH), num_sub_sentences: T.int.optional, ) -> SCHEMA_FETCH_STORY_RESULT: # https://github.com/dragnet-org/dragnet # https://github.com/misja/python-boilerpipe # https://github.com/dalab/web2text # https://github.com/grangier/python-goose # https://github.com/buriy/python-readability # https://github.com/codelucas/newspaper DEFAULT_RESULT = dict(feed_id=feed_id, offset=offset, url=url) text = text.strip() if not text: return DEFAULT_RESULT text = story_html_clean(text) content = story_readability(text) content = process_story_links(content, url) content_info = StoryContentInfo(content) text_content = shorten(content_info.text, width=_MAX_STORY_CONTENT_LENGTH) num_sentences = len(split_sentences(text_content)) if len(content) > _MAX_STORY_CONTENT_LENGTH: msg = 'too large story#%s,%s size=%s url=%r, will only save plain text' LOG.warning(msg, feed_id, offset, len(content), url) content = text_content # 如果取回的内容比RSS内容更短,就不是正确的全文 if num_sub_sentences is not None: if not is_fulltext_content(content_info): if num_sentences <= num_sub_sentences: msg = 'fetched story#%s,%s url=%s num_sentences=%s less than num_sub_sentences=%s' LOG.info(msg, feed_id, offset, url, num_sentences, num_sub_sentences) return DEFAULT_RESULT summary = shorten(text_content, width=_MAX_STORY_SUMMARY_LENGTH) if not summary: return DEFAULT_RESULT result = dict( **DEFAULT_RESULT, content=content, summary=summary, sentence_count=num_sentences, ) if not ctx.message.is_ask: ctx.hope('harbor_rss.update_story', result) return result
async def do_fetch_story( ctx: ActorContext, feed_id: T.int, offset: T.int.min(0), url: T.url, use_proxy: T.bool.default(False), num_sub_sentences: T.int.optional, ) -> SCHEMA_FETCH_STORY_RESULT: LOG.info(f'fetch story#{feed_id},{offset} url={unquote(url)} begin') options = _proxy_helper.get_proxy_options() if DNS_SERVICE.is_resolved_url(url): use_proxy = False # make timeout less than actor default 30s to avoid ask timeout options.update(request_timeout=25) async with AsyncFeedReader(**options) as reader: use_proxy = use_proxy and reader.has_proxy url, content, response = await _fetch_story(reader, feed_id, offset, url, use_proxy=use_proxy) DEFAULT_RESULT = dict(feed_id=feed_id, offset=offset, url=url, response_status=response.status, use_proxy=response.use_proxy) if not content: return DEFAULT_RESULT if len(content) >= _MAX_STORY_HTML_LENGTH: content = story_html_clean(content) if len(content) >= _MAX_STORY_HTML_LENGTH: msg = 'too large story#%s,%s size=%s url=%r' LOG.warning(msg, feed_id, offset, len(content), url) content = story_html_to_text(content)[:_MAX_STORY_HTML_LENGTH] msg_func = ctx.ask if ctx.message.is_ask else ctx.hope result = await msg_func( 'worker_rss.process_story_webpage', dict( feed_id=feed_id, offset=offset, url=url, text=content, num_sub_sentences=num_sub_sentences, )) if not ctx.message.is_ask: return DEFAULT_RESULT result.update(DEFAULT_RESULT) return result
def _get_storys(entries: list): storys = deque(maxlen=300) # limit num storys while entries: data = entries.pop() story = {} content = '' if data["content"]: # both content and summary will in content list, peek the longest for x in data["content"]: value = x["value"] if value and len(value) > len(content): content = value if not content: content = data["description"] if not content: content = data["summary"] story['has_mathjax'] = story_has_mathjax(content) link = normlize_url(data["link"]) valid_link = '' if link: try: valid_link = validate_url(link) except Invalid: LOG.warning(f'invalid story link {link!r}') story['link'] = valid_link content = story_html_clean(content) if len(content) >= 1024 * 1024: msg = 'too large story link=%r content length=%s, will only save plain text!' LOG.warning(msg, link, len(content)) content = story_html_to_text(content) content = process_story_links(content, valid_link) story['content'] = content summary = data["summary"] if not summary: summary = content summary = shorten(story_html_to_text(summary), width=300) story['summary'] = summary title = shorten(data["title"] or link or summary, 200) unique_id = shorten(data['id'] or link or title, 200) content_hash_base64 = compute_hash_base64(content, summary, title) story['title'] = title story['content_hash_base64'] = content_hash_base64 story['unique_id'] = unique_id story['author'] = shorten(data["author"], 200) story['dt_published'] = _get_dt_published(data) story['dt_updated'] = _get_dt_updated(data) storys.append(story) return list(storys)
def do_process_story_webpage( ctx: ActorContext, story_id: T.int, url: T.url, text: T.str.maxlen(_MAX_STORY_HTML_LENGTH), num_sub_sentences: T.int.optional, ): # https://github.com/dragnet-org/dragnet # https://github.com/misja/python-boilerpipe # https://github.com/dalab/web2text # https://github.com/grangier/python-goose # https://github.com/buriy/python-readability # https://github.com/codelucas/newspaper text = text.strip() if not text: return text = story_html_clean(text) content = story_readability(text) content = process_story_links(content, url) if len(content) > _MAX_STORY_CONTENT_LENGTH: msg = 'too large story#%s size=%s url=%r, will only save plain text' LOG.warning(msg, story_id, len(content), url) content = shorten(story_html_to_text(content), width=_MAX_STORY_CONTENT_LENGTH) # 如果取回的内容比RSS内容更短,就不是正确的全文 if num_sub_sentences is not None: if not is_fulltext_content(content): num_sentences = len(split_sentences(story_html_to_text(content))) if num_sentences <= num_sub_sentences: msg = 'fetched story#%s url=%s num_sentences=%s less than num_sub_sentences=%s' LOG.info(msg, story_id, url, num_sentences, num_sub_sentences) return summary = shorten(story_html_to_text(content), width=_MAX_STORY_SUMMARY_LENGTH) if not summary: return ctx.hope( 'harbor_rss.update_story', dict( story_id=story_id, content=content, summary=summary, url=url, ))
async def do_fetch_story( ctx: ActorContext, story_id: T.int, url: T.url, use_proxy: T.bool.default(False), ): LOG.info(f'fetch story#{story_id} url={unquote(url)} begin') options = _get_proxy_options() options.update(allow_private_address=CONFIG.allow_private_address) async with AsyncFeedReader(**options) as reader: use_proxy = use_proxy and reader.has_rss_proxy response = await reader.read(url, use_proxy=use_proxy) if response and response.url: url = str(response.url) LOG.info( f'fetch story#{story_id} url={unquote(url)} status={response.status} finished' ) if not (response and response.ok): return if not response.content: msg = 'story#%s url=%s response text is empty!' LOG.error(msg, story_id, unquote(url)) return try: content = response.content.decode(response.encoding) except UnicodeDecodeError as ex: LOG.warning('fetch story unicode decode error=%s url=%r', ex, url) content = response.content.decode(response.encoding, errors='ignore') if len(content) >= 1024 * 1024: content = story_html_clean(content) if len(content) >= 1024 * 1024: msg = 'too large story#%s size=%s url=%r' LOG.warning(msg, story_id, len(content), url) content = story_html_to_text(content) await ctx.hope('worker_rss.process_story_webpage', dict( story_id=story_id, url=url, text=content, ))
def _get_storys(entries: list): storys = deque(maxlen=300) # limit num storys while entries: data = entries.pop() story = {} story['unique_id'] = shorten(_get_story_unique_id(data), 200) content = '' if data["content"]: # both content and summary will in content list, peek the longest for x in data["content"]: value = x["value"] if value and len(value) > len(content): content = value if not content: content = data["description"] if not content: content = data["summary"] story['has_mathjax'] = story_has_mathjax(content) content = story_html_clean(content) content = process_story_links(content, data["link"]) story['content'] = content summary = data["summary"] if not summary: summary = content # TODO: performance summary = shorten(story_html_to_text(summary), width=300) story['summary'] = summary story['link'] = data["link"] title = shorten(data["title"] or story['link'] or story['unique_id'], 200) content_hash_base64 = compute_hash_base64(content, summary, title) story['title'] = title story['content_hash_base64'] = content_hash_base64 story['author'] = shorten(data["author"], 200) story['dt_published'] = _get_dt_published(data) story['dt_updated'] = _get_dt_updated(data) storys.append(story) return list(storys)