Пример #1
0
def main() -> None:
    # Parse CLI arguments.
    parser = argparse.ArgumentParser()
    parser.add_argument('--endpoint',
                        required=False,
                        type=str,
                        help='TVmaze API endpoint (e.g. `shows`)')
    parser.add_argument(
        '--data-file-path',
        required=False,
        type=str,
        help='Use the specified data file instead of downloading it')
    parser.add_argument('--filter-dict',
                        required=True,
                        type=json.loads,
                        help='Filter criteria (e.g. `{"name":"Archer"}`)')
    args = parser.parse_args()

    if (args.endpoint is not None) + (args.data_file_path is not None) != 1:
        raise ValueError(
            'You must specify either `--endpoint` or `--data-file-path` argument, but not both.'
        )

    # Get and filter the results.
    results = TVmaze.get_results(args.endpoint,
                                 data_file_path=args.data_file_path)
    filtered_results = results.get_filtered_results(args.filter_dict)

    # Output the filtered results.
    print()
    print(json.dumps(filtered_results, indent=4, sort_keys=True))
    print()
Пример #2
0
 def test_get_results_non_paginated_endpoint_ok(self, get_mock):
     self.assertEqual(Results([{
         'a': 1
     }, {
         'b': 2
     }]), TVmaze.get_results('shows/315/episodes'))
     get_mock.assert_called_once_with(
         'http://api.tvmaze.com/shows/315/episodes')
Пример #3
0
 def test_get_results_paginated_endpoint_one_page(self, get_mock):
     self.assertEqual(Results([{
         'a': 1
     }, {
         'b': 2
     }]), TVmaze.get_results('shows'))
     self.assertEqual(2, get_mock.call_count)
     get_mock.assert_has_calls([
         call('http://api.tvmaze.com/shows?page=0'),
         call('http://api.tvmaze.com/shows?page=1'),
     ],
                               any_order=False)
Пример #4
0
 def test_get_results_paginated_endpoint_two_pages(self, get_mock):
     self.assertEqual(Results([{
         'b': 2
     }, {
         'd': 6
     }, {
         'cc': 5,
         'E': 11
     }]), TVmaze.get_results('shows'))
     self.assertEqual(3, get_mock.call_count)
     get_mock.assert_has_calls([
         call('http://api.tvmaze.com/shows?page=0'),
         call('http://api.tvmaze.com/shows?page=1'),
         call('http://api.tvmaze.com/shows?page=2'),
     ],
                               any_order=False)
Пример #5
0
 def test_get_results_paginated_endpoint_no_pages(self, get_mock):
     self.assertEqual(Results([]), TVmaze.get_results('shows'))
     get_mock.assert_called_once_with('http://api.tvmaze.com/shows?page=0')
Пример #6
0
 def test_get_results_load_from_file(self, get_mock):
     results = TVmaze.get_results(None, self.TEST_FILE_FILE)
     self.assertEqual(110, results.get_num_results())
     self.assertFalse(get_mock.called)
Пример #7
0
 def test_get_results_from_file(self):
     results = TVmaze.get_results_from_file(self.TEST_FILE_FILE)
     self.assertEqual(110, results.get_num_results())
Пример #8
0
 def loadResults(cls, base_file_name):
     cls.results = TVmaze.get_results_from_file(
         f'test_data/{base_file_name}.json')