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

Code before:
import itertools
import urllib.parse

from .common import InfoExtractor
from ..utils import (
    clean_html,
    int_or_none,
    parse_iso8601,
    update_url_query,
    url_or_none,
)
from ..utils.traversal import traverse_obj


class TuneInBaseIE(InfoExtractor):
    def _call_api(self, item_id, endpoint=None, note='Downloading JSON metadata', query=None):
        path = f'/{endpoint}' if endpoint else ''

        return self._download_json(
            f'https://api.tunein.com/profiles/{item_id}{path}', item_id, note=note, query=query)

    def _extract_formats_and_subtitles(self, content_id):
        streams = self._download_json(
            'https://opml.radiotime.com/Tune.ashx', content_id, query={
                'formats': 'mp3,aac,ogg,flash,hls',
                'id': content_id,
                'render': 'json',
            },
        )['body']

        formats, subtitles = [], {}
        for stream in streams:
            if stream.get('media_type') == 'hls':
                fmts, subs = self._extract_m3u8_formats_and_subtitles(stream['url'], content_id, fatal=False)
                formats.extend(fmts)
                self._merge_subtitles(subs, target=subtitles)
            else:
                formats.append(traverse_obj(stream, {
                    'abr': ('bitrate', {int_or_none}),
                    'ext': ('media_type', {str}),
                    'url': ('url', {self._proto_relative_url}, {url_or_none}),
                }))

        return formats, subtitles


Review comment (suggestion):
I would write it like this
```py
    def _call_api(self, item_id, endpoint=None, note='Downloading JSON metadata', query=None):
        return self._download_json(
            join_nonempty('https://api.tunein.com/profiles', item_id, endpoint, delim='/'),
            item_id, note=note, query=query)   
```

Code after:
import functools
import urllib.parse

from .common import InfoExtractor
from ..utils import (
    OnDemandPagedList,
    UnsupportedError,
    clean_html,
    int_or_none,
    join_nonempty,
    parse_iso8601,
    update_url_query,
    url_or_none,
)
from ..utils.traversal import traverse_obj


class TuneInBaseIE(InfoExtractor):
    def _call_api(self, item_id, endpoint=None, note='Downloading JSON metadata', fatal=False, query=None):
        return self._download_json(
            join_nonempty('https://api.tunein.com/profiles', item_id, endpoint, delim='/'),
            item_id, note=note, fatal=fatal, query=query) or {}

    def _extract_formats_and_subtitles(self, content_id):
        streams = self._download_json(
            'https://opml.radiotime.com/Tune.ashx', content_id, query={
                'formats': 'mp3,aac,ogg,flash,hls',
                'id': content_id,
                'render': 'json',
            })

        formats, subtitles = [], {}
        for stream in traverse_obj(streams, ('body', lambda _, v: url_or_none(v['url']))):
            if stream.get('media_type') == 'hls':
                fmts, subs = self._extract_m3u8_formats_and_subtitles(stream['url'], content_id, fatal=False)
                formats.extend(fmts)
                self._merge_subtitles(subs, target=subtitles)
            else:
                formats.append(traverse_obj(stream, {
                    'abr': ('bitrate', {int_or_none}),
                    'ext': ('media_type', {str}),
                    'url': ('url', {self._proto_relative_url}),
                }))

        return formats, subtitles