Repository: yt-dlp/yt-dlp

Issue:
Infinite loop downloading Bilibili comments
### Checklist

- [X] I'm reporting a broken site
- [X] I've verified that I'm running yt-dlp version **2021.10.22**. ([update instructions](https://github.com/yt-dlp/yt-dlp#update))
- [X] I've checked that all provided URLs are alive and playable in a browser
- [X] I've checked that all URLs and arguments with special characters are [properly quoted or escaped](https://github.com/ytdl-org/youtube-dl#video-url-contains-an-ampersand-and-im-getting-some-strange-output-1-2839-or-v-is-not-recognized-as-an-internal-or-external-command)
- [X] I've searched the [bugtracker](https://github.com/yt-dlp/yt-dlp/issues?q=) for similar issues including closed ones. DO NOT post duplicates
- [X] I've read the [guidelines for opening an issue](https://github.com/yt-dlp/yt-dlp/blob/master/CONTRIBUTING.md#opening-an-issue)
- [ ] I've read about [sharing account credentials](https://github.com/yt-dlp/yt-dlp/blob/master/CONTRIBUTING.md#are-you-willing-to-share-account-details-if-needed) and I'm willing to share it if required

### Region

all

### Description

There is an infinite loop in the Bilibili comments extraction code:

https://github.com/yt-dlp/yt-dlp/blob/ad64a2323f1ce0f8aeb07e4ead46630edec2bf2d/yt_dlp/extractor/bilibili.py#L374-L382

`itertools.count(1)` is an infinite generator, but there's no `break` or `return` in the loop.

Note: this code was recently refactored (https://github.com/yt-dlp/yt-dlp/commit/e88d44c6ee66e2a1b814c2fe89fc53b3c3e029ef), but the infinite loop was already present.

Separately, `commentPageNumber` is left over from the refactoring and can be removed.

### Verbose log

```shell
[debug] Command-line config: ['https://www.bilibili.com/video/BV1JK4y1A7uD', '--write-comments', '--skip-download', '-Uv']
[debug] Encodings: locale UTF-8, fs utf-8, out utf-8, err utf-8, pref UTF-8[debug] yt-dlp version 2021.10.22 (source)
[debug] Python version 3.9.5 (CPython 64bit) - Linux-5.11.0-27-generic-x86_64-with-glibc2.31
[debug] exe versions: ffmpeg 4.2.4, ffprobe 4.2.4
[debug] Optional libraries: Cryptodome, keyring, mutagen, sqlite, websockets
[debug] Proxy map: {}
yt-dlp is up to date (2021.10.22)
[debug] [BiliBili] Extracting URL: https://www.bilibili.com/video/BV1JK4y1A7uD
[BiliBili] 1JK4y1A7uD: Grabbing original ID via API
[BiliBili] 887984324: Downloading webpage
[BiliBili] 887984324: Extracting videos in anthology
[BiliBili] Downloading anthology 887984324 - add --no-playlist to just download video
[download] Downloading playlist: 你们要的水豚拉粑粑视频（我终于蹲到了！）
[info] Writing playlist metadata as JSON to: 你们要的水豚拉粑粑视频（我终于蹲到了！） [BV1JK4y1A7uD].info.json
[BiliBili] playlist 你们要的水豚拉粑粑视频（我终于蹲到了！）: Collected 1 videos; downloading 1 of them
[download] Downloading video 1 of 1
[debug] [BiliBili] Extracting URL: https://www.bilibili.com/video/BV1JK4y1A7uD?p=1
[BiliBili] 1JK4y1A7uD: Grabbing original ID via API
[BiliBili] 887984324: Downloading webpage
[BiliBili] 887984324: Downloading video info page
[debug] Formats sorted by: hasvid, ie_pref, lang, quality, res, fps, hdr:12(7), vcodec:vp9.2(10), acodec, filesize, fs_approx, tbr, vbr, abr, asr, proto, vext, aext, hasaud, source, id
[BiliBili] 887984324: Extracting videos in anthology
[BiliBili] 887984324: Downloading tags
[debug] Default format spec: bestvideo*+bestaudio/best
[info] 887984324_part1: Downloading 1 format(s): 0
[BiliBili] 887984324: Extracting comments from page 1
[BiliBili] 887984324: Extracting comments from page 2
[BiliBili] 887984324: Extracting comments from page 3
[BiliBili] 887984324: Extracting comments from page 4
[BiliBili] 887984324: Extracting comments from page 5
[BiliBili] 887984324: Extracting comments from page 6
[BiliBili] 887984324: Extracting comments from page 7
[BiliBili] 887984324: Extracting comments from page 8
[BiliBili] 887984324: Extracting comments from page 9
[BiliBili] 887984324: Extracting comments from page 10
[BiliBili] 887984324: Extracting comments from page 11
[BiliBili] 887984324: Extracting comments from page 12
[BiliBili] 887984324: Extracting comments from page 13
[BiliBili] 887984324: Extracting comments from page 14
[BiliBili] 887984324: Extracting comments from page 15
[BiliBili] 887984324: Extracting comments from page 16
[BiliBili] 887984324: Extracting comments from page 17
[BiliBili] 887984324: Extracting comments from page 18
[BiliBili] 887984324: Extracting comments from page 19
[BiliBili] 887984324: Extracting comments from page 20
[BiliBili] 887984324: Extracting comments from page 21
[BiliBili] 887984324: Extracting comments from page 22
[BiliBili] 887984324: Extracting comments from page 23
[BiliBili] 887984324: Extracting comments from page 24
[BiliBili] 887984324: Extracting comments from page 25
[BiliBili] 887984324: Extracting comments from page 26
[BiliBili] 887984324: Extracting comments from page 27
[BiliBili] 887984324: Extracting comments from page 28
[BiliBili] 887984324: Extracting comments from page 29
[BiliBili] 887984324: Extracting comments from page 30
[BiliBili] 887984324: Extracting comments from page 31
[BiliBili] 887984324: Extracting comments from page 32
[BiliBili] 887984324: Extracting comments from page 33
[BiliBili] 887984324: Extracting comments from page 34
[BiliBili] 887984324: Extracting comments from page 35
[BiliBili] 887984324: Extracting comments from page 36
[BiliBili] 887984324: Extracting comments from page 37
[BiliBili] 887984324: Extracting comments from page 38
[BiliBili] 887984324: Extracting comments from page 39
[BiliBili] 887984324: Extracting comments from page 40
[BiliBili] 887984324: Extracting comments from page 41
[BiliBili] 887984324: Extracting comments from page 42
[BiliBili] 887984324: Extracting comments from page 43
[BiliBili] 887984324: Extracting comments from page 44
[BiliBili] 887984324: Extracting comments from page 45
[BiliBili] 887984324: Extracting comments from page 46
[BiliBili] 887984324: Extracting comments from page 47
[BiliBili] 887984324: Extracting comments from page 48
[BiliBili] 887984324: Extracting comments from page 49
[BiliBili] 887984324: Extracting comments from page 50
[BiliBili] 887984324: Extracting comments from page 51
[BiliBili] 887984324: Extracting comments from page 52
[BiliBili] 887984324: Extracting comments from page 53
[BiliBili] 887984324: Extracting comments from page 54
[BiliBili] 887984324: Extracting comments from page 55
[BiliBili] 887984324: Extracting comments from page 56
[BiliBili] 887984324: Extracting comments from page 57
^C[BiliBili] Interrupted by user
[BiliBili] Extracted 50 comments
[info] Writing video metadata as JSON to: 你们要的水豚拉粑粑视频（我终于蹲到了！） [887984324_part1].info.json
[download] Finished downloading playlist: 你们要的水豚拉粑粑视频（我终于蹲到了！）
```



Discussion:

Resolving patch:
diff --git a/yt_dlp/extractor/bilibili.py b/yt_dlp/extractor/bilibili.py
--- a/yt_dlp/extractor/bilibili.py
+++ b/yt_dlp/extractor/bilibili.py
@@ -376,8 +376,10 @@ def _get_comments(self, video_id, commentPageNumber=0):
             replies = traverse_obj(
                 self._download_json(
                     f'https://api.bilibili.com/x/v2/reply?pn={idx}&oid={video_id}&type=1&jsonp=jsonp&sort=2&_=1567227301685',
-                    video_id, note=f'Extracting comments from page {idx}'),
-                ('data', 'replies')) or []
+                    video_id, note=f'Extracting comments from page {idx}', fatal=False),
+                ('data', 'replies'))
+            if not replies:
+                return
             for children in map(self._get_all_children, replies):
                 yield from children
 


Test patch: