]> git.aero2k.de Git - urlbot-v3.git/commitdiff
tokenization did require changes as httpsyoutu is not exactly the searched token
authorThorsten <mail@aero2k.de>
Thu, 11 Feb 2021 16:40:46 +0000 (17:40 +0100)
committerThorsten <mail@aero2k.de>
Thu, 11 Feb 2021 16:40:46 +0000 (17:40 +0100)
distbot/plugins/url.py
distbot/plugins/youtube.py

index 6b41dc98a4a18d2379b0f9abef2402e1f3667284..802dee27a2015fd60fbece959f3a4b31653ac04a 100644 (file)
@@ -89,6 +89,9 @@ class URLResolver(Worker):
 
         out = []
         for url in result[:10]:
+            if "https://youtu" in url:
+                # does not work, another plugins does the job.
+                continue
             if any([re.match(b, url) for b in url_blacklist]):
                 logger.info('url blacklist match for ' + url)
                 break
index 0587a8fbf77cdd04f00deef3264295cd3cfc95c3..4ea3aa03052fe909bba524d956452bda1c033f41 100644 (file)
@@ -1,3 +1,4 @@
+import logging
 import re
 
 import requests
@@ -5,16 +6,11 @@ import requests
 from distbot.bot.worker import Worker
 from distbot.common.action import Action
 
+log = logging.getLogger(__name__)
+
 
 class Youtube(Worker):
-    """
-    # approach 1: https://www.googleapis.com/youtube/v3/videos?part=snippet&id={YOUTUBE_VIDEO_ID}&fields=items(id,snippet)&key={YOUR_API_KEY}
-    # approach 2 (without key): https://www.youtube.com/oembed?url=http://youtube.com/watch?v={YOUTUBE_VIDEO_ID}&format=json
-    """
-    binding_keys = [
-        "#.youtube.#", "#.youtu.#",
-        "#.youtube.#.nospoiler.#", "#.youtu.#.nospoiler.#",
-    ]
+    binding_keys = Worker.CATCH_ALL
     description = "resolves titles of posted youtube URLs"
 
     URL_TEMPLATE = "https://www.youtube.com/oembed?url=http://youtube.com/watch?v={YOUTUBE_VIDEO_ID}&format=json"
@@ -26,6 +22,7 @@ class Youtube(Worker):
         regex = re.compile(regex)
         result = regex.search(body)
         if not result:
+            log.warning("Could not extract youtube video ID from: %s", body)
             return None
         else:
             return result.groupdict().get("youtubeid", None)
@@ -36,6 +33,11 @@ class Youtube(Worker):
         return response.json().get("title")
 
     def parse_body(self, msg):
+
+        result = re.findall(r'(https?://youtu[^\s>]+)', msg["body"])
+        if not result or "(nospoiler)" in msg["body"]:
+            return
+
         try:
             youtube_id = self.get_youtube_id_from_url(msg["body"])
             if not youtube_id: