Skip to content

Instantly share code, notes, and snippets.

@rinx
Created February 21, 2016 10:07
Show Gist options
  • Save rinx/622d47106ae004c8c650 to your computer and use it in GitHub Desktop.
Save rinx/622d47106ae004c8c650 to your computer and use it in GitHub Desktop.
flashair to google drive v3
local cjson = require "cjson"
-- Basic account info
local client_id = ""
local client_secret = ""
local refresh_token = ""
local boundary = "--61141483716826"
local function checkWlanConnect()
local res = fa.ReadStatusReg()
local a = string.sub(res, 13, 16)
a = tonumber(a, 16)
if (bit32.extract(a, 15) == 1) then
print("connect")
return true
end
if (bit32.extract(a, 0) == 1) then
print("mode Bridge")
return true
end
if (bit32.extract(a, 12) == 1) then
print("mode AP")
return true
end
return false
end
local function findNewestFile()
last_fname = ""
last_fpath = ""
max_mod = 0
fpath = "/DCIM"
for dirname in lfs.dir(fpath) do
dirpath = fpath .. "/" .. dirname
mod_dir = lfs.attributes( dirpath, "mode" )
if mod_dir == "directory" then
for filename in lfs.dir(dirpath) do
filepath = dirpath .. "/" .. filename
mod = lfs.attributes( filepath, "modification" )
if mod > max_mod then
max_mod = mod
last_fname = filename
last_fpath = filepath
end
end
else
mod = lfs.attributes( dirpath, "modification" )
if mod > max_mod then
max_mod = mod
last_fname = dirname
last_fpath = dirpath
end
end
end
return last_fpath
end
local function getAuth()
local mes="client_id="..client_id
mes=mes.."&client_secret="..client_secret
mes=mes.."&refresh_token="..refresh_token
mes=mes.."&grant_type=refresh_token"
local length = string.len(mes)
print("Sending: ["..mes.."]\n")
b, c, h = fa.request{
url = "https://accounts.google.com/o/oauth2/token",
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
["Content-Length"] = length,
},
method = "POST",
body=mes,
}
print("sended, header:"..h.."\n")
local tempTable = {}
tempTable = cjson.decode(b)
local access_token = tempTable["access_token"]
print("access_token:"..access_token.."\n")
return (access_token)
end
local function uploadPhoto(token, filePath)
local fileName = string.sub(filePath,(string.len(filePath) - string.find(string.reverse(filePath),"/") + 2))
local mes = "--"..boundary.."\r\n"
mes=mes.."Content-Type: application/json; charset=UTF-8\r\n"
mes=mes.."\r\n"
mes=mes.."{\r\n"
mes=mes.." \"name\": \""..fileName.."\"\r\n"
mes=mes.."}\r\n"
mes=mes.."\r\n"
mes=mes.."--"..boundary.."\r\n"
mes=mes.."Content-Type: image/jpeg\r\n"
mes=mes.."\r\n"
mes=mes.."<!--WLANSDFILE-->\r\n"
mes=mes.."--"..boundary.."--\r\n"
print("sending:"..filePath.."\n")
b, c, h = fa.request{
url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
headers = {
["Content-Type"] = "multipart/related; boundary="..boundary,
["Content-Length"] = tostring(lfs.attributes(filePath,"size") + string.len(mes) - 17),
["Authorization"] = "Bearer "..token,
},
method = "POST",
file = filePath,
body = mes,
bufsize=1460*10
}
print("sended, header:"..h.."\n")
print("sended, body:"..b.."\n")
print("sended:"..filePath.."\n")
end
sleep(6000)
if (checkWlanConnect()) then
newestfile = findNewestFile()
collectgarbage()
if (not (newestfile == "")) then
print("newestfile:"..newestfile.."\n")
collectgarbage()
access_token = getAuth()
collectgarbage()
uploadPhoto(access_token, newestfile)
end
end
collectgarbage()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment