# -*- coding: utf-8 -*-
import clr
import sys
import System
import re

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



try:
    sys.path.append(UI.StartupPath+'\Script\Module\dll')
    clr.AddReferenceToFile("EasyScript.dll")

    from EasyScript import *
    zkm = EasyScript()


    UI.UpdateLog("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※")
    UI.UpdateLog("※　　　【免空科學研究所　ｔｉｇｅｒｍａｘ．ｄｙｎｄｎｓ．ｏｒｇ】　　　※")
    UI.UpdateLog("※　　　～㊣　　　　　　　　　ＺＫＭ　　製作分享　　　　　　　㊣～　　　※")
    UI.UpdateLog("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※")
    UI.UpdateLog("【腳本更新】：2013.03.13 panda0305 撰寫腳本",2)
    UI.UpdateLog("【上傳空間】：Sendspace.pl",2)
    UI.UpdateLog("【上傳大小】：750 MB",2)
    UI.UpdateLog("【砍檔時間】：未知",2)
    UI.UpdateLog("【腳本功能】：無帳號上傳",2)
    UI.UpdateLog("【腳本備註】：支援各32/64位元Windows系列",1)
    UI.UpdateLog("=========================================",2)
    UI.UpdateLog("連結至 www.sendspace.pl..",2)
    HTML=HTTP.GetURL("http://www.sendspace.pl/en/")
    PHPSESSID=Simple_Regular('"PHPSESSID": "(.+?)"',HTML,1)
    upload_url=Simple_Regular('upload_url: "(.+?)"',HTML,1)

    if PHPSESSID!=False and upload_url!=False:
        UI.UpdateLog("取得PHPSESSID參數->"+PHPSESSID)
        UI.UpdateLog("取得upload_url參數->"+upload_url)
        UI.UpdateLog("開始上傳....",4)
        HTTP.AddUpParam("Filename",UI.FileName)
        HTTP.AddUpParam("PHPSESSID",PHPSESSID)
        HTTP.AddUpParam("Upload","Submit Query")
        HTTP.AddFileReference("Filedata")
        HTML1=UI.UpFile(upload_url)
        link=Simple_Regular('(\S+)',HTML1,1)
        DownloadLink1="http://www.sendspace.pl/en/files/save/?file_info="+link+"&size="+UI.FileSize
        if DownloadLink1!=False:
            HTML2=HTTP.GetURL(DownloadLink1)
            DownloadLink2=zkm.GetstrToVariable(HTML2,'id="profile_name" onclick="this.select();" class="reg" value="','"',1)
            DeleteLink2=zkm.GetstrToVariable(HTML2,'id="profile_name" onclick="this.select();" class="reg" value="','"',2)
            UI.UpdateLog("連結1為真實連結："+DownloadLink2,3)
            UI.UpdateLog("連結2為刪檔連結："+DeleteLink2,3)
            UI.AddShareLink(DownloadLink2+"^"+DeleteLink2)
            UI.ProceSuccess()
        else:
            UI.UpdateLog("上傳失敗無法取得連結",3)
            UI.ProceFalse()
    else:
        UI.UpdateLog("發生錯誤!無法取得必要參數值",3)
        UI.ProceFalse()
except Exception as e:
    UI.UpdateLog(e.message)
    UI.ProceFalse()