# -*- coding: UTF-8 -*-
# /*
# *      Copyright (C) 2016 ivars777
# *
# *
# *  This Program is free software; you can redistribute it and/or modify
# *  it under the terms of the GNU General Public License as published by
# *  the Free Software Foundation; either version 2, or (at your option)
# *  any later version.
# *
# *  This Program is distributed in the hope that it will be useful,
# *  but WITHOUT ANY WARRANTY; without even the implied warranty of
# *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# *  GNU General Public License for more details.
# *
# *  You should have received a copy of the GNU General Public License
# *  along with this program; see the file COPYING.  If not, write to
# *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
# *  http://www.gnu.org/copyleft/gpl.html
# *
# */

import re,os,sys
import json
try:
    import util
except:
    pp = os.path.dirname(os.path.abspath(__file__))
    sys.path.insert(0,os.sep.join(pp.split(os.sep)[:-1]))
    import util
import urllib2
import requests
try:
    from requests.packages.urllib3.exceptions import InsecureRequestWarning
    requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
except:
    pass
#from aadecode import AADecoder

if __name__ <> "__main__":
    __name__ = 'hqq'

def supports(url):
    m = re.search(r"https?://hdgo\.\w+/(.+?)$", url, re.DOTALL)
    if m:
        return True
    else:
        return False

def resolve(url):
    HTTP_HEADER = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
        'Accept-Encoding': 'none',
        'Accept-Language': 'en-US,en;q=0.8',
        'Referer': url}  # 'Connection': 'keep-alive'
    streams = []
    m = re.search(r"https?://hdgo\.\w+/(.+?)$", url, re.DOTALL)
    vid=m.group(1)
    url2 = "http://couber.be/"+vid
    r = requests.get(url2,headers=HTTP_HEADER)
    if r.status_code <> 200:
        return streams
    m = re.search('<iframe src="([^"]+)"', r.content, re.DOTALL)
    if not m: return streams
    url3 = m.group(1)
    HTTP_HEADER["Rererer"] = url2
    r = requests.get(url3,headers=HTTP_HEADER)
    m = re.search(r"else{\s+setFlash\('([^']+)'\);", r.content, re.DOTALL)
    if not m: return streams
    q = ["1080p","720p","480p","360p"]
    for i,ss in enumerate(m.group(1).split(",")):
        s = ss.split(" or ")
        if not s[0]: continue
        stream = util.item()
        stream["url"] = s[0]
        stream["name"] = s[0]
        stream["quality"] = q[i]
        streams.append(stream)
    return streams


if __name__ == "__main__":

    from subprocess import call
    url = "http://hdgo.cc/video/t/Qrz0riUvA65GtkTpDvmlD9TBOn56HSm2/127280/"
    url = "http://hdgo.cc/video/t/Qrz0riUvA65GtkTpDvmlD9TBOn56HSm2/34879/"
    streams = resolve(url)
    if not streams:
        print "No streams found"
        sys.exit()
    for s in streams:
        print s
    util.play_video(streams)


    #print streams[0]["url"]
    #call([r"gst-launch-1.0.exe",'uri="%s""'%streams[0]["url"]])
    pass
