Alexyz21
Silver Member | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору TaskAdd.lua: Код: local function fread(f) local h,x = io.open(f,"rb"),nil if h then x=h:read("*all"); io.close(h) end return x end local function fwrite(s,f) s = s or "" local h,x = io.open(f,"wb"),nil if h then x=h:write(s); io.close(h) end return x end local ftask="C:\\Lua5\\GetYAD.txt" local function TaskAdd(c,a,b) a,b,c = a or "",b or "",c or "" if a~="" then local r,s=false,fread(ftask) if s then for x in s:gmatch("purl: (%C-)[;%c]") or s:gmatch("purl: (%C-)$") do if x==a then r=true break end end end if not r then l="purl: "..a if b~="" then l=l.."; tdir: "..b end l=l.."\n" c=c:lower() if c=="ins" then fwrite(l..s,ftask) elseif c=="add" then fwrite(s..l,ftask) end end end end if arg[2] and (arg[2]:match("https?://yadi%.sk/") or arg[2]:match("https?://narod%.ru/disk/")) then TaskAdd(arg[1],arg[2],arg[3]) end | GetYAD.lua: Код: local function sleep(x) if x>0 then os.execute("ping -n "..tostring(x+1).." localhost > NUL") end end local function fread(f) local h,x = io.open(f,"rb"),nil if h then x=h:read("*all"); io.close(h) end return x end local function fwrite(s,f) s = s or "" local h,x = io.open(f,"wb"),nil if h then x=h:write(s); io.close(h) end return x end local function fappend(s,f) s = s or "" local h,x = io.open(f,"ab"),nil if h then x=h:write(s); io.close(h) end return x end local function GetPage(x) local s0="" if x then s0=io.popen("curls.exe -k -L --location-trusted "..x,"rb"):read("*all") end return s0 end local function encURL(x) x=string.gsub(x,"[^0-9A-Za-z%.%-_~]",function(x) local x,s=x:byte(),"0123456789ABCDEF" local z,y=math.floor(x/16)+1,math.mod(x,16)+1 return "%"..s:sub(z,z)..s:sub(y,y) end) return x end local ftask="C:\\Lua5\\GetYAD.txt" local tdir0="Z:\\Temp\\" local token="..." local function TaskAdd(a,b) a,b = a or "",b or "" if a~="" then local r,s=false,fread(ftask) if s then for x in s:gmatch("purl: (%C-)[;%c]") or s:gmatch("purl: (%C-)$") do if x==a then r=true break end end end if not r then l="purl: "..a if b~="" then l=l.."; tdir: "..b end l=l.."\n" fappend(l,ftask) end end end local function TaskRemove() local s=fread(ftask) if s and s~="" then s=s:match("^.-\n(.*)$") or "" fwrite(s,ftask) end end local function TaskRead() local s,a,b = fread(ftask) if s and s~="" then a=s:match("^purl: (%C-)[;%c]") or s:match("^purl: (%C-)$") b=s:match("^purl: %C-; tdir: (%C-)%c") or s:match("^purl: %C-; tdir: (%C-)$") end return a,b end local function GetDirectURL(urlh,url) return GetPage(urlh..' -X GET "'..url..'"'):match('"href":"(.-)"') end local function SaveToDisk(urlh,key) local url='https://cloud-api.yandex.net/v1/disk/public/resources/save-to-disk/?public_key='..encURL(key) return GetPage(urlh..' -X POST "'..url..'"') end local function fDelete(urlh,url) print(GetPage(urlh..' -X DELETE "'..url..'&permanently=true"')) -- delete file from YADisk end local function CheckSize(x) local s,h = nil,io.open(x,"r") if h then s=h:seek("end") h:close() end return s end local function fDownload(urlh,url,fpath,fsize,fResume) local line=GetDirectURL(urlh,url) if line then line=urlh..' -X GET "'..line..'" -o "'..fpath..'"' local dsize=CheckSize(fpath) if dsize==fsize then print("File exist!") else if dsize and fResume and dsize>0 and dsize<fsize then line=line..' -C -'else line=line..' --create-dirs' end print(GetPage(line)) -- download file dsize=CheckSize(fpath) if dsize==fsize then print("Task done") TaskRemove() -- delete 1st task else print("Download error") end end else fwrite("Error: fDownload()\nurl: "..url,fpath:gsub("\\[^\\]+$","\\debug.txt")) end end local function GetYAD(purl,tdir) if purl then if not tdir then tdir=tdir0 end local urlh0='-H "Content-Type: application/json"' local urlh='-H "Authorization: OAuth '..token..'" '..urlh0 local url=GetPage('"'..purl..'"') local patt='"mediatype":".-",.-,"size":(%d-)},"mtime":(%d-),"utime":%d-,"type":"file","name":"(.-)",.-,"hash":"(.-)"' local fsize,ftime,fname,key=url:match(patt) if fsize and ftime and fname and key then local line1='fname: '..fname..'\nfsize: '..fsize..'\nftime: '..os.date("%d.%m.%y %H:%M:%S",ftime)..'\nkey: '..key..'\npurl: '..purl local fpath,fsize,fResume = tdir..fname,tonumber(fsize),false local line0=fread(fpath..'.txt') if line0 and line1:match("^(.-)\nkey:")==line0:match("^(.-)\nkey:") then fResume=true else fwrite(line1,fpath..'.txt') end local url0='https://cloud-api.yandex.net/v1/disk/resources?path=disk%3A%2F%D0%97%D0%B0%D0%B3%D1%80%D1%83%D0%B7%D0%BA%D0%B8%2F'..encURL(fname) url=url0:gsub("(resources)(%?path)","%1/download%2") url=GetDirectURL(urlh,url) -- file exist to YADisk? if not url then url=SaveToDisk(urlh,key):match('"href":"(.-)"') if url then url=url:gsub("%+","%%20"):gsub("(resources)(%?path)","%1/download%2") sleep(10) fDownload(urlh,url,fpath,fsize,fResume) fDelete(urlh,url0) else url='https://cloud-api.yandex.net/v1/disk/public/resources/download?public_key='..encURL(key) fDownload(urlh0,url,fpath,fsize,fResume) end else url=url0:gsub("(resources)(%?path)","%1/download%2") fDownload(urlh,url,fpath,fsize,fResume) fDelete(urlh,url0) end else fwrite("Error: GetYAD()\nurl: "..url,tdir.."debug.txt") end else print("Task list is clear - need new URL for download") end end if arg[1] then TaskAdd(arg[1],arg[2]) end -- add task to tasklist while true do local a,b=TaskRead() if not a then print("All Tasks completed") break end GetYAD(a,b) end |
| Всего записей: 3484 | Зарегистр. 16-06-2007 | Отправлено: 09:16 18-06-2015 | Исправлено: Alexyz21, 10:41 19-06-2015 |
|