Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env lua5.1
- -- requires flickcurl and jhead installed, also libimage-exiftool-perl
- require("lfs") --sudo apt-get install liblua5.1-filesystem0
- local directory = "/home/fuxoft/Pictures/upload/"
- local extensions = {"jpg","jpeg","gif","png","tif","tiff"}
- assert(directory:match("/$"),"No trailing slash in directory name")
- local function is_jpeg(fname)
- return string.lower(fname):match("%.jpe?g$")
- end
- local function is_image(fname)
- local lower = fname:lower()
- for i, ext in ipairs(extensions) do
- if lower:match("%."..ext.."$") then
- return true
- end
- end
- return false
- end
- local function print_size(num)
- num = num / 1000000
- return (math.floor(num*10+0.5)/10).." MB"
- end
- local function print_time(num)
- num = math.floor(num + 0.5)
- return string.format("%d:%02d",math.floor(num/60),num % 60)
- end
- --Find Instagram files and set EXIF date from their filenames
- for fname in lfs.dir(directory) do
- local year, month, day, hour, min, sec = fname:match("IMG_(%d%d%d%d)(%d%d)(%d%d)_(%d%d)(%d%d)(%d%d).jpg")
- if sec then
- year = tonumber(year)
- assert(year >= 2012 and year<2030, "Bad year: "..year)
- local fullname = directory..fname
- print("Instagram: "..fname)
- os.execute("jhead -mkexif "..fullname)
- os.execute(string.format("jhead -ts%s:%s:%s-%s:%s:%s %s",year, month, day, hour, min, sec, fullname))
- os.execute("exiftool -overwrite_original -keywords=instagram "..fullname)
- --print(fname)
- local fullname = directory..fname
- end
- end
- --os.exit()
- --rename all files
- print("Autorotating.....")
- local stat = os.execute("jhead -autorot "..directory.."*")
- print("Renaming.....")
- local stat = os.execute("jhead -np_%Y-%m-%d_%H-%M-%S "..directory.."*")
- --assert(stat == 0, "Jhead error, stat = "..stat)
- local files = {}
- local totalsize = 0
- for fname in lfs.dir(directory) do
- if fname ~= "." and fname ~= ".." then
- --print(fname)
- local fullname = directory..fname
- --print(lfs.attributes(fullname,"mode"))
- local attrs = lfs.attributes(fullname)
- if not (attrs.mode=="file" and is_image(fname)) then
- print("Ignoring: "..fname)
- else --Rename
- --print("ok")
- local size = attrs.size
- table.insert(files,{fname = fname, fullname = fullname,size=size})
- totalsize = totalsize + size
- end
- end
- end
- table.sort(files, function(a,b) return a.fname < b.fname end)
- local remainsize = totalsize
- local uplsize = 0
- local timestart = os.time()
- for i,file in ipairs(files) do
- print("Uploading "..i.."/"..(#files).." ("..print_size(file.size).."): "..file.fname)
- repeat
- local status_ok = 0 == os.execute(string.format('flickcurl upload "%s" public 2> /dev/null',file.fullname))
- if not status_ok then
- print("* ERROR * Retrying...")
- end
- assert(os.execute("sleep 0.1")==0,"User abort")
- until status_ok
- remainsize = remainsize - file.size
- uplsize = uplsize + file.size
- local timedone = os.time() - timestart
- local done_pct = uplsize / totalsize
- local timetotal = timedone / done_pct
- local timeremain = timetotal - timedone
- print("----UPLOAD OK---- Remaining: "..print_time(timeremain).." ("..print_size(remainsize)..", "..math.floor(100-done_pct*100+0.5).."%)")
- os.remove(file.fullname)
- end
- print("************** ALL DONE **************")
- os.execute("sleep 9")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement