# -*- coding: utf-8 -*-
import re
import HTMLParser

#泛用函數

#通用變數
SpaceURL   = "http://netload.in/"

#主程序函數
def Main(ModeSelect):
    ModeNotice   = ["上傳","下載","使用"]
    Modefunction = ["無帳號上傳","一般下載","刪除檔案"]

    UI.UpdateLog("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※")
    UI.UpdateLog("※　　　【免空科學研究所　ｔｉｇｅｒｍａｘ．ｄｙｎｄｎｓ．ｏｒｇ】　　　※")
    UI.UpdateLog("※　　　～㊣　　　　　　　陽だまり  　　製作分享　　　　　　　㊣～　　　※")
    UI.UpdateLog("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※")
    UI.UpdateLog("　")
    UI.UpdateLog("【腳本更新】：2013.03.12 陽だまり 修正原始冏rz內附腳本")
    UI.UpdateLog("【"+ModeNotice[ModeSelect]+"空間】："+SpaceURL)
    UI.UpdateLog("【"+ModeNotice[ModeSelect]+"大小】：2,000 MB")
    UI.UpdateLog("【砍檔時間】：未知")
    UI.UpdateLog("【腳本功能】："+Modefunction[ModeSelect])
    UI.UpdateLog("【腳本備註】：支援各32/64位元Windows系列")

    ChangeMode = {
        0:(lambda :StartUpload()),
        1:(lambda :StartDownload()),
        2:(lambda :StartDelete())
    }
    ChangeMode[ModeSelect]()


def StartUpload():
    UI.UpdateLog("開始上傳程序。")
    HTML = HTTP.GetURL(SpaceURL)

    ReResault = re.search("action=\"(.*?upload.php)",HTML)
    if ReResault == None:
        UI.UpdateLog("找不到上傳網址，上傳程序中斷。",3)
        UI.ProceFalse()
    else:
        UploadUrl = ReResault.group(1)
        UI.UpdateLog("找到上傳網址:"+UploadUrl+"，開始上傳檔案。")
        HTTP.AddUpParam("upload_hash","")
        HTTP.AddFileReference("file")
        HTML = UI.UpFile(UploadUrl)
        ReResault = re.search("Files were successfully uploaded",HTML)
        if ReResault == None:
            UI.UpdateLog("檔案上傳失敗，請連絡腳本寫作人員。",3)
            UI.ProceFalse()
        else:
            UI.UpdateLog("檔案上傳成功，相關連結如下：")
            ReResault   = re.search("download link.*\n.*?Upload_Options_Link\" href=\"(.*?)\"",HTML)
            DownloadUrl = ReResault.group(1)
            ReResault   = re.search(r"deletion link.*\n.*?Upload_Options_Link\" href=\"(.*?)\"",HTML)
            DeleteUrl   = ReResault.group(1)
            UI.UpdateLog("下載連結："+DownloadUrl)
            UI.UpdateLog("刪除連結："+DeleteUrl)
            UI.AddShareLink(DownloadUrl+"^"+ DeleteUrl)
            UI.ProceSuccess()


def StartDownload():
    UI.UpdateLog("開始下載程序。")
    HTML = HTTP.GetURL(UI.Link)
    ReResault = re.search("\"whitelink\" href=\"(.*?)\"",HTML)
    if ReResault == None:
        UI.UpdateLog("找不到下載連結，請使用瀏覽器確認檔案是否正常。",3)
        UI.ProceFalse()
    else:
        DecodeHtmlCode = HTMLParser.HTMLParser()
        DownloadUrl    = SpaceURL + DecodeHtmlCode.unescape(ReResault.group(1))
        HTML = HTTP.GetURL(DownloadUrl)
        ReResault = re.search("action=\"(.*?)\"",HTML)
        if ReResault == None:
            UI.UpdateLog("找不到下載正確的下載表單，請使用瀏覽器確認檔案是否正常。",3)
            UI.ProceFalse()
        else:
            DownloadUrl = SpaceURL + ReResault.group(1)
            ReResault   = re.search("src=\"(share.*?)\"",HTML)
            CaptchaUrl  = SpaceURL + ReResault.group(1)
            # UI.UpdateLog(CaptchaUrl)
            # UI.PromptCaptcha(CaptchaUrl)
            ReResault   = re.search("\"file_id\".*?value=\"(.*?)\"",HTML)
            HTTP.AddPostParam("file_id",ReResault.group(1))
            # HTTP.AddPostParam("captcha_check",UI.CaptchaValue)
            HTTP.AddPostParam("captcha_check","7878")
            HTTP.AddPostParam("start","")
            HTML = HTTP.PostURL(DownloadUrl,HTTP.PostData)
            ReResault   = re.search("\"Orange_Link\" href=\"(.*?)\"",HTML)
            if ReResault == None:
                UI.UpdateLog("驗証碼輸入錯誤。",3)
                UI.ProceFalse()
            else:
                UI.Sleep(20,True)
                RealFileLink = ReResault.group(1)
                UI.UpdateLog("找到檔案下載連結："+RealFileLink)
                UI.UpdateLog("開始下載檔案。")
                DownloadStatus = UI.DownloadURL(RealFileLink)
                if DownloadStatus==True:
                    UI.UpdateLog("檔案下載完成。",3)
                    UI.ProceSuccess()
                else:
                    UI.UpdateLog("檔案下載失敗，請使用瀏覽器確認檔案是否正常。",3)
                    UI.ProceFalse()

def StartDelete():
    UI.UpdateLog("開始刪除檔案程序。")
    HTML = HTTP.GetURL(UI.Link)
    ReResault = re.search("successfully deleted",HTML)
    if ReResault == None:
        UI.UpdateLog("該檔案可能已刪除或者不存在，可使用瀏覽器再次確認。")
        UI.ProceSuccess()
    else:
        UI.UpdateLog("檔案刪除完成。")
        UI.ProceSuccess()

try:
    GetLink = UI.Link
    # 進入下載、刪除模式
    ReResault = re.search("netload\.in",GetLink)
    if ReResault == None:
        UI.UpdateLog("該腳本不支援非"+SpaceURL+"空間的連結，請確認連結是否正確。")
        UI.ProceFalse()
    else:
        ReResault = re.search("file_id=",GetLink)
        if ReResault == None:
            # 進入下載模式
            Main(1)
        else:
            # 進入刪除模式
            Main(2)

except Exception as e:
    if e.message == "'UpFile' object has no attribute 'Link'":
        # 進入上傳模式
        Main(0)
    else:
        UI.UpdateLog(e.message)
        UI.UpdateLog("發生未知的錯誤，請將錯誤畫面提供予腳本寫作人員。")
        UI.ProceFalse()