From e0b31dafc751be8ea91ae9318f4a9c1cefc02ce3 Mon Sep 17 00:00:00 2001
From: Ronni <github@celler.org>
Date: Mon, 20 Feb 2023 20:39:31 +0100
Subject: [PATCH] [erocast] Add new extractor for erocast.me

---
 youtube_dl/extractor/erocast.py    | 48 ++++++++++++++++++++++++++++++
 youtube_dl/extractor/extractors.py |  2 ++
 2 files changed, 50 insertions(+)
 create mode 100644 youtube_dl/extractor/erocast.py

diff --git a/youtube_dl/extractor/erocast.py b/youtube_dl/extractor/erocast.py
new file mode 100644
index 000000000..8ece447a9
--- /dev/null
+++ b/youtube_dl/extractor/erocast.py
@@ -0,0 +1,48 @@
+# coding: utf-8
+from __future__ import unicode_literals
+
+from .common import InfoExtractor
+
+
+class ErocastIE(InfoExtractor):
+    IE_NAME = 'erocast'
+    _VALID_URL = r'https?://(?:www\.)?erocast\.me/track/(?P<id>[0-9]+)/(?P<display_id>[0-9a-zA-Z_-]+)'
+    _TEST = {
+        'url': 'https://erocast.me/track/6508/piano-sample-by-ytdl',
+        'md5': '6764726b2d19161e93c9cf3a9a69800a',
+        'info_dict': {
+            'id': '6508',
+            'ext': 'mp4',
+            'title': 'Piano sample by ytdl',
+            'uploader': 'ytdl',
+        }
+    }
+
+    def _real_extract(self, url):
+        video_id = self._match_id(url)
+        webpage = self._download_webpage(url, video_id)
+
+        # The Song data is in a script tag with the following format:
+        # (see https://github.com/ytdl-org/youtube-dl/issues/31203#issuecomment-1259867716)
+        searchPattern = r'<script>var song_data_' + str(video_id) + r' = (.+?)<\/script>'
+        jsonString = self._html_search_regex(searchPattern, webpage, 'data')
+
+        # The data is in JSON format, so we convert the JSON String to a python object
+        # and read the data from this json object
+        jsonObject = self._parse_json(jsonString, None, fatal=False)
+        audio_url = jsonObject['stream_url']
+        title = jsonObject['title']
+        user_name = jsonObject['user']['name']
+
+        # The audio url is a m3u8 playlist, so we extract the audio url from this playlist
+        formats = []
+        formats.extend(self._extract_m3u8_formats(
+            audio_url, video_id, 'mp4', 'm3u8_native',
+            m3u8_id='hls', fatal=False))
+
+        return {
+            'id': video_id,
+            'formats': formats,
+            'title': title,
+            'uploader': user_name,
+        }
diff --git a/youtube_dl/extractor/extractors.py b/youtube_dl/extractor/extractors.py
index 3a87f9e33..1b6ef599c 100644
--- a/youtube_dl/extractor/extractors.py
+++ b/youtube_dl/extractor/extractors.py
@@ -353,6 +353,8 @@ from .elpais import ElPaisIE
 from .embedly import EmbedlyIE
 from .engadget import EngadgetIE
 from .eporner import EpornerIE
+from .erocast import ErocastIE
+
 from .eroprofile import EroProfileIE
 from .escapist import EscapistIE
 from .espn import (