#!/usr/bin/env python

"""
ClicknUpload.link Service Code

Returns ssl error, cannot use code :(
"""

HTTP_HEADERS = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}

####################################################################################################
def MetadataObjectForURL(url):

    try:
        html = HTML.ElementFromURL(url)
    except Exception as e:
        Log.Error(str(e))
        raise Ex.MediaNotAvailable

    title = html.xpath('//input[@name="fname"]/@value')
    if not title:
        raise Ex.MediaExpired

    return VideoClipObject(
        title=title[0].strip(),
        thumb='http://i.imgur.com/75YO83o.jpg',
        source_title='ClicknUpload'
        )

####################################################################################################
def MediaObjectsForURL(url):

    return [
        MediaObject(
            video_resolution='sd',
            audio_channels=2,
            optimized_for_streaming=True,
            parts=[
                PartObject(key=Callback(PlayVideo, url=url))
                ]
            )
        ]

####################################################################################################
@indirect
def PlayVideo(url, **kwargs):

    try:
        base_html = HTML.ElementFromURL(url, headers=HTTP_HEADERS)
    except:
        raise Ex.MediaNotAvailable

    html = get_post_html(url, base_html)
    if (len(html) is not None):
        for node in html.xpath('//button[@class="downloadbtn"]'):
            r = Regex(r'window\.open\([\'\"](http[^\'\"]+)[\'\"]').search(node.get('onclick'))
            if r:
                Log.Debug('* PlayVideo URL = \"%s\"' %r.group(1))
                return IndirectResponse(VideoClipObject, key=r.group(1))

    raise Ex.MediaNotAvailable

####################################################################################################
def get_post_html(url, html):
    data = dict()
    for item in html.xpath('//input'):
        data.update({item.get('name'): item.get('value')})

    if bool(data):
        return get_post_html(url, HTML.ElementFromString(HTTP.Request(url, headers=HTTP_HEADERS, values=data).content))
    else:
        return html

    return False
