#include <GUIConstants.au3>
#include <EditConstants.au3>
Global $url, $gui, $input1, $input2, $input3, $input4, $btn1
Opt("GUICloseOnESC", 0)
$gui = GUICreate("ВК Сокращалка ссылок", 400, 300)
GUICtrlCreateLabel("Логин", 10, 10, 90, 30)
$input1 = GUICtrlCreateInput("", 110, 10, 190, 30)
GUICtrlSetLimit($input1, 70)
GUICtrlCreateLabel("Пароль", 10, 50, 90, 30)
$input2 = GUICtrlCreateInput("", 110, 50, 190, 30, $ES_PASSWORD)
GUICtrlSetLimit($input2, 25)
GUICtrlCreateLabel("URL", 10, 160, 90, 30)
$input3 = GUICtrlCreateInput("", 110, 160, 190, 30)
$btn1 = GUICtrlCreateButton("Получить короткую ссылку", 50, 200, 170, 30)
GUICtrlSetState($btn1, $GUI_DEFBUTTON)
GUICtrlCreateLabel("Короткая ссылка", 10, 250, 140, 30)
$input4 = GUICtrlCreateInput("", 160, 250, 160, 30, $ES_READONLY)
GUISetState(@SW_SHOW, $gui)
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
ExitLoop
Case $btn1
If StringLen(GUICtrlRead($input1)) > 5 And StringLen(GUICtrlRead($input2)) > 5 And StringLen(GUICtrlRead($input3)) > 5 Then
$url = _VKShortURL(GUICtrlRead($input1), GUICtrlRead($input2), GUICtrlRead($input3))
If Not @error Then
GUICtrlSetData($input4, $url)
GUICtrlSetState($input4, $GUI_FOCUS)
Else
MsgBox(16, "error" & @error, "error code: " & @error, 10, $gui)
EndIf
Else
MsgBox(16, "error", "error", 2, $gui)
EndIf
EndSwitch
WEnd
GUIDelete($gui)
Func _VKShortURL($sLogin, $sPassword, $sURL)
Local $oReq, $sSendStr, $Cookie, $sAction, $sURLCC
Local $sResponse, $aStringSplit, $sLink
$sURLCC = "https://vk.com/cc"
$sUserAgent = 'Mozilla/5.0 (Windows NT 6.1; rv:27.0) Gecko/20100101 Firefox/27.0'
$oReq = ObjCreate('WinHttp.WinHttpRequest.5.1')
If Not IsObj($oReq) Then Return SetError(1, 0, 0)
$oReq.Open('GET', "http://m.vk.com/", False)
$oReq.SetRequestHeader('User-Agent', $sUserAgent)
$oReq.SetRequestHeader('Cache-Controle', 'no-cach')
If $Cookie <> "" Then $oReq.SetRequestHeader('Cookie', $Cookie)
$oReq.SetRequestHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
$oReq.SetRequestHeader('Accept-Language', 'ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3')
$oReq.SetRequestHeader('Connection', 'keep-alive')
$oReq.Send($sSendStr)
$Cookie = __SetCookieParse($oReq.GetAllResponseHeaders())
$sResponse = $oReq.responseText
If StringInStr($sResponse, '://login.vk.com/?act=login') Then
$sAction = StringRegExpReplace($sResponse, "(?si).*?\<form.*?action\=['""](.*?)['""].*?\>.*", "$1")
$oReq.Open('POST', $sAction, False)
$oReq.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
$oReq.SetRequestHeader('User-Agent', $sUserAgent)
$oReq.SetRequestHeader('Cache-Controle', 'no-cach')
If $Cookie <> "" Then $oReq.SetRequestHeader('Cookie', $Cookie)
$oReq.SetRequestHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
$oReq.SetRequestHeader('Accept-Language', 'ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3')
$oReq.SetRequestHeader('Connection', 'keep-alive')
$oReq.SetRequestHeader('Referer', 'http://m.vk.com/')
$sSendStr = 'email=' & __encodeURIComponent($sLogin) & '&pass=' & __encodeURIComponent($sPassword) & ''
$oReq.Send($sSendStr)
EndIf
$oReq.Open('POST', $sURLCC, False)
$oReq.SetRequestHeader('X-Requested-With', 'XMLHttpRequest')
$oReq.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
$oReq.SetRequestHeader('User-Agent', $sUserAgent)
$oReq.SetRequestHeader('Cache-Controle', 'no-cach')
If $Cookie <> "" Then $oReq.SetRequestHeader('Cookie', $Cookie)
$oReq.SetRequestHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
$oReq.SetRequestHeader('Accept-Language', 'ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3')
$oReq.SetRequestHeader('Connection', 'keep-alive')
$oReq.SetRequestHeader('Referer', $sURLCC)
$sSendStr = 'act=shorten&al=1&link=' & __encodeURIComponent($sURL)
$oReq.Send($sSendStr)
$sResponse = $oReq.responseText
ConsoleWrite("POST_REQUEST: " & $sSendStr & @CRLF)
ConsoleWrite("RESPONSE: " & $sResponse & @CRLF)
$aStringSplit = StringSplit($sResponse, "<!>", 1)
If @error Then Return SetError(@error, @extended, 0)
$sLink = $aStringSplit[$aStringSplit[0]]
Return $sLink
EndFunc ;==>_VKShortURL
Func __SetCookieParse($AllResponseHeaders)
$sCOOKIES = ""
$aResponseHeaders = StringSplit($AllResponseHeaders, @CRLF)
For $i = 1 To $aResponseHeaders[0]
If StringLeft($aResponseHeaders[$i], 10) = 'Set-Cookie' Then
$iPozSim = StringInStr($aResponseHeaders[$i], ';')
$sCOOKIES &= StringMid($aResponseHeaders[$i], 13, $iPozSim - 13) & '; '
EndIf
Next
$sCOOKIES = StringMid($sCOOKIES, 1, StringLen($sCOOKIES) - 2)
Return $sCOOKIES
EndFunc ;==>__SetCookieParse
Func __encodeURIComponent($sTxt)
$sTxt = StringReplace($sTxt, @CRLF, '\r\n')
$sTxt = StringReplace($sTxt, @CR, '\r')
$sTxt = StringReplace($sTxt, @LF, '\n')
$sTxt = StringReplace($sTxt, "'", "\'")
$oSC = ObjCreate("ScriptControl")
If Not IsObj($oSC) Then Return SetError(1)
$oSC.Language = "JavaScript"
Return $oSC.Eval('encodeURIComponent("' & $sTxt & '");')
EndFunc ;==>__encodeURIComponent
Func __decodeURIComponent($sTxt)
$sTxt = StringReplace($sTxt, @CRLF, '\r\n')
$sTxt = StringReplace($sTxt, @CR, '\r')
$sTxt = StringReplace($sTxt, @LF, '\n')
$sTxt = StringReplace($sTxt, "'", "\'")
$oSC = ObjCreate("ScriptControl")
If Not IsObj($oSC) Then Return SetError(1)
Return $oSC.Eval('decodeURIComponent("' & $sTxt & '");')
EndFunc ;==>__decodeURIComponent