def test_search_by_date_tags_post_type_params_mutually_exclusive(): """Tags and post type params should not be mixed up together""" with pytest.raises(ValueError): res = search_by_date('lisp', 'pg', stories=True, tags=tags.Comment) next(res) with pytest.raises(ValueError): res = search_by_date('lisp', stories=True, tags=tags.Comment) next(res) with pytest.raises(ValueError): res = search_by_date(stories=True, tags=tags.Comment) next(res)
def iter_search(self, query: str, limit=None) -> Iterator[Result]: from hn import search_by_date # pip3 install python-hn results = search_by_date(query) # By default, all the different "post types" will be included: stories, comments, polls, etc. for r in results: crs = r['created_at'] dt = datetime.strptime(crs, '%Y-%m-%dT%H:%M:%S.%f%z') p = r['points'] p = -1 if p is None else p st = r['story_text'] ct = r['comment_text'] assert not (st is not None and ct is not None) text = st or ct or '' nc = r['num_comments'] nc = -1 if nc is None else nc yield Result( uid=r['objectID'], when=dt, user=r['author'], url=r['url'], title=r['title'], text=text, points=p, comments=nc, )
def test_search_by_date_with_query_and_author_post_types_params(): with (REQUESTS_PATH / '1.1.json').open() as fp: responses.add( responses.GET, 'https://hn.algolia.com/api/v1/search_by_date?query=rmotr&hitsPerPage=2&tags=(story,comment),author_pg', json=json.loads(fp.read()), status=200) with (REQUESTS_PATH / '1.2.json').open() as fp: responses.add( responses.GET, 'https://hn.algolia.com/api/v1/search_by_date?query=rmotr&hitsPerPage=2&tags=(story,comment),author_pg&numericFilters=created_at_i<1542316220', json=json.loads(fp.read()), status=200) res = search_by_date('rmotr', author='pg', stories=True, comments=True, hits_per_page=2) post = next(res) assert post['story_id'] == 18445714 post = next(res) assert post['story_id'] == 18462671 post = next(res) assert post['story_id'] == 18460087 post = next(res) assert post['story_id'] == 18457200
def test_search_by_date_with_query_created_and_points(): with (REQUESTS_PATH / '1.1.json').open() as fp: responses.add( responses.GET, 'https://hn.algolia.com/api/v1/search_by_date?hitsPerPage=2&numericFilters=created_at_i>1514764800,points=50', json=json.loads(fp.read()), status=200) with (REQUESTS_PATH / '1.2.json').open() as fp: responses.add( responses.GET, 'https://hn.algolia.com/api/v1/search_by_date?hitsPerPage=2&numericFilters=created_at_i>1514764800,points=50,created_at_i<1542316220', json=json.loads(fp.read()), status=200) res = search_by_date(hits_per_page=2, created_at__gt='2018', points=50) post = next(res) assert post['story_id'] == 18445714 post = next(res) assert post['story_id'] == 18462671 post = next(res) assert post['story_id'] == 18460087 post = next(res) assert post['story_id'] == 18457200
def _search(self, keyword: str, since: datetime, until: datetime) -> Dict[Any, Any]: response = hn.search_by_date( q=keyword, comments=True, created_at__gt=str(since.date()), created_at__lt=str(until.date()), ) return response