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

import re
import clr
import sys
import System
import random
import string
from os import urandom
from random import choice

#調用EasyScript.dll指令
sys.path.append(UI.StartupPath+'\Script\Module\dll')
clr.AddReferenceToFile("EasyScript.dll")
from EasyScript import *
EasyScript = EasyScript()

#隨機產生數字
def UID_var():  
    uidlen = []  
    while (len(uidlen) < 13):  
        uidlen.append(choice(string.digits))  
    return ''.join(uidlen)

def Matches(*s):
    html=s[0]
    sstr=s[1]
    lens=re.findall(sstr,html)
    #UI.UpdateLog('符合Matches匹配共有%d個'%(len(lens)),1)
    ss=[]
    if s[len(s)-1]!=False:
        for i, v in enumerate(lens):
            ss.append(v)
            UI.UpdateLog('MatchCollection[%d] = %s'%(i,v))
    return lens

    
UI.UpdateLog("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※")
UI.UpdateLog("※　　　【免空科學研究所　ｔｉｇｅｒｍａｘ．ｄｙｎｄｎｓ．ｏｒｇ】　　　※")
UI.UpdateLog("※　　　～㊣　　　　　　　　　楓　　　痕　　製作分享　　　　　㊣～　　　※")
UI.UpdateLog("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※")
UI.UpdateLog("【腳本更新】：2013.03.29 楓痕 修正原始冏rz內附腳本",2)
UI.UpdateLog("【上傳空間】：2shared.com",2)
UI.UpdateLog("【上傳大小】：200 MB",2)
UI.UpdateLog("【砍檔時間】：未知",2)
UI.UpdateLog("【腳本功能】：支援帳號上傳",1)
UI.UpdateLog("【腳本備註】：支援各32/64位元Windows系列",1)
HTTP.ClearProcedureData()
sid=UID_var()
FileName=EasyScript.SSplit(UI.FileName,".",1)
LogUrl=("http://www.2shared.com/login?callback=jsonp"+sid)
callback=("jsonp"+sid)
ReResault = re.search("[\w]",UI.User)
if ReResault != None:
    UI.UpdateLog("登入帳號【"+UI.User+"】",2)
    HTTP.AddPostParam("login",UI.User)
    HTTP.AddPostParam("password",UI.Password)
    HTTP.AddPostParam("callback",callback)
    HTML=HTTP.PostURL(LogUrl,HTTP.PostData)
    ReResault = re.search('"ok"',HTML)
    if ReResault == None:
        UI.UpdateLog("登入失敗...",3)
        UI.ProceFalse()
    else:
        UI.UpdateLog("成功登入...!",3)
else:
    UI.UpdateLog("請輸入帳號...",3)
    UI.ProceFalse()

HTML=HTTP.GetURL("http://www.2shared.com/")
MatchCollection=Matches(HTML,'uploadForm" action="(.+?)"',False)
FileId=EasyScript.SSplit(MatchCollection[0],"?",1,"CR")
sID=EasyScript.SSplit(FileId,"=",1,"CR")
upload_post=(MatchCollection[0])
UI.UpdateLog("開始上傳....",4)
HTTP.AddFileReference("fff","application/rar")
HTML=UI.UpFile(upload_post)
HTTP.SetCookie(HTTP.GetCookie)
HTML=HTTP.GetURL("http://www.2shared.com/uploadComplete.jsp?"+FileId)
ReResault = re.search("downloadLink",HTML)
if ReResault == None:
        UI.UpdateLog("上傳失敗...",3)
        UI.ProceFalse()
        exit()
else:
        UI.UpdateLog("上傳成功，開始抓取載點！",4)
        HotLink=EasyScript.GetstrToVariable(HTML,'width:468px">','</textarea>',1)
        UI.UpdateLog("檔案連結 【1】"+HotLink,3)
        UI.AddShareLink(HotLink)
        EasyScript.SaveDataToFile(UI.StartupPath+'\\Script\sharelink\2shared.com\\'+FileName+'\\網頁連結(不含檔名).txt',HotLink)
        EasyScript.SaveDataToFile(UI.StartupPath+'\\Script\sharelink\2shared.com\\'+FileName+'\\網頁連結(包含檔名).txt',HotLink+"|"+UI.FileName)
        UI.UpdateLog("備份路徑"+UI.StartupPath+'\\Script\sharelink\2shared.com\\'+FileName+'',3)
        UI.ProceSuccess()