def get_similar(self, pid): """ Get papers similar in content to the named paper. """ logging.info("Getting ArXiv papers similar to paper: {}".format(pid)) similarURL = ASP_BaseURL + str(pid) similar_papers = papers_from_embedded_script(similarURL)[1:] # TODO is the searched paper always first? attached_papers = [] for p in similar_papers: attached_papers.append(paper_snippet(p)) return build_message(text="*Your Library*", markdown=False, parts=attached_papers)
def get_recommended(self, session): """ Get the papers recommended to the user based on their saved papers and their search domain. """ # TODO the /recommend endpoint has FILTERS logging.info("Getting recommended ArXiv papers for user.") recommendedURL = ASP_BaseURL + "/recommend" papers = papers_from_embedded_script(recommendedURL, session) attached_papers = [] for i, p in enumerate(papers): attached_papers.append(paper_snippet(p, i + 1)) return build_message( text="*Your Recommended*", markdown=False, parts=attached_papers), attached_papers[:5]
def get_most_recent(self, session): """ Get most recently published papers within the user's search domain. """ logging.info("Getting most recent ArXiv papers within user's domain.") papers = papers_from_embedded_script(ASP_BaseURL+"/", session) attached_papers = [] for i, p in enumerate(papers): attached_papers.append(paper_snippet(p, i + 1)) return build_message( text="*Your Most Recent*", markdown=False, parts=attached_papers), attached_papers[:5]
def get_top_recent(self, session): """ Get the 'top' recent papers within the user's search domain. """ # TODO the /top endpoint has FILTERS logging.info("Getting top recent ArXiv papers in user's domain.") topURL = ASP_BaseURL + "/top" # default filters papers = papers_from_embedded_script(topURL, session=session) attached_papers = [] for i, p in enumerate(papers): attached_papers.append(paper_snippet(p, i + 1)) return build_message( text="*Your Library*", markdown=False, parts=attached_papers), attached_papers[:5]
def get_library(self, session): """ Get all papers saved by the user. Their 'library'. """ logging.info("Getting ArXiv library.") libraryURL = ASP_BaseURL + "/library" try: papers = papers_from_embedded_script(libraryURL, session=session) except ReadTimeout: return build_message( text="*Connection Problem. Please try again*", markdown=False, parts=None) attached_papers = [] for i, p in enumerate(papers): attached_papers.append(paper_snippet(p, i + 1)) return build_message( text="*Your Library*", markdown=False, parts=attached_papers), attached_papers
def test_paper_snippet(): paper = { 'link': "www.paper_link.com", 'title': "A Paper Title", 'authors': ['A. Scott', 'F. Itzgerald', 'M. Wallis'], 'originally_published_time': '3:30 pm', 'pid': 123982409 } expected_snippet = { 'fallback': 'A Paper Title', 'text': '<www.paper_link.com|1. A Paper Title>\nA. Scott, F. Itzgerald, M. Wallis - 3:30 pm\n<http://www.arxiv-sanity.com/pdf/123982409.pdf|PDF>\n' } assert expected_snippet == paper_snippet(paper, 1)
def search_arxiv(self, query, num_papers=5): """ Search arxiv papers by search string. """ logging.info("Searching ArXiv for: {}".format(query)) tokens = query.split(' ') searchEndpoint = 'search?q=' + '+'.join(tokens) searchURL = ASP_BaseURL + searchEndpoint papers = papers_from_embedded_script(searchURL) bot_uid = self.clients.bot_user_id() attached_papers = [] # For each paper for i in range(min(num_papers,len(papers))): attached_papers.append(paper_snippet(papers[i], i + 1)) attached_papers[-1]['footer'] = "> `<@" + bot_uid + "> show more papers` - to see more papers" # build the json message object return build_message( text="*Search Results*", markdown=False, parts=attached_papers), attached_papers
def get_paper(self, paper_id): """ Return specified paper from within the set. """ if not self.previous_attachments: logging.info("Can't retreive paper. No papers in current context.") return build_message(text="*Can't retreive paper*"), None else: pid = re.findall('[0-9]+\.?[0-9]*', self.previous_attachments[paper_id - 1]['text'])[0] logging.info("Getting paper: {} from ArXiv.".format(pid)) paperURL = ASP_BaseURL + "/" + str(pid) paper = papers_from_embedded_script(paperURL)[ 0] # only get first, rest are related papers return build_message(text="*Here's your paper*", markdown=False, parts=None), [ paper_snippet(paper, 1, include_abstract=True) ]
def test_paper_snippet_with_reddit_discussions(mock_reddit_conversations): mock_reddit_conversations.return_value = ( 'r/helloworld', 'www.link.com', 30 ) paper = { 'link': "www.paper_link.com", 'title': "A Paper Title", 'authors': ['A. Scott', 'F. Itzgerald', 'M. Wallis'], 'originally_published_time': '3:30 pm', 'pid': 123982409 } expected_snippet = { 'fallback': 'A Paper Title', 'text': '<www.paper_link.com|1. A Paper Title>\nA. Scott, F. Itzgerald, M. Wallis - 3:30 pm\n<http://www.arxiv-sanity.com/pdf/123982409.pdf|PDF>\n<www.link.com|r/helloworld Discussion. 30 comments\n' } assert expected_snippet == paper_snippet(paper, 1, include_discussions=True)
def setup_method(self): self.intent_handler = ApiAiIntentHandler(MockClient()) self.dummy_parts = [] for i in range(len(dummy_papers)): self.dummy_parts.append(paper_snippet(dummy_papers[i], i + 1))