#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import xbmc,xbmcgui
from os import getcwd
from os.path import join
from sys import path
from resources.lib import common
from resources.lib.handler.requestHandler import cRequestHandler
from resources.lib.handler.ParameterHandler import ParameterHandler
path.append(join(common.addonPath, "resources", "lib"))
path.append(join(common.addonPath, "resources", "lib", "handler"))
import cfscrape

USER_AGENT = 'Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0'

def _getRequestHandler(sUrl, cookie):
    oRequest = cRequestHandler(sUrl)
    oRequest.addHeaderEntry('User-Agent', USER_AGENT)
    oRequest.addHeaderEntry('Cookie', cookie)
    return oRequest

def _getHtmlContent(URL_MAIN):
    params = ParameterHandler()
	
    scrapper = cfscrape.CloudflareScraper()
    cookie_value, user_agent = scrapper.get_cookie_string(URL_MAIN,USER_AGENT)
	
    params.setParam('cfCookie', cookie_value)
    params.setParam('sUrl', URL_MAIN)
	
    entryUrl = params.getValue('sUrl')
    sHtmlContent = _getRequestHandler(entryUrl, params.getValue('cfCookie')).request()
	
    return sHtmlContent