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

import re

UI.UpdateLog("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※")
UI.UpdateLog("※　　　【免空科學研究所　ｔｉｇｅｒｍａｘ．ｄｙｎｄｎｓ．ｏｒｇ】　　　※")
UI.UpdateLog("※　　　～㊣　　　　　　　　　影楓　　製作分享　　　　　　　　㊣～　　　※")
UI.UpdateLog("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※")
UI.UpdateLog("【腳本更新】：2013.03.13",2)
UI.UpdateLog("【上傳空間】：[ES上傳] imgur.com (圖空)",2)
UI.UpdateLog("【上傳大小】：10 MB",2)
UI.UpdateLog("【砍檔時間】：未知",2)
UI.UpdateLog("【腳本功能】：無帳號上傳",2)
UI.UpdateLog("※連結至 imgur.com 圖空上傳頁面...",4)

URL = "http://imgur.com/upload/start_session"
HTML = HTTP.GetURL(URL)
Match = re.search(":\"([^\"]+)",HTML)
if Match == None:
        UI.UpdateLog("※無法擷取上傳所需參數..." + HTML,3)
        UI.ProceFalse()
        exit()
else:
        sid = Match.group(1)
        UI.UpdateLog("※sid → " + sid,3)
        UI.UpdateLog("※上傳所需參數值擷取成功...",4)
        UI.UpdateLog("=================================================================")
HTTP.ClearProcedureData()
HTTP.AddUpParam("edit","0")
HTTP.AddUpParam("terms","0")
HTTP.AddUpParam("catify","0")
HTTP.AddUpParam("layout","b")
HTTP.AddUpParam("sid",sid)
HTTP.AddUpParam("create_album","0")
HTTP.AddUpParam("total_uploads","1")
HTTP.AddUpParam("current_upload","1")
HTTP.AddUpParam("Upload","Submit Query")
HTTP.AddUpParam("gallery_submit","false")
HTTP.AddUpParam("Filename",UI.FileName)
HTTP.AddUpParam("album_title","Optional Album Title")
HTTP.AddUpParam("gallery_title","Gallery submission title (required)")
HTTP.AddFileReference("Filedata","application/octet-stream")
UI.UpdateLog("※開始上傳...",4)
HTML = UI.UpFile("http://imgur.com/upload")
Match = re.search("success",HTML)
if Match == None:
        UI.UpdateLog("※上傳失敗，網站改版或檔案超過上限值..." + HTML,3)
        UI.ProceFalse()
        exit()
else:
        UI.UpdateLog("※檔案：【" + UI.FileName + "】，上傳成功。",4)
        UI.UpdateLog("=================================================================")
Match = re.search("hash\":\"([^\"]+)",HTML)
if Match == None:
        UI.UpdateLog("※無法擷取圖片連結網址..." + HTML,3)
        UI.ProceFalse()
        exit()
else:
        HotLink = "http://imgur.com/" + Match.group(1) + ".jpg"
        ImgLink = "[img]" + HotLink + "[/img]"
HTTP.SetCookie(HTTP.GetCookie)
DelUrl = "http://imgur.com/" + Match.group(1)
DelHtml = HTTP.GetURL(DelUrl)
Match = re.search("text\" value=\"([^\"]+)",DelHtml)
if Match == None:
        UI.UpdateLog("※無法擷取刪除連結網址..." + DelHtml,3)
        UI.ProceFalse()
        exit()
else:
        DelLink = Match.group(1)
UI.AddShareLink(HotLink + "^" + ImgLink + "^" + DelLink)
UI.UpdateLog("※圖片網址：" + HotLink,4)
UI.UpdateLog("※論壇語法：" + ImgLink,4)
UI.UpdateLog("※刪除網址：" + DelLink,3)
UI.UpdateLog("=================================================================")
UI.ProceSuccess()
exit()