Esempio n. 1
0
def random_xkcd(latest=False):
    """Retrieve url of a random or the latest xkcd"""
    logging.debug("Running random xkcd...")

    try:
        client = xkcd_wrapper.Client()
        comic = client.get_latest() if latest is True else client.get_random()
        return f"{comic.title}:\n{comic.description}\n{comic.image}"

    except Exception as e:
        logging.error(e)
Esempio n. 2
0
 def test_get(self):
     c = xkcd_wrapper.Client()
     with requests_mock.mock() as mock:
         mock.get(comic_id_url.format(628), text=xkcd_api_example_628_raw)
         mock.get(xkcd_api_example_628_dict['img'], content=raw_comic_image)
         response = c.get(628)
         check_comic(self, response, xkcd_api_example_628_dict)
         with self.assertRaises(TypeError):
             c.get('')
         with self.assertRaises(TypeError):
             c.get([1, 2, 3])
Esempio n. 3
0
 def test_get_without_raw_image(self):
     c = xkcd_wrapper.Client()
     with requests_mock.mock() as mock:
         mock.get(comic_id_url.format(628), text=xkcd_api_example_628_raw)
         response = c.get(628, raw_comic_image=False)
         check_comic(self,
                     response,
                     xkcd_api_example_628_dict,
                     raw_image=False)
         with self.assertRaises(TypeError):
             c.get('')
         with self.assertRaises(TypeError):
             c.get([1, 2, 3])
Esempio n. 4
0
 def test_get_latest(
         self):  # let's assume the example_628 json is the latest comic
     c = xkcd_wrapper.Client()
     with requests_mock.mock() as mock:
         mock.get(latest_comic_url, text=xkcd_api_example_628_raw)
         response = c.get_latest(raw_comic_image=False)
         check_comic(self,
                     response,
                     xkcd_api_example_628_dict,
                     raw_image=False)
         # alias
         response = c.latest(raw_comic_image=False)
         check_comic(self,
                     response,
                     xkcd_api_example_628_dict,
                     raw_image=False)
Esempio n. 5
0
    def test_client_init(self):
        with requests_mock.mock() as mock:
            mock.get(requests_mock.ANY)
            c = xkcd_wrapper.Client()
        self.assertFalse(
            mock.called)  # no request was performed during Client init
        self.assertIsInstance(c, xkcd_wrapper.Client)
        self.assertIsInstance(c._base_url, str)
        self.assertIsInstance(c._api, str)
        self.assertIsInstance(c._explanation_wiki_url, str)

        self.assertIsInstance(c._response_int_values, dict)
        self.assertEqual(c._response_int_values['num'], 'id')
        self.assertEqual(c._response_int_values['year'], 'date')
        self.assertEqual(c._response_int_values['month'], 'date')
        self.assertEqual(c._response_int_values['day'], 'date')
Esempio n. 6
0
 def test_get_random(
         self):  # let's assume the example_628 json is the latest comic
     c = xkcd_wrapper.Client()
     with requests_mock.mock() as mock:
         mock.get(latest_comic_url, text=xkcd_api_example_628_raw)
         mock.get(comic_id_url.format(138), text=xkcd_api_example_138_raw)
         random.seed(
             1)  # with latest comic being 628, random value will be 138
         response = c.get_random(raw_comic_image=False)
         self.assertEqual(response.id, xkcd_api_example_138_dict['num'])
         check_comic(self,
                     response,
                     xkcd_api_example_138_dict,
                     raw_image=False)
         # alias
         random.seed(1)
         response = c.random(raw_comic_image=False)
         self.assertEqual(response.id, xkcd_api_example_138_dict['num'])
         check_comic(self,
                     response,
                     xkcd_api_example_138_dict,
                     raw_image=False)
Esempio n. 7
0
 def test_get_bad_response_fields(self):
     c = xkcd_wrapper.Client()
     with requests_mock.mock() as mock:
         mock.get(comic_id_url.format(628), text=xkcd_api_example_wrong_raw)
         with self.assertRaises(xkcd_wrapper.exceptions.BadResponseField):
             c.get(628, raw_comic_image=False)
Esempio n. 8
0
 def test_get_http_error(self):
     c = xkcd_wrapper.Client()
     with requests_mock.mock() as mock:
         mock.get(comic_id_url.format(628), status_code=404)
         with self.assertRaises(xkcd_wrapper.exceptions.HttpError):
             c.get(628, raw_comic_image=False)
Esempio n. 9
0
 def test_comic_id_url(self, _id):
     c = xkcd_wrapper.Client()
     self.assertEqual(c.comic_id_url(_id), comic_id_url.format(_id))
Esempio n. 10
0
 def test_latest_comic_url(self):
     c = xkcd_wrapper.Client()
     self.assertEqual(c.latest_comic_url(), latest_comic_url)
Esempio n. 11
0
 def test_base_url(self):
     c = xkcd_wrapper.Client()
     self.assertEqual(c.base_url(), base_url)
Esempio n. 12
0
 def test__repr__(self):
     c = xkcd_wrapper.Client()
     self.assertEqual(str(c), 'xkcd_wrapper.Client()')