# -*- coding: utf-8 -*-

import clr
import sys
import os
import System
import re
import urllib
import urllib2
import httplib
import random

#簡易正規表達式
def Simple_Regular(RegularStr,String,index=0):
    STR=re.search(RegularStr,String)
    if STR is None:
        return False
    else:
        return STR.group(index)

#結束
def end():
    if Status==True:
        UI.UpdateLog("下載成功",3)
        UI.ProceSuccess()
        #exit()
    else:
        UI.UpdateLog("下載失敗",2)
        UI.ProceFalse()

#超過50MB以上檔案需要驗證碼下載
def BigFileDown():
    UI.UpdateLog("取得驗證碼")
    HTML = HTTP.GetURL("http://www.google.com/recaptcha/api/challenge?k="+Recaptcha+"&ajax=1&cachestop="+str(random.random()))
    challenge = Simple_Regular("challenge : '([^\']+)",HTML,1)
    UI.UpdateLog("http://www.google.com/recaptcha/api/image?c="+challenge)
    UI.PromptCaptcha("http://www.google.com/recaptcha/api/image?c="+challenge)
    UI.UpdateLog("輸入驗證碼："+UI.CaptchaValue)
    #送出驗證碼
    HTTP.AddPostParam("challenge",challenge)
    HTTP.AddPostParam("response",UI.CaptchaValue)
    HTTP.AddPostParam("shortencode",shortencode)
    HTML=HTTP.PostURL(host+"/rest/captcha/test",HTTP.PostData)
    #UI.UpdateLog(HTML)
    if re.search("true",HTML) != None:
        UI.UpdateLog("開始下載...",3)
        Status=UI.DownloadURL(host + dllocation)
        end()

#未超過50MB大小檔案不需要驗證碼下載
def SmallFileDownload():
    ss = s.replace("document.getElementById('dlbutton').href =","return ").replace("document.getElementById('omg').getAttribute('class')","1")
    UI.UpdateLog(ss)
    JScode = "function c(){ " + ss + "}"
    UI.UpdateLog(JScode)

    Staus=JSLangueScript.Source(JScode)
    if Staus==True:
        ReturnStr=JSLangueScript.Exec('c',())
        UI.UpdateLog("JScript無參數傳遞："+str(ReturnStr))
    else:
        UI.UpdateLog(JSLangueScript.Error_Message)
        UI.UpdateLog(host + str(ReturnStr))
    
    Status=UI.DownloadURL(host + str(ReturnStr))
    if Status==True:
        end()

UI.UpdateLog("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※")
UI.UpdateLog("※　　　【免空科學研究所　ｔｉｇｅｒｍａｘ．ｄｙｎｄｎｓ．ｏｒｇ】　　　※")
UI.UpdateLog("※　　　～㊣　　　　　　　ｔｉｇｅｒ　　製作分享　　　　　　　㊣～　　　※")
UI.UpdateLog("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※")
UI.UpdateLog("【腳本更新】：2013.04.20",2)
UI.UpdateLog("【下載空間】：zippyshare(免空)",2)
UI.UpdateLog("【砍檔時間】：90",2)
UI.UpdateLog("【腳本功能】：無帳號下載，大檔驗證碼下載/小檔直接下載",2)
UI.UpdateLog("※連結至 zippyshare.com...",4)
HTML = HTTP.GetURL(UI.Link)
UI.UpdateLog(HTML)

UI.FileName = Simple_Regular('addthis:title="([^\"]+)',HTML,1)
host = Simple_Regular('og:url" content="([^com]+com)',HTML,1)
UI.UpdateLog("〔下載 host：" + host + "〕〔檔案名稱：" + UI.FileName + "〕")

Recaptcha = Simple_Regular('Recaptcha\.create\("([^\"]+)',HTML,1)
if Recaptcha != False:
    UI.UpdateLog("需要驗證碼下載",3)
    shortencode = Simple_Regular("shortencode: '([^\']+)",HTML,1)
    UI.UpdateLog("〔Recaptcha："+ Recaptcha + "〕〔shortencode：" + shortencode+"〕")
    dllocation = Simple_Regular("document.location = '([^\']+)",HTML,1)
    UI.UpdateLog("下載網址："+ dllocation)
    BigFileDown()
else:
    UI.UpdateLog("不需要驗證碼下載",4)
    s = Simple_Regular('javascript\">\s+(.+omg[^\<]+)<',HTML,1)
    #UI.UpdateLog(s)
    SmallFileDownload()

UI.ProceFalse()