1 (изменено: Malcev, 2019-08-15 12:55:20)

Тема: AHK: Заливка файлов на fex.net

Размер файлов до 100 гигов.

file := "D:\SHOGUN_S001_S001_T002.rar"

ComObjError(false)
setbatchlines -1
Gui,  -border -caption +hwndhGui
Gui, Show, w0 h0
percents := 0
SetTimer, uploadPercent, 300
f := FileOpen(file, "r")
FileSize := f.length
SplitPath, file, FileName

HTTP := ComObjCreate("WinHTTP.WinHTTPRequest.5.1")
loop
{
   HTTP.Open("GET", "https://api.fex.net/api/v1/anonymous/upload-token", true)
   HTTP.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko")
   HTTP.SetRequestHeader("Content-Type", "application/json")
   HTTP.SetRequestHeader("Pragma", "no-cache")
   HTTP.SetRequestHeader("Cache-Control", "no-cache, no-store")
   HTTP.SetRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT")
   HTTP.Send()
   HTTP.WaitForResponse()
   if (HTTP.Status = 200)
      break
   sleep 50
}
RegexMatch(HTTP.ResponseText, "s)""token"":""(.+?)""", match)
token := match1

loop
{
   HTTP.Open("POST", "https://api.fex.net/api/v1/anonymous/file", true)
   HTTP.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko")
   HTTP.SetRequestHeader("Content-Type", "application/json")
   HTTP.SetRequestHeader("Authorization","Bearer " token)
   HTTP.SetRequestHeader("Pragma", "no-cache")
   HTTP.SetRequestHeader("Cache-Control", "no-cache, no-store")
   HTTP.SetRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT")
   HTTP.Send("{""directory_id"":null,""size"":" FileSize ",""name"":""" FileName """}")
   HTTP.WaitForResponse()
   if (HTTP.Status = 200)
      break
   sleep 50
}
RegexMatch(HTTP.ResponseText, "s)""anon_upload_link"":""(.+?)"".+?""location"":""(https:.+?):.+?(/.+?)""", match)
uploadLink := "https://fex.net/s/" match1, uploadLocation := match2 match3

loop
{
   HTTP.Open("POST", uploadLocation, true)
   HTTP.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko")
   HTTP.SetRequestHeader("Content-Type", "application/json")
   HTTP.SetRequestHeader("Authorization","Bearer " token)
   HTTP.SetRequestHeader("Fsp-FileName", FileName)
   HTTP.SetRequestHeader("Fsp-Size", FileSize)
   HTTP.SetRequestHeader("Fsp-Version", "1.0.0")
   HTTP.SetRequestHeader("Pragma", "no-cache")
   HTTP.SetRequestHeader("Cache-Control", "no-cache, no-store")
   HTTP.SetRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT")
   HTTP.Send()
   HTTP.WaitForResponse()
   if (HTTP.Status = 201)
      break
   sleep 50
}

chunksize := 4194304
chunks := Ceil(FileSize/chunksize)
LastChunkSize := Mod(FileSize, chunksize)
loop % chunks
{ 
   offset := chunksize*(A_Index-1)
   If (A_Index = chunks) and (LastChunkSize != 0)
      chunksize := LastChunkSize
   safeArr := ComObjArray(0x11, chunksize) ; Create SAFEARRAY = VT_ARRAY|VT_UI1
   pvData := NumGet(ComObjValue(safeArr) + 12 + (A_PtrSize==8 ? 4 : 0)) ; get pvData memeber
   f.RawRead(pvData + 0, chunksize)
   loop
   {
      HTTP.Open("PATCH", uploadLocation, true)
      HTTP.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko")
      HTTP.SetRequestHeader("Authorization","Bearer " token)
      HTTP.SetRequestHeader("Content-Type", "application/offset+octet-stream")
      HTTP.SetRequestHeader("Fsp-FileName", FileName)
      HTTP.SetRequestHeader("Fsp-Offset", offset)
      HTTP.SetRequestHeader("Fsp-Version", "1.0.0")
      HTTP.SetRequestHeader("Pragma", "no-cache")
      HTTP.SetRequestHeader("Cache-Control", "no-cache, no-store")
      HTTP.SetRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT")
      HTTP.Send(safeArr)
      HTTP.WaitForResponse()
      if (HTTP.Status = 200) or (HTTP.Status = 204)
         break
      loop
      {
         HTTP.Open("POST", "https://api.fex.net/api/v1/anonymous/file", true)
         HTTP.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko")
         HTTP.SetRequestHeader("Content-Type", "application/json")
         HTTP.SetRequestHeader("Authorization","Bearer " token)
         HTTP.SetRequestHeader("Pragma", "no-cache")
         HTTP.SetRequestHeader("Cache-Control", "no-cache, no-store")
         HTTP.SetRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT")
         HTTP.Send("{""directory_id"":null,""size"":" FileSize ",""name"":""" FileName """}")
         HTTP.WaitForResponse()
         if (HTTP.Status = 200)
            break
         sleep 50
      }
   }
   count+=100/chunks
   percents := Format("{1:0.2f}", count)
}
safeArr := ""
f.Close()
if InStr(HTTP.ResponseText, "download_url")
   msgbox % clipboard := uploadLink
else
   msgbox % "Error`n" HTTP.ResponseText
ExitApp


uploadPercent:
if StrLen(title) > 5
   title := ""
else
   title .= "."
WinSetTitle, ahk_id %hGui%,, % percents "%" title
return
+ DD

2

Re: AHK: Заливка файлов на fex.net

Добавил проверку и продолжение загрузки после прерванного соединениея.