michael-lazar / rtv

Browse Reddit from your terminal
MIT License
4.64k stars 273 forks source link

add Twitch mime parser #694

Open geeseven opened 5 years ago

geeseven commented 5 years ago

Is your feature request related to a problem? Please describe. Twitch streams or clips act differently than other video links as they can only be opened in a browser.

Describe the solution you'd like It would be nice to launch mpv or vlc for Twitch streams or clips.

Describe alternatives you've considered Copy/paste is a workaround.

Additional context Seeing that youtube-dl has support for Twitch, I have a working branch that uses 'video/x-youtube' like Vimeo. I would be more than happy to make a pull request.

goggle commented 5 years ago

I think it would be helpful to have examples of all the possible Twitch URLs.

geeseven commented 5 years ago

I am currently using the following regex:

https?://(clips\.|www\.|m\.)?twitch\.tv/[^.]+$
geeseven commented 5 years ago

Looking over the current Twitch extractor for youtube-dl, looks like we would also need to add the go and player subdomains.

geeseven commented 5 years ago

Given #696, it does not make sense to submit a pull request. For anyone interested, here is what I did:

$ git diff rtv/mime_parsers.py
diff --git a/rtv/mime_parsers.py b/rtv/mime_parsers.py
index 9d7eae8..3a51444 100644
--- a/rtv/mime_parsers.py
+++ b/rtv/mime_parsers.py
@@ -320,6 +320,17 @@ class StreamableMIMEParser(OpenGraphMIMEParser):
     """
     pattern = re.compile(r'https?://(www\.)?streamable\.com/[^.]+$')

+class TwitchMIMEParser(BaseMIMEParser):
+    """
+    Twitch videos can be streamed with vlc or downloaded with youtube-dl.
+    Assign a custom mime-type so they can be referenced in mailcap.
+    """
+    pattern = re.compile(r'https?://(clips|go|m|player|www)\.?twitch\.tv/[^.]+$')
+
+    @staticmethod
+    def get_mimetype(url):
+        return url, 'video/x-youtube'
+

 class LiveleakMIMEParser(BaseMIMEParser):
     """
@@ -479,6 +490,7 @@ parsers = [
     GiphyMIMEParser,
     ImgflipMIMEParser,
     LivememeMIMEParser,
+    TwitchMIMEParser,
     MakeamemeMIMEParser,
     WorldStarHipHopMIMEParser,
     GifvMIMEParser,
ajakk commented 5 years ago

Hi geeseven, I have a fork of rtv called tuir on Gitlab here and I would love to include your code here if you submit a pull request over there