def get_current_news(self): headers = { "q": self.active_category } # q=query parameter/search parameter section = theguardian_section.Section(api='test', **headers) section_content = section.get_content_response() results = section.get_results(section_content) editions = results[0]['editions'] articles = [ edi["apiUrl"] for edi in editions if self.active_category in edi['id'] and self.active_edition == edi['code'] ] if not articles: articles = editions[0][ "apiUrl"] # there was only a default edition or categories weren't found else: articles = articles[0] content = theguardian_content.Content(api='test', url=articles) content_response = content.get_content_response() results = content_response['response']['results'] newest_result = results[0] title = get_article_title(newest_result['apiUrl']) return title, newest_result['webUrl']
def test_section_get_indirect_content(self): api_key = "test" section = theguardian_section.Section(api_key) res = section.get_request_response() section_content = section.get_content_response() self.assertEqual(res.status_code, 200) self.assertIn("response", section_content.keys())
def test_section_get_result_without_exception(self): api_key = "test" section = theguardian_section.Section(api_key) section_content = section.get_content_response() results = section.get_results(section_content) self.assertIs(type(results), list) self.assertIn("editions", results[0].keys())
def test_section_get_indirect_content_invalid_credentials(self): api_key = "tests" section = theguardian_section.Section(api_key) res = section.get_request_response() section_content = section.get_content_response() self.assertEqual(res.status_code, 403) self.assertIn("message", section_content.keys()) self.assertEqual("Invalid authentication credentials", section_content["message"])
from theguardian import theguardian_section # create section section = theguardian_section.Section(api='test') # gets raw_response raw_section_content = section.get_request_response() print("Request Response status code {status}.".format( status=raw_section_content.status_code)) print("Request Response headers {header}.".format( header=raw_section_content.headers)) # content print("Section Response headers {}.".format(section.response_headers())) # get all results of a page json_content = section.get_content_response() all_results = section.get_results(json_content) print("All results {}.".format(all_results)) # actual response print("Response {response}".format(response=json_content))
def test_section_get_result_with_exception(self): api_key = "test" section = theguardian_section.Section(api_key) self.assertRaises(TypeError, section.get_results, "some random text")
def test_section_get_direct_content(self): api_key = "test" res = theguardian_section.Section(api_key).get_content_response() self.assertIn("response", res.keys())
def test_section_response_failure_incorrect_api_key(self): api_key = "tests" res = theguardian_section.Section(api_key).get_request_response() self.assertEqual(res.status_code, 403)
def test_section_response_success_correct_details(self): api_key = "test" res = theguardian_section.Section(api_key).get_request_response() self.assertEqual(res.status_code, 200)
""" This example deals with returning content of section. """ from theguardian import theguardian_section from theguardian import theguardian_content # get the sports sections headers = {"q": "sports"} # q=query parameter/search parameter section = theguardian_section.Section(api='test', **headers) # get the results section_content = section.get_content_response() results = section.get_results(section_content) # get different editions from the results editions = results[0]['editions'] # get uk/sports edition apiUrl uk_sports = [edi["apiUrl"] for edi in editions if edi["id"] == "uk/sport"][0] # use this api url to sports content content = theguardian_content.Content(api='test', url=uk_sports) # get section response content_response = content.get_content_response() print(content_response)