Repository: yt-dlp/yt-dlp

Issue:
[Site Request] TokenTube
<!--

######################################################################
  WARNING!
  IGNORING THE FOLLOWING TEMPLATE WILL RESULT IN ISSUE CLOSED AS INCOMPLETE
######################################################################

-->


## Checklist

<!--
Carefully read and work through this check list in order to prevent the most common mistakes and misuse of yt-dlp:
- First of, make sure you are using the latest version of yt-dlp. Run `yt-dlp --version` and ensure your version is 2021.08.10. If it's not, see https://github.com/yt-dlp/yt-dlp on how to update. Issues with outdated version will be REJECTED.
- Make sure that all provided video/audio/playlist URLs (if any) are alive and playable in a browser.
- Make sure that site you are requesting is not dedicated to copyright infringement, see https://github.com/yt-dlp/yt-dlp. yt-dlp does not support such sites. In order for site support request to be accepted all provided example URLs should not violate any copyrights.
- Search the bugtracker for similar site support requests: https://github.com/yt-dlp/yt-dlp. DO NOT post duplicates.
- Finally, put x into all relevant boxes like this [x] (Dont forget to delete the empty space)
-->

- [x] I'm reporting a new site support request
- [x] I've verified that I'm running yt-dlp version **2021.08.10**
- [x] I've checked that all provided URLs are alive and playable in a browser
- [x] I've checked that none of provided URLs violate any copyrights
- [x] The provided URLs do not contain any DRM to the best of my knowledge
- [x] I've searched the bugtracker for similar site support requests including closed ones


## Example URLs

<!--
Provide all kinds of example URLs support for which should be included. Replace following example URLs by yours.
-->

- Single video: https://tokentube.net/l/3236632011/Praise-A-Thon-Pastori-Chrisin-ja-Pastori-Bennyn-kanssa-27-8-2021
- Channel: https://tokentube.net/channel/3697658904/TokenTube


## Description

<!--
Provide any additional information.
If work on your issue requires account credentials please provide them or explain how one can obtain them.
-->

Hi, can you add support for TokenTube? It's a Finnish alternative to YouTube.



Discussion:
it has hardcoded video url
use this regex to obtain it `src\':\'([^\']+)`

Resolving patch:
diff --git a/yt_dlp/extractor/extractors.py b/yt_dlp/extractor/extractors.py
--- a/yt_dlp/extractor/extractors.py
+++ b/yt_dlp/extractor/extractors.py
@@ -1410,6 +1410,10 @@
     ToggleIE,
     MeWatchIE,
 )
+from .tokentube import (
+    TokentubeIE,
+    TokentubeChannelIE
+)
 from .tonline import TOnlineIE
 from .toongoggles import ToonGogglesIE
 from .toutv import TouTvIE
diff --git a/yt_dlp/extractor/tokentube.py b/yt_dlp/extractor/tokentube.py
new file mode 100644
--- /dev/null
+++ b/yt_dlp/extractor/tokentube.py
@@ -0,0 +1,152 @@
+# coding: utf-8
+from __future__ import unicode_literals
+
+import functools
+import re
+
+from .common import InfoExtractor
+from ..utils import (
+    parse_count,
+    unified_strdate,
+    js_to_json,
+    OnDemandPagedList,
+)
+
+
+class TokentubeIE(InfoExtractor):
+    _VALID_URL = r'https?://(?:www\.)?tokentube\.net/(?:view\?[vl]=|[vl]/)(?P<id>\d+)'
+    _TESTS = [{
+        'url': 'https://tokentube.net/l/3236632011/Praise-A-Thon-Pastori-Chrisin-ja-Pastori-Bennyn-kanssa-27-8-2021',
+        'info_dict': {
+            'id': '3236632011',
+            'ext': 'mp4',
+            'title': 'Praise-A-Thon Pastori Chrisin ja Pastori Bennyn kanssa 27.8.2021',
+            'description': '',
+            'uploader': 'Pastori Chris - Rapsodia.fi',
+            'upload_date': '20210827',
+        },
+        'params': {
+            'skip_download': True,
+        },
+    }, {
+        'url': 'https://tokentube.net/v/3950239124/Linux-Ubuntu-Studio-perus-k%C3%A4ytt%C3%B6',
+        'md5': '0e1f00421f501f5eada9890d38fcfb56',
+        'info_dict': {
+            'id': '3950239124',
+            'ext': 'mp4',
+            'title': 'Linux Ubuntu Studio perus käyttö',
+            'description': 'md5:854ff1dc732ff708976de2880ea32050',
+            'uploader': 'jyrilehtonen',
+            'upload_date': '20210825',
+        },
+    }, {
+        'url': 'https://tokentube.net/view?v=3582463289',
+        'info_dict': {
+            'id': '3582463289',
+            'ext': 'mp4',
+            'title': 'Police for Freedom - toiminta aloitetaan Suomessa ❤️??',
+            'description': 'md5:cd92e620d7f5fa162e8410d0fc9a08be',
+            'uploader': 'Voitontie',
+            'upload_date': '20210428',
+        }
+    }]
+
+    def _real_extract(self, url):
+        video_id = self._match_id(url)
+        webpage = self._download_webpage(url, video_id)
+
+        title = self._html_search_regex(r'<h1\s*class=["\']title-text["\']>(.+?)</h1>', webpage, 'title')
+
+        data_json = self._html_search_regex(r'({["\']html5["\'].+?}}}+)', webpage, 'data json')
+        data_json = self._parse_json(js_to_json(data_json), video_id, fatal=False)
+
+        sources = data_json.get('sources') or self._parse_json(
+            self._html_search_regex(r'updateSrc\(([^\)]+)\)', webpage, 'sources'),
+            video_id, transform_source=js_to_json)
+
+        formats = [{
+            'url': format.get('src'),
+            'format_id': format.get('label'),
+            'height': format.get('res'),
+        } for format in sources]
+
+        view_count = parse_count(self._html_search_regex(
+            r'<p\s*class=["\']views_counter["\']>\s*([\d\.,]+)\s*<span>views?</span></p>',
+            webpage, 'view_count', fatal=False))
+
+        like_count = parse_count(self._html_search_regex(
+            r'<div\s*class="sh_button\s*likes_count">\s*(\d+)\s*</div>',
+            webpage, 'like count', fatal=False))
+
+        dislike_count = parse_count(self._html_search_regex(
+            r'<div\s*class="sh_button\s*dislikes_count">\s*(\d+)\s*</div>',
+            webpage, 'dislike count', fatal=False))
+
+        upload_date = unified_strdate(self._html_search_regex(
+            r'<span\s*class="p-date">Published\s*on\s+([^<]+)',
+            webpage, 'upload date', fatal=False))
+
+        uploader = self._html_search_regex(
+            r'<a\s*class="place-left"[^>]+>(.+?)</a>',
+            webpage, 'uploader', fatal=False)
+
+        description = self._html_search_meta('description', webpage)
+
+        self._sort_formats(formats)
+
+        return {
+            'id': video_id,
+            'formats': formats,
+            'title': title,
+            'view_count': view_count,
+            'like_count': like_count,
+            'dislike_count': dislike_count,
+            'upload_date': upload_date,
+            'description': description,
+            'uploader': uploader,
+        }
+
+
+class TokentubeChannelIE(InfoExtractor):
+    _PAGE_SIZE = 20
+    IE_NAME = 'Tokentube:channel'
+    _VALID_URL = r'https?://(?:www\.)?tokentube\.net/channel/(?P<id>\d+)/[^/]+(?:/videos)?'
+    _TESTS = [{
+        'url': 'https://tokentube.net/channel/3697658904/TokenTube',
+        'info_dict': {
+            'id': '3697658904',
+        },
+        'playlist_mincount': 7,
+    }, {
+        'url': 'https://tokentube.net/channel/3353234420/Linux/videos',
+        'info_dict': {
+            'id': '3353234420',
+        },
+        'playlist_mincount': 20,
+    }, {
+        'url': 'https://tokentube.net/channel/3475834195/Voitontie',
+        'info_dict': {
+            'id': '3475834195',
+        },
+        'playlist_mincount': 150,
+    }]
+
+    def _fetch_page(self, channel_id, page):
+        page += 1
+        videos_info = self._download_webpage(
+            f'https://tokentube.net/videos?p=0&m=1&sort=recent&u={channel_id}&page={page}',
+            channel_id, headers={'X-Requested-With': 'XMLHttpRequest'},
+            note=f'Downloading page {page}', fatal=False)
+        if '</i> Sorry, no results were found.' not in videos_info:
+            for path, media_id in re.findall(
+                    r'<a[^>]+\bhref=["\']([^"\']+/[lv]/(\d+)/\S+)["\'][^>]+>',
+                    videos_info):
+                yield self.url_result(path, ie=TokentubeIE.ie_key(), video_id=media_id)
+
+    def _real_extract(self, url):
+        channel_id = self._match_id(url)
+
+        entries = OnDemandPagedList(functools.partial(
+            self._fetch_page, channel_id), self._PAGE_SIZE)
+
+        return self.playlist_result(entries, channel_id)


Test patch: