# Path: musicazoo/lib/watch_dl.py
# class WatchCartoonOnlineIE(InfoExtractor):
#     #IE_NAME = u'WatchCartoonOnline'
#     _VALID_URL = r'(?:http://)?(?:www\.)?watchcartoononline\.com/([^/]+)'
# 
#     def _real_extract(self,url):
#         o=urllib2.build_opener(urllib2.HTTPHandler).open
# 
#         mobj = re.match(self._VALID_URL, url)
#         video_id = mobj.group(1)
#         webpage = o('http://www.watchcartoononline.com/{0}'.format(video_id)).read()
# 
#         title_escaped = re.search(r'<h1.*?>(.+?)</h1>',webpage).group(1)
#         title = html_parser.unescape(title_escaped)
# 
#         video_url = re.search(r'<iframe id="(.+?)0" (.+?)>', webpage).group()
#         video_url = re.search('src="(.+?)"', video_url).group(1).replace(' ','%20')
# 
#         params = urllib.urlencode({'fuck_you':'','confirm':'Click Here to Watch Free!!'})
#         request = urllib2.Request(video_url,params)
#         video_webpage = o(request).read()
#         final_url =  re.findall(r'file: "(.+?)"', video_webpage)
#         redirect_url=urllib.unquote(final_url[-1]).replace(' ','%20')
#         flv_url = o(redirect_url).geturl()
# 
#         return {'url':flv_url, 'title':title, 'id': video_id}


import os
import socket
import tempfile
import time
import youtube_dl 
import Queue
import shmooze.lib.packet as packet
import musicazoo.lib.vlc as vlc
import threading
import urllib2
import sys
from shmooze.modules import JSONParentPoller
from musicazoo.lib.watch_dl import WatchCartoonOnlineIE
from youtube_dl.compat import compat_cookiejar, compat_urllib_request
from youtube_dl.utils import make_HTTPS_handler, YoutubeDLHandler
            # General configuration
            tf=tempfile.NamedTemporaryFile(delete=False)
            tf.close()
            self.cookies=tf.name
            jar = compat_cookiejar.MozillaCookieJar(self.cookies)
            cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
            proxies = compat_urllib_request.getproxies()
            proxy_handler = compat_urllib_request.ProxyHandler(proxies)
            https_handler = make_HTTPS_handler(params)
            ydlh = YoutubeDLHandler(params)
            opener = compat_urllib_request.build_opener(https_handler, proxy_handler, cookie_processor, ydlh)
            compat_urllib_request.install_opener(opener)

            y=youtube_dl.YoutubeDL({'outtmpl':u'','skip_download':True}, auto_init=False) # empty outtmpl needed due to weird issue in youtube-dl
            y.add_info_extractor(WatchCartoonOnlineIE())
            y.add_default_info_extractors()

            try:
                info=y.extract_info(url,download=False)
            except Exception:
                raise
                self.status='invalid'
                self.queue.removeMeAsync(self.uid) # Remove if possible # TODO error here
                self.ready.release()
                return False

            jar.save()

            if 'entries' in info:
                vinfo=info['entries'][0]

# Path: musicazoo/lib/watch_dl.py
# class WatchCartoonOnlineIE(InfoExtractor):
#     #IE_NAME = u'WatchCartoonOnline'
#     _VALID_URL = r'(?:http://)?(?:www\.)?watchcartoononline\.com/([^/]+)'
# 
#     def _real_extract(self,url):
#         o=urllib2.build_opener(urllib2.HTTPHandler).open
# 
#         mobj = re.match(self._VALID_URL, url)
#         video_id = mobj.group(1)
#         webpage = o('http://www.watchcartoononline.com/{0}'.format(video_id)).read()
# 
#         title_escaped = re.search(r'<h1.*?>(.+?)</h1>',webpage).group(1)
#         title = html_parser.unescape(title_escaped)
# 
#         video_url = re.search(r'<iframe id="(.+?)0" (.+?)>', webpage).group()
#         video_url = re.search('src="(.+?)"', video_url).group(1).replace(' ','%20')
# 
#         params = urllib.urlencode({'fuck_you':'','confirm':'Click Here to Watch Free!!'})
#         request = urllib2.Request(video_url,params)
#         video_webpage = o(request).read()
#         final_url =  re.findall(r'file: "(.+?)"', video_webpage)
#         redirect_url=urllib.unquote(final_url[-1]).replace(' ','%20')
#         flv_url = o(redirect_url).geturl()
# 
#         return {'url':flv_url, 'title':title, 'id': video_id}


# Path: musicazoo/modules/youtube.py
import os
import socket
import tempfile
import time
import youtube_dl 
import Queue
import shmooze.lib.packet as packet
import musicazoo.lib.vlc as vlc
import threading
import urllib2
import sys
from shmooze.modules import JSONParentPoller
from musicazoo.lib.watch_dl import WatchCartoonOnlineIE
from youtube_dl.compat import compat_cookiejar, compat_urllib_request
from youtube_dl.utils import make_HTTPS_handler, YoutubeDLHandler

            # General configuration
            tf=tempfile.NamedTemporaryFile(delete=False)
            tf.close()
            self.cookies=tf.name
            jar = compat_cookiejar.MozillaCookieJar(self.cookies)
            cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
            proxies = compat_urllib_request.getproxies()
            proxy_handler = compat_urllib_request.ProxyHandler(proxies)
            https_handler = make_HTTPS_handler(params)
            ydlh = YoutubeDLHandler(params)
            opener = compat_urllib_request.build_opener(https_handler, proxy_handler, cookie_processor, ydlh)
            compat_urllib_request.install_opener(opener)

            y=youtube_dl.YoutubeDL({'outtmpl':u'','skip_download':True}, auto_init=False) # empty outtmpl needed due to weird issue in youtube-dl
            y.add_info_extractor(WatchCartoonOnlineIE())
            y.add_default_info_extractors()

            try:
                info=y.extract_info(url,download=False)
            except Exception:
                raise
                self.status='invalid'
                self.queue.removeMeAsync(self.uid) # Remove if possible # TODO error here
                self.ready.release()
                return False

            jar.save()

            if 'entries' in info:
                vinfo=info['entries'][0]

            else: