import requests
import weechat as wc
import re

wc.register('youtube.py', 'snowfag', '1.0', 'BSD-2c', 'Youtube video title announcer', '', '')

def config(*args, **kwargs):
    if not wc.config_is_set_plugin('channels'):
        wc.config_set_plugin('channels', '.*')
    if not wc.config_is_set_plugin('api_key'):
	wc.config_set_plugin('api_key', 'not_set')
    if not wc.config_is_set_plugin('other_bots'):
        wc.config_set_plugin('other_bots', 'not_set')
    return wc.WEECHAT_RC_OK

def privmsg(data, signal, signal_data):
    (server, signal) = signal.split(",")
    channels = wc.config_get_plugin('channels').replace(',', '|')
    api_key = wc.config_get_plugin('api_key')
    bots_list = wc.config_get_plugin('other_bots').split(",")
    details = wc.info_get_hashtable("irc_message_parse", {"message": signal_data, "server": server})
    youtube_regex_match = re.compile(r'(.*https?://)?(www\.)?(youtube|youtu|youtube-nocookie)\.(com|be)/(watch\?v=|embed/|v/|.+\?v=)?([^&=%\?]{11})(.*)').match(details['text'])
    buffer_name = details['channel']
    buffer_pointer = wc.info_get("irc_buffer", "%s,%s" % (server, buffer_name))
    channels_regex = re.compile(r'(%s)' % (channels), re.I)
    bots_exist = False
    if channels_regex.match(buffer_name) and youtube_regex_match:
        if not bots_list == "not_set":
            for other_bots in bots_list:
                bots_test = wc.nicklist_search_nick(buffer_pointer, "", other_bots)
                if bots_test:
                    bots_exist = True
	if not bots_exist:
            if not api_key == "not_set":
                vid_id = youtube_regex_match.group(6)
                r = requests.get('https://www.googleapis.com/youtube/v3/videos/?id=%s&part=snippet&key=%s' % (vid_id, api_key))
                try:
                    vid_title = r.json()['items'][0]['snippet']['title'].encode('utf-8')
                except:
                    vid_title = "Error getting video title."
                wc.command("", r"/msg %s Youtube video title: %s" % (buffer_name, vid_title))
            else:
                wc.command("", r"/msg %s Youtube api key not set." % (buffer_name))
    return wc.WEECHAT_RC_OK
wc.hook_signal("*,irc_in_privmsg", "privmsg", "")
config()

@@ -1648,16 +1648,18 @@
        r
+vt
  = reque
@@ -1716,18 +1716,19 @@
 eos/?id=
-%25s
+%7B0%7D
 &part=sn
@@ -1741,14 +1741,158 @@
 key=
-%25s' %25 
+%7B1%7D'.format(vid_id, api_key))%0A                rvc = requests.get('https://www.googleapis.com/youtube/v3/videos/?id=%7B0%7D&part=statistics&key=%7B1%7D'.format
 (vid
@@ -1960,16 +1960,18 @@
 itle = r
+vt
 .json()%5B
@@ -2018,16 +2018,98 @@
 utf-8')%0A
+                    vid_views = rvc.json()%5B'items'%5D%5B0%5D%5B'statistics'%5D%5B'viewCount'%5D%0A
         
@@ -2177,21 +2177,20 @@
 g video 
-title
+info
 .%22%0A     
@@ -2226,38 +2226,46 @@
 msg 
-%25s 
+%7B0%7D %5B
 Youtube
- video title: %25s%22 %25 
+%5D %7B1%7D %7C Views: %7B2%7D%22.format
 (buf
@@ -2283,16 +2283,27 @@
 id_title
+, vid_views
 ))%0A     
@@ -2353,18 +2353,19 @@
  r%22/msg 
-%25s
+%7B0%7D
  Youtube
@@ -2382,19 +2382,23 @@
 ot set.%22
- %25 
+.format
 (buffer_

change formating and add view count