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

UI.UpdateLog("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※")
UI.UpdateLog("※　　　【免空科學研究所　ｔｉｇｅｒｍａｘ．ｄｙｎｄｎｓ．ｏｒｇ】　　　※")
UI.UpdateLog("※　　　～㊣　　　　　　　　　　影楓　　製作分享　　　　　　　㊣～　　　※")
UI.UpdateLog("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※")
UI.UpdateLog("【腳本更新】：2013.03.13",2)
UI.UpdateLog("【下載空間】：[ES下載] www.megacloud.com",2)
UI.UpdateLog("【砍檔時間】：未知",2)
UI.UpdateLog("【腳本功能】：輸入該站網址進行下載!!",2)

import re

def encodeURIComponent():
	JScode='''
	        function encodeURI(s)  
	        {  
	            return encodeURIComponent(s);
	        }
	        '''
	JSLangueScript.Source(JScode)
	encodeURI = JSLangueScript.Exec('encodeURI',(download_token,))
	return encodeURI
UI.UpdateLog("※連結至 www.megacloud.com 下載網址...",4)
HTML=HTTP.GetURL(UI.Link)
Match = re.search("id\=\"fsid\" value\=\"([^\"]+)",HTML)
if Match == None:
	UI.UpdateLog("※無法擷取上傳所需參數..." + HTML,3)
	UI.ProceFalse()
	exit()
else:
	fsid = Match.group(1)
Match = re.search("user\_id\"\:([^\,]+)",HTML)
if Match == None:
	UI.UpdateLog("※無法擷取上傳所需參數..." + HTML,3)
	UI.ProceFalse()
	exit()
else:
	user_id = Match.group(1)
Match = re.search("action\_root\=\"([^\"]+)",HTML)
if Match == None:
	UI.UpdateLog("※無法擷取上傳所需參數..." + HTML,3)
	UI.ProceFalse()
	exit()
else:
	action = Match.group(1)
Match = re.search("filename\"\:\"([^\"]+)",HTML)
if Match == None:
	UI.UpdateLog("※無法擷取檔案名稱..." + HTML,3)
	UI.ProceFalse()
	exit()
else:
	DownloadName = Match.group(1)
UI.UpdateLog("※fsid → " + fsid,3)
UI.UpdateLog("※user_id → " + user_id,3)
UI.UpdateLog("※action → " + action,3)
HTTP.ClearProcedureData()
HTTP.AddPostParam("form_submit","true")
HTTP.AddPostParam("user_id",user_id)
HTTP.AddPostParam("fsid",fsid)
HTML=HTTP.PostURL("https://www.megacloud.com/ajax/get_download_token.php",HTTP.PostData)
Match = re.search("download\_token\"\:\"([^\"]+)",HTML)
if Match == None:
	UI.UpdateLog("※無法擷取上傳所需參數..."+HTML,3)
	UI.ProceFalse()
	exit()
else:
	download_token = Match.group(1)
	UI.UpdateLog("※download_token → " + download_token,3)
	UI.UpdateLog("=================================================================")
encodeURIComponent()
encodeURI_token = encodeURIComponent()
DownloadLink = action + "?fsid=" + fsid + "&userid=" + user_id + "&token=" + encodeURI_token
UI.UpdateLog("※檔案名稱 → " + DownloadName,4)
UI.UpdateLog("※下載載點 → " + DownloadLink,4)
UI.FileName = (DownloadName)
UI.UpdateLog("=================================================================")
UI.UpdateLog("※開始下載...",4)
Staus = UI.DownloadURL(DownloadLink)
if Staus == True:
	UI.UpdateLog("※檔案【" + DownloadName + "】下載成功!!",4)
	UI.UpdateLog("=================================================================")
else:
	UI.UpdateLog("※檔案下載失敗，請確認網站是否改版或下載網址是否失效...",3)
	UI.ProceFalse()
	exit()