import re
import urllib

import isodate
import requests

import smartbot
from smartbot import utils
from smartbot.exceptions import *
from smartbot.formatting import *


REGEX = r"(?:https?://)?(?:www\.)?(?:youtu\.?be|listenonrepeat)(?:\.com)?/(?:watch/?\?v=)?([^\s]+)"

class Plugin(smartbot.Plugin):
    """Get information about posted YouTube videos."""
    names = ["youtube", "utube"]

    def __init__(self, key):
        self.key = key

    def _get_reply(self, i, video):
        title = video["snippet"]["title"]
        channelTitle = video["snippet"]["channelTitle"]
        duration = isodate.parse_duration(video["contentDetails"]["duration"])
        views = video["statistics"]["viewCount"]
        likes = video["statistics"]["likeCount"]
        dislikes = video["statistics"]["dislikeCount"]
        thumbnail = video["snippet"]["thumbnails"]["default"]["url"]

        return "{}: {} by {} | {} | {} {} {}".format(
            self.bot.format("[{}]".format(i), Style.bold),
            title,
            channelTitle,
            duration,
            views,
            self.bot.format(likes, Colour.fg_green),
            self.bot.format(dislikes, Colour.fg_red)
        )

    def pre_on_message(self, handler, msg):
        if re.findall(REGEX, msg["message"], re.IGNORECASE):
            handler.disable_plugin("websites")

    def on_message(self, msg, reply):
        match = re.findall(REGEX, msg["message"], re.IGNORECASE)
        for i, video_id in enumerate(match):
            url = "https://www.googleapis.com/youtube/v3/videos"
            payload = {
                "key": self.key,
                "id": video_id,
                "part": ",".join(["contentDetails", "snippet", "statistics"])
            }

            s = utils.web.requests_session()
            res = s.get(url, params=payload).json()
            if res["items"]:
                video = res["items"][0]
                reply(self._get_reply(i, video))

    def on_help(self):
        return "Sends information about YouTube URLs posted."

@@ -174,18 +174,16 @@
 EX = r%22(
-?:
 https?:/
@@ -190,18 +190,16 @@
 /)?(
-?:
 www%5C.)?(
 ?:yo
@@ -194,18 +194,16 @@
 www%5C.)?(
-?:
 youtu%5C.?
@@ -225,34 +225,15 @@
 at)(
-?:
 %5C.com)?
-/(?:watch/?%5C?v=)?
 (%5B%5E%5C
@@ -977,39 +977,107 @@
     
-title,%0A            channelTitle
+self.bot.format(title, Style.underline),%0A            self.bot.format(channelTitle, Style.underline)
 ,%0A  
@@ -1244,102 +1244,476 @@
 def 
-pre_on_message(self, handler, msg):%0A        if re.findall(REGEX, msg%5B%22message%22%5D, re.IGNORECASE
+_find_video_ids(self, text):%0A        video_ids = %5B%5D%0A        matches = re.findall(REGEX, text, re.IGNORECASE)%0A        for match in matches:%0A            try:%0A                url = urllib.parse.urlparse(%22%22.join(match))%0A                video_ids.extend(urllib.parse.parse_qs(url.query)%5B%22v%22%5D)%0A            except (ValueError, KeyError):%0A                pass%0A        return video_ids%0A%0A    def pre_on_message(self, handler, msg):%0A        if self._find_video_ids(msg%5B%22message%22%5D
 ):%0A 
@@ -1809,63 +1809,55 @@
     
-match = re.findall(REGEX, msg%5B%22message%22%5D, re.IGNORECASE
+video_ids = self._find_video_ids(msg%5B%22message%22%5D
 )%0A  
@@ -1891,21 +1891,25 @@
 umerate(
-match
+video_ids
 ):%0A     

Improve URL detection in YouTube plugin