- Offizieller Beitrag
Hallo,
Ich versuche gerade ein Skript zu schreiben, dass mir automatisch CSV-Dateien bei vokker.net importiert. Höchstwahrscheinlich haben die damit keine Probleme, schließlich steigert es die Zugriffe zum Lernen und damit die Werbeeinnahmen. Eine API hat der Dienst offenbar nicht, hatte sie sogar erfolglos darauf angesprochen.
Nun aber zum Code:
Spoiler anzeigen
#include-once
[/autoit] [autoit][/autoit] [autoit]#include "WinHttp.au3"
#include "WinHttpConstants.au3"
#include "logging.au3"
#include <File.au3>
#include <Array.au3>
_vokkerUploadCSV("username", "password", @ScriptDir & "\test.csv")
[/autoit] [autoit][/autoit] [autoit]Func _vokkerUploadCSV($user, $pass, $filename)
; unterstützt?
If Not _WinHttpCheckPlatform() Then
_Log("Kein WinHTTP-Support")
Return
EndIf
_Log("WinHTTP-Support ok.")
; Initialize and get session handle
Local $hOpen = _WinHttpOpen('Mozilla/5.0 (Windows NT 6.2; rv:10.0.2) Gecko/20120101 Firefox/10.0.2')
; Get connection handle
Local $hConnect = _WinHttpConnect($hOpen, 'www.phase-6.com', $INTERNET_DEFAULT_HTTPS_PORT)
;~ _Log(@error)
;~ _Log($hConnect)
; Request
Local $postData = "josso_username=" & $user & "&josso_password=" & $pass
$hResult = _WinHttpSimpleSSLRequest($hConnect, "POST", "login/signon/login.do", "http://www.vokker.net", $postData, $WINHTTP_NO_ADDITIONAL_HEADERS, True)
;~ _ArrayDisplay($result)
;~ debugHTML($hResult[1])
_WinHttpCloseHandle($hConnect)
$regArray = StringRegExp($hResult[0], "Set-Cookie: (.*)", 2)
if not IsArray($regArray) Then Return False
Local $cookie = $regArray[1]
_Log("Cookie: " & $cookie)
$regArray = StringRegExp($cookie, "([A-Z0-9]{20,})", 2)
if not IsArray($regArray) Then Return False
Local $Session_ID = $regArray[1]
_Log("Session ID: " & $Session_ID)
; Upload-Teil
; Get connection handle
$hConnect = _WinHttpConnect($hOpen, "www.vokker.net", $INTERNET_DEFAULT_HTTPS_PORT)
; Request
Local $file = FileOpen(@ScriptDir & "\importTemplate.txt", 256)
Local $postData = FileRead($file)
Local $file2 = FileOpen($filename, 256)
$postData = StringReplace($postData, "$$filename$$", StringTrimLeft($filename, StringInStr($filename, "\", False, -1)))
$postData = StringReplace($postData, "$$content$$", FileRead($file2))
FileClose($file2)
$postData = StringReplace($postData, "$$title$$", "Testtitel!")
$postData = StringReplace($postData, "$$srcLang$$", "de")
$postData = StringReplace($postData, "$$tgtLang$$", "pl")
_Log($postData)
$dataLength = StringLen($postData)
MsgBox(0, "", $dataLength)
FileClose($file)
$hResult = _WinHttpSimpleSSLRequest($hConnect, "POST", "de/export/importieren.php", "http://www.vokker.net/de/export/import.php", Binary($postData), "Content-Length: " & $dataLength & @CRLF & "Content-Type: multi-part/form-data; boundary=-----------------------------41184676334"&@CRLF, True)
_Log("Ergebnis: " & $hResult & @CRLF & "Error: " & @error)
_ArrayDisplay($hResult)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
EndFunc ;==>_vokkerUploadCSV
Func debugHTML($string)
Local $filename = _TempFile() & ".html"
FileWrite($filename, $string)
ShellExecute($filename)
EndFunc
Ausgabe:
WinHTTP-Support ok.
Cookie: JSESSIONID=7505E7A14605681B4D707BC6F5112FC9; Path=/login
Session ID: 7505E7A14605681B4D707BC6F5112FC9
-----------------------------41184676334
Content-Disposition: form-data; name="file_csv"; filename="test.csv"
Content-Type: application/octet-stream
aktuell: (gegenwärtig) Thema, Problem, Situation ; aktualny
der Bereich: (Gebiet) ; obszar m
dumm: (opp klug) Person, Tat ; glupi
dumm: adv (einfältig) dumm fragen ; glupio zapytac
die Krise: ; kryzys m
wachsen: (größer, länger werden) Kind, Pflanzen, Haare ; rosnac
-----------------------------41184676334
Content-Disposition: form-data; name="unit_name_csv"
Testtitel!
-----------------------------41184676334
Content-Disposition: form-data; name="first_language"
de
-----------------------------41184676334
Content-Disposition: form-data; name="foreign_language"
pl
-----------------------------41184676334
Content-Disposition: form-data; name="csv_submitted"
Import ?
-----------------------------41184676334--
Ergebnis: 0
Error: 2
Alles anzeigen
Ich komme da einfach nicht weiter :(.
Kann jemand helfen?
Im Anhang das komplette Projekt, bewusst mit gültigen Login-Daten.
Vielen Dank!
Johannes