Advertisement
fuxoft

Flickr upload directory (Linux)

Dec 3rd, 2012
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.24 KB | None | 0 0
  1. #!/usr/bin/env lua5.1
  2. -- requires flickcurl and jhead installed, also libimage-exiftool-perl
  3. require("lfs") --sudo apt-get install liblua5.1-filesystem0
  4. local directory = "/home/fuxoft/Pictures/upload/"
  5. local extensions = {"jpg","jpeg","gif","png","tif","tiff"}
  6.  
  7. assert(directory:match("/$"),"No trailing slash in directory name")
  8.  
  9. local function is_jpeg(fname)
  10.     return string.lower(fname):match("%.jpe?g$")
  11. end
  12.  
  13. local function is_image(fname)
  14.     local lower = fname:lower()
  15.     for i, ext in ipairs(extensions) do
  16.         if lower:match("%."..ext.."$") then
  17.             return true
  18.         end
  19.     end
  20.     return false
  21. end
  22.  
  23. local function print_size(num)
  24.     num = num / 1000000
  25.     return (math.floor(num*10+0.5)/10).." MB"
  26. end
  27.  
  28. local function print_time(num)
  29.     num = math.floor(num + 0.5)
  30.     return string.format("%d:%02d",math.floor(num/60),num % 60)
  31. end
  32.  
  33. --Find Instagram files and set EXIF date from their filenames
  34. for fname in lfs.dir(directory) do
  35.     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")
  36.     if sec then
  37.         year = tonumber(year)
  38.         assert(year >= 2012 and year<2030, "Bad year: "..year)
  39.         local fullname = directory..fname
  40.         print("Instagram: "..fname)
  41.         os.execute("jhead -mkexif "..fullname)
  42.         os.execute(string.format("jhead -ts%s:%s:%s-%s:%s:%s %s",year, month, day, hour, min, sec, fullname))
  43.         os.execute("exiftool -overwrite_original -keywords=instagram "..fullname)
  44.         --print(fname)
  45.         local fullname = directory..fname
  46.     end
  47. end
  48. --os.exit()
  49.  
  50. --rename all files
  51. print("Autorotating.....")
  52. local stat = os.execute("jhead -autorot "..directory.."*")
  53. print("Renaming.....")
  54. local stat = os.execute("jhead -np_%Y-%m-%d_%H-%M-%S "..directory.."*")
  55. --assert(stat == 0, "Jhead error, stat = "..stat)
  56.  
  57. local files = {}
  58. local totalsize = 0
  59. for fname in lfs.dir(directory) do
  60.     if fname ~= "." and fname ~= ".." then
  61.         --print(fname)
  62.         local fullname = directory..fname
  63.         --print(lfs.attributes(fullname,"mode"))
  64.         local attrs = lfs.attributes(fullname)
  65.         if not (attrs.mode=="file" and is_image(fname)) then
  66.             print("Ignoring: "..fname)
  67.         else --Rename
  68.             --print("ok")
  69.             local size = attrs.size
  70.             table.insert(files,{fname = fname, fullname = fullname,size=size})
  71.             totalsize = totalsize + size
  72.         end
  73.     end
  74. end
  75.  
  76. table.sort(files, function(a,b) return a.fname < b.fname end)
  77.  
  78. local remainsize = totalsize
  79. local uplsize = 0
  80. local timestart = os.time()
  81. for i,file in ipairs(files) do
  82.     print("Uploading "..i.."/"..(#files).." ("..print_size(file.size).."): "..file.fname)
  83.     repeat
  84.         local status_ok = 0 == os.execute(string.format('flickcurl upload "%s" public 2> /dev/null',file.fullname))
  85.         if not status_ok then
  86.             print("* ERROR * Retrying...")
  87.         end
  88.         assert(os.execute("sleep 0.1")==0,"User abort")
  89.     until status_ok
  90.     remainsize = remainsize - file.size
  91.     uplsize = uplsize + file.size
  92.     local timedone = os.time() - timestart
  93.     local done_pct = uplsize / totalsize
  94.     local timetotal = timedone / done_pct
  95.     local timeremain = timetotal - timedone
  96.     print("----UPLOAD OK---- Remaining: "..print_time(timeremain).." ("..print_size(remainsize)..", "..math.floor(100-done_pct*100+0.5).."%)")
  97.     os.remove(file.fullname)
  98. end
  99. print("************** ALL DONE **************")
  100. os.execute("sleep 9")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement