Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/discoveryplusindia.py (Python)

Code before:
            url, display_id, 'ap2-prod-direct.discoveryplus.in', 'dplusindia', 'in')


class DiscoveryPlusIndiaShowIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?discoveryplus\.in/show/(?P<show_name>\S*)'
    _TESTS = [{
        'url': 'https://www.discoveryplus.in/show/how-do-they-do-it',
        'playlist_mincount': 140,
        'info_dict': {
            'id': 'how-do-they-do-it',
        },
    }
    ]

    def _entries(self, show_name):
        headers = {
            'authority': 'ap2-prod-direct.discoveryplus.in',
            'x-disco-client': 'WEB:UNKNOWN:dplus-india:prod',
            'x-disco-params': 'realm=dplusindia',
            'origin': 'https://www.discoveryplus.in',
            'referer': 'https://www.discoveryplus.in/',
        }
        show_url = 'https://ap2-prod-direct.discoveryplus.in/cms/routes/show/{}?include=default'.format(show_name)
        show_json = self._download_json(show_url,
                                        video_id=show_name,
                                        headers=headers).get('included')[4].get('attributes').get('component')
        show_id = show_json.get('mandatoryParams').split('=')[-1]
        season_url = 'https://ap2-prod-direct.discoveryplus.in/content/videos?sort=episodeNumber&filter[seasonNumber]={}&filter[show.id]={}&page[size]=100&page[number]={}'
        for season in show_json.get('filters')[0].get('options'):
            season_id = season.get('id')
            season_json = self._download_json(season_url.format(season_id, show_id, '1'), video_id=show_id, headers=headers)
            total_pages = int(season_json.get('meta').get('totalPages')) + 1
            for page in range(1, total_pages):
                episode_url = season_url.format(season_id, show_id, str(page))
                episodes_json = self._download_json(
                    episode_url, video_id=show_id, headers=headers,
                    note='Downloading JSON metadata page %s' % page).get('data')
                for episode in episodes_json:

                    video_id = episode.get('attributes').get('path')
                    yield self.url_result(
                        'https://discoveryplus.in/videos/%s' % video_id,
                        ie=DiscoveryPlusIndiaIE.ie_key(), video_id=video_id)

    def _real_extract(self, url):
        show_name = re.match(self._VALID_URL, url).group('show_name')
        return self.playlist_result(self._entries(show_name), playlist_id=show_name)


Review comment (refactor):
```suggestion
        show_json = try_get(
            self._download_json(
                'https://ap2-prod-direct.discoveryplus.in/cms/routes/show/%s?include=default' % show_name,
                show_name, headers=headers),
            lambda x: x['included'][4]['attributes']['component'])
```

Don't use `.get().get()...`. That is what `try_get` is for. Similar changes needed below.
If the field must be present for the extraction to work, you can use direct access `x['key']['key']` so that an error will be thrown, and if the key may not be present, use `try_get` instead of a `.get` chain

Also, can u explain the reason behind the `[4]`. Is the information always guaranteed to be in the 4th item?

Code after:
        display_id = self._match_id(url)
        return self._get_disco_api_info(
            url, display_id, 'ap2-prod-direct.discoveryplus.in', 'dplusindia', 'in')


class DiscoveryPlusIndiaShowIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?discoveryplus\.in/show/(?P<show_name>[^/]+)/?(?:[?#]|$)'
    _TESTS = [{
        'url': 'https://www.discoveryplus.in/show/how-do-they-do-it',
        'playlist_mincount': 140,
        'info_dict': {
            'id': 'how-do-they-do-it',
        },
    }
    ]

    def _entries(self, show_name):
        headers = {
            'x-disco-client': 'WEB:UNKNOWN:dplus-india:prod',
            'x-disco-params': 'realm=dplusindia',
            'referer': 'https://www.discoveryplus.in/',
        }
        show_url = 'https://ap2-prod-direct.discoveryplus.in/cms/routes/show/{}?include=default'.format(show_name)
        show_json = self._download_json(show_url,
                                        video_id=show_name,
                                        headers=headers)['included'][4]['attributes']['component']
        show_id = show_json['mandatoryParams'].split('=')[-1]
        season_url = 'https://ap2-prod-direct.discoveryplus.in/content/videos?sort=episodeNumber&filter[seasonNumber]={}&filter[show.id]={}&page[size]=100&page[number]={}'
        for season in show_json['filters'][0]['options']:
            season_id = season['id']
            total_pages, page_num = 1, 0
            while page_num < total_pages:
                season_json = self._download_json(season_url.format(season_id, show_id, compat_str(page_num + 1)),
                                                  video_id=show_id, headers=headers,
                                                  note='Downloading JSON metadata%s' % (' page %d' % page_num if page_num else ''))
                if page_num == 0:
                    total_pages = try_get(season_json, lambda x: x['meta']['totalPages'], int) or 1
                episodes_json = season_json['data']
                for episode in episodes_json:
                    video_id = episode['attributes']['path']
                    yield self.url_result(
                        'https://discoveryplus.in/videos/%s' % video_id,
                        ie=DiscoveryPlusIndiaIE.ie_key(), video_id=video_id)
                page_num += 1

    def _real_extract(self, url):
        show_name = re.match(self._VALID_URL, url).group('show_name')
        return self.playlist_result(self._entries(show_name), playlist_id=show_name)