Skip to content

Instantly share code, notes, and snippets.

@lamdor
Created October 30, 2013 18:01
Show Gist options
  • Select an option

  • Save lamdor/7237178 to your computer and use it in GitHub Desktop.

Select an option

Save lamdor/7237178 to your computer and use it in GitHub Desktop.
require 'time'
def dropbox(dir)
File.join("~/Dropbox", dir)
end
DOWNLOADS_DIR = "~/Downloads"
PENDING_DIR = dropbox("Desktop/Pending")
Maid.rules do
[ DOWNLOADS_DIR, PENDING_DIR ].each do |pdf_dir|
rule "organize #{pdf_dir} pdfs" do
dir("#{pdf_dir}/*.pdf").each do |pdf|
if paper?(pdf) or slides?(pdf)
move(pdf, dropbox("Papers or Slides"))
elsif book?(pdf)
if beta_book?(pdf) then move_to_pending(pdf) else move(pdf, dropbox("Books")) end
else
move_to_pending(pdf)
end
end
end
end
[ DOWNLOADS_DIR, PENDING_DIR ].each do |inbox_dir|
rule "clean up #{inbox_dir} dir" do
mkdir(archive_under(inbox_dir))
dir("#{inbox_dir}/*").each do |file|
if 4.weeks.since?(accessed_at(file)) and file != archive_under(inbox_dir)
move_to_archive(inbox_dir, file)
end
end
dir("#{archive_under(inbox_dir)}/*").each do |file|
if 8.weeks.since?(accessed_at(file))
trash(file)
end
end
end
end
rule "organize uploaded photos" do
dir("#{dropbox('Camera Uploads')}/*").each do |photo|
created = photo_creation_date(photo)
if 1.week.since?(created)
dest = dropbox_photo_ymd_dir(created)
mkdir(dest) and move(photo, dest)
end
end
end
end
def move_to_pending(file)
move(file, PENDING_DIR) unless already_in_pending?(file)
end
def already_in_pending?(file)
File.exists?(File.expand_path(File.join(PENDING_DIR, File.basename(file))))
end
def move_to_archive(base, file)
move(file, archive_under(base))
end
def archive_under(base)
File.join(base, "Archive")
end
def dropbox_photo_ymd_dir(date)
File.join(dropbox("Pictures"), [date.year, "%02d" % date.month, "%02d" % date.day].join("/"))
end
def book?(pdf)
number_of_pages(pdf) > 30 and not landscape?(pdf)
end
def slides?(pdf)
landscape?(pdf)
end
def paper?(pdf)
number_of_pages(pdf) < 30 and created_by_tex(pdf)
end
def created_by_tex(pdf)
creator(pdf).include?("TeX")
end
def landscape?(pdf)
page_width(pdf) > page_height(pdf)
end
def beta_book?(pdf)
prag_prog_beta?(pdf) || manning_meap?(pdf)
end
def prag_prog_beta?(pdf)
pdf =~ /b[\d_]+\.pdf$/
end
def manning_meap?(pdf)
pdf =~ /MEAP.*\.pdf/
end
def creator(pdf)
spotlight_metadata("kMDItemCreator", pdf)
end
def page_height(pdf)
spotlight_metadata("kMDItemPageHeight", pdf).to_i
end
def page_width(pdf)
spotlight_metadata("kMDItemPageWidth", pdf).to_i
end
def number_of_pages(pdf)
spotlight_metadata("kMDItemNumberOfPages", pdf).to_i
end
def photo_creation_date(photo)
Time.parse(spotlight_metadata("kMDItemContentCreationDate", photo))
end
def spotlight_metadata(name, file)
cmd("mdls -raw -name #{name} #{sh_escape(file)}")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment