Code: Select all
EnableExplicit
#NoMansSkyModsURL$ = "https://nomansskymods.com"
#User$ = "blabla"
#Passwd$ = "blublu"
Structure DownloadInfoStructure
FileURL$
_wpnonce$
id$
shortcodeId$
backurl$
EndStructure
Global ReceivedData.s
IncludeFile "libcurl.pbi"
Procedure Dump(Text$, *Data, Size.i)
Protected.i File, i, c, Byte
Protected Line$
File = OpenFile(#PB_Any, "c:\tmp\libcurl_debug.txt", #PB_File_Append)
If File
WriteStringN(File, Text$)
WriteStringN(File, "")
For i = 0 To size - 1 Step 16
Line$ = ""
For c = 0 To 15
If i + c < size
Line$ + RSet(Hex(PeekA(*Data + i + c), #PB_Byte), 2, "0") + " "
Else
Line$ + " "
EndIf
Next c
For c = 0 To 15
If i + c < size
Byte = PeekA(*Data + i + c)
If Byte < ' '
Line$ + "."
Else
Line$ + Chr(Byte)
EndIf
Else
Line$ + " "
EndIf
Next c
WriteStringN(File, Line$)
Next i
WriteStringN(File, "")
CloseFile(File)
EndIf
EndProcedure
ProcedureC.l my_trace(*handle, type.l, *Data, size.l, *userp)
Protected Result.l
Select type
Case #CURLINFO_TEXT
Debug "== Info: " + PeekS(*Data, size, #PB_UTF8)
Case #CURLINFO_HEADER_OUT
Dump("Send header", *Data, size)
Case #CURLINFO_DATA_OUT
Dump("Send Data", *Data, size)
Case #CURLINFO_SSL_DATA_OUT
Dump("Send SSL Data", *Data, size)
Case #CURLINFO_HEADER_IN
Dump("Recv header", *Data, size)
Case #CURLINFO_DATA_IN
Dump("Recv data", *Data, size)
Case #CURLINFO_SSL_DATA_IN
Dump("Recv SSL data", *Data, size)
EndSelect
ProcedureReturn Result
EndProcedure
Procedure.i NoMansSkyModsLogin(User$, Passwd$, CookieFilename$)
Protected Result.i, curl.i, Post$, Result$, *Post
curl = curl_easy_init()
If curl
Post$ = "log=" + #User$ + "&"
Post$ + "pwd=" + #Passwd$ + "&"
Post$ + "wp-submit=Log+In&"
Post$ + "redirect_to=" + #NoMansSkyModsURL$ + "/login/&"
Post$ + "wppb_login=true&"
Post$ + "wppb_form_location=page&"
Post$ + "wppb_request_url=" + #NoMansSkyModsURL$ + ":433/login/&"
Post$ + "wppb_lostpassword_url=&"
Post$ + "wppb_redirect_priority=&"
Post$ + "wppb_referer_url="
Debug Post$
Debug ""
curl_easy_setopt(curl, #CURLOPT_URL, str2curl(#NoMansSkyModsURL$ + "/wp-login.php"))
curl_easy_setopt(curl, #CURLOPT_POST, 1)
;curl_easy_setopt(curl, #CURLOPT_POSTFIELDS, str2curl(Post$))
*Post = UTF8(Post$) ; needed because it has to be available over the complete time
curl_easy_setopt(curl, #CURLOPT_POSTFIELDS, *Post)
curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYPEER, 0)
curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYHOST, 0)
curl_easy_setopt(curl, #CURLOPT_HEADER, @"")
curl_easy_setopt(curl, #CURLOPT_WRITEFUNCTION, @curlWriteData())
curl_easy_setopt(curl, #CURLOPT_COOKIEJAR, str2curl(CookieFilename$))
curl_easy_setopt(curl, #CURLOPT_REFERER, str2curl(#NoMansSkyModsURL$ + "/login/"))
; the following 2 lines are needed for debugging
;curl_easy_setopt(curl, #CURLOPT_DEBUGFUNCTION, @my_trace())
;curl_easy_setopt(curl, #CURLOPT_VERBOSE, 1)
Result = curl_easy_perform(curl)
If Result = #CURLE_OK
Result$ = curlGetData()
;Debug Result$
If FindString(Result$, "user_login")
Result = #False
Else
Result = #True
EndIf
Else
Result = #False
EndIf
curl_easy_cleanup(curl)
FreeMemory(*Post)
EndIf
ProcedureReturn Result
EndProcedure
Procedure.s GetNoMansSkyModsDownloadPage(Url$, CookieFilename$, *DownloadInfo.DownloadInfoStructure)
Protected Result$, curl.i, Result.i, Pos1.i, Pos2.i, Pos3.i, Pos4.i
curl = curl_easy_init()
If curl
curl_easy_setopt(curl, #CURLOPT_URL, str2curl(Url$))
curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYPEER, 0)
curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYHOST, 0)
curl_easy_setopt(curl, #CURLOPT_HEADER, @"")
curl_easy_setopt(curl, #CURLOPT_WRITEFUNCTION, @curlWriteData())
curl_easy_setopt(curl, #CURLOPT_COOKIEFILE, str2curl(CookieFilename$))
Result = curl_easy_perform(curl)
If Result = #CURLE_OK
Result$ = curlGetData()
If FindString(Result$, " 200 OK")
Pos2 = FindString(Result$, ".zip")
If Pos2
Pos1 = Pos2
Pos2 + 4
While Mid(Result$, Pos1, 1) <> #DQUOTE$ And Pos1 > 0
Pos1 - 1
Wend
If Pos1
Pos1 + 1
*DownloadInfo\FileURL$ = Mid(Result$, Pos1, Pos2 - Pos1)
Pos3 = FindString(Result$, "_wpnonce", Pos2)
If Pos3
Pos3 + 17
Pos4 = FindString(Result$, #DQUOTE$, Pos3)
*DownloadInfo\_wpnonce$ = Mid(Result$, Pos3, Pos4 - Pos3)
EndIf
Pos3 = FindString(Result$, #DQUOTE$ + "id" + #DQUOTE$, Pos2)
If Pos3
Pos3 + 12
Pos4 = FindString(Result$, #DQUOTE$, Pos3)
*DownloadInfo\id$ = Mid(Result$, Pos3, Pos4 - Pos3)
EndIf
Pos3 = FindString(Result$, "shortcodeId", Pos2)
If Pos3
Pos3 + 20
Pos4 = FindString(Result$, #DQUOTE$, Pos3)
*DownloadInfo\shortcodeId$ = Mid(Result$, Pos3, Pos4 - Pos3)
EndIf
Pos3 = FindString(Result$, "backurl", Pos2)
If Pos3
Pos3 + 16
Pos4 = FindString(Result$, #DQUOTE$, Pos3)
*DownloadInfo\backurl$ = Mid(Result$, Pos3, Pos4 - Pos3)
EndIf
Else
Result$ = ""
EndIf
Else
Result$ = ""
EndIf
Else
Result$ = ""
EndIf
Else
Debug "BKK"
EndIf
curl_easy_cleanup(curl)
EndIf
ProcedureReturn *DownloadInfo\FileURL$
EndProcedure
ProcedureC.l curlWriteFile(*ptr, Size, NMemB, *Stream)
ProcedureReturn WriteData(*Stream, *ptr, Size * NMemB)
EndProcedure
Procedure.i GetNoMansSkyModsFile(URL$, SaveAs$, CookieFilename$, *DownloadInfo.DownloadInfoStructure)
Protected Result.i, Curl.i, File.i, Post$, *Post
File = CreateFile(#PB_Any, SaveAs$)
If File
curl = curl_easy_init()
If curl
curl_easy_setopt(curl, #CURLOPT_URL, str2curl(Url$))
Post$ = "_wpnonce=" + *DownloadInfo\_wpnonce$ + "&"
Post$ + "id=" + *DownloadInfo\id$ + "&"
Post$ + "shortcodeID=" + *DownloadInfo\shortcodeId$ + "&"
Post$ + "backrl=" + *DownloadInfo\backurl$
*Post = UTF8(Post$) ; needed because it has to be available over the complete time
curl_easy_setopt(curl, #CURLOPT_POSTFIELDS, *Post)
curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYPEER, 0)
curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYHOST, 0)
curl_easy_setopt(curl, #CURLOPT_HEADER, @"")
curl_easy_setopt(curl, #CURLOPT_WRITEFUNCTION, @curlWriteFile())
curl_easy_setopt(curl, #CURLOPT_COOKIEFILE, str2curl(CookieFilename$))
curl_easy_setopt(curl, #CURLOPT_WRITEDATA, File)
Result = curl_easy_perform(curl)
If Result = #CURLE_OK
Result = #True
Else
Result = #False
EndIf
curl_easy_cleanup(curl)
FreeMemory(*Post)
EndIf
CloseFile(File)
EndIf
ProcedureReturn Result
EndProcedure
;-Main
Define CookieFilename$, FileURL$
Define DownloadInfo.DownloadInfoStructure
InitNetwork()
CookieFilename$ = GetTemporaryDirectory() + "NoMansSkyModsCookies.txt"
Debug "Cookiefile:" + CookieFilename$
Debug ""
If NoMansSkyModsLogin(#User$, #Passwd$, CookieFilename$)
Debug ""
Debug "Login Ok"
Debug ""
FileURL$ = GetNoMansSkyModsDownloadPage(#NoMansSkyModsURL$ + "/mods/no-mans-sky-save-editor/", CookieFilename$, @DownloadInfo)
If FileURL$ <> ""
; Debug "c:\tmp\" + GetFilePart(FileURL$)
;
; Debug "_wpnonce: " + DownloadInfo\_wpnonce$
; Debug "id: " + DownloadInfo\id$
; Debug "ShortCodeID: " + DownloadInfo\shortcodeId$
; Debug "BackURL: " + DownloadInfo\backurl$
If GetNoMansSkyModsFile(FileURL$, "c:\tmp\" + GetFilePart(FileURL$), CookieFilename$, @DownloadInfo)
Debug "File receive Ok"
EndIf
EndIf
EndIf
If FileSize(CookieFilename$) > 0
If DeleteFile(CookieFilename$)
Debug "Cookies deleted"
EndIf
EndIf
For an example I leave the debug and dump functions inside this listing.
You can remove them.