Skip to content

Instantly share code, notes, and snippets.

@nicosuave
Created January 31, 2013 22:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicosuave/4687364 to your computer and use it in GitHub Desktop.
Save nicosuave/4687364 to your computer and use it in GitHub Desktop.
Maid rules
Maid.rules do
## Functions ##
# Dupe detection thanks to bobthecow
def files(paths)
dir(paths).select { |f| File.file?(f) }
end
def size_of(path)
File.size(path)
end
def checksum_for(path)
Digest::MD5.hexdigest(File.read(path))
end
def dupes_in(paths)
dupes = []
files(paths) # Start by filtering out non-files
.group_by { |f| size_of(f) } # ... then grouping by size, since that's fast
.reject { |s, p| p.length < 2 } # ... and filter out any non-dupes
.map do |size, candidates|
dupes += candidates
.group_by { |p| checksum_for(p) } # Now group our candidates by a slower checksum calculation
.reject { |c, p| p.length < 2 } # ... and filter out any non-dupes
.values
end
dupes
end
## Utilities ##
rule 'Update yourself' do
`cd ~/.maid && git pull`
end
## Trash stuff ##
rule 'Trash linux isos' do
dir('~/Downloads/*.iso').each { |p| trash p }
end
# Thanks gsiener
rule 'Trash downloaded software' do
# These can generally be downloaded again very easily if needed...
# but just in case, give me a few days before trashing them.
dir('~/Downloads/*.{dmg,pkg}').each do |p|
trash(p) if accessed_at(p) > 3.days.ago
end
osx_app_extensions = %w(app dmg pkg wdgt)
osx_app_patterns = osx_app_extensions.map { |ext| (/\.#{ext}\/$/) }
zips_with_osx_apps_inside = dir('~/Downloads/*.zip').select do |path|
candidates = zipfile_contents(path)
candidates.any? { |c| osx_app_patterns.any? { |re| c.match(re) } }
end
trash(zips_with_osx_apps_inside)
end
# Thanks bobthecow
rule 'Trash duplicate downloads' do
dupes_in('~/Downloads/*').each do |dupes|
# Keep the dupe with the shortest filename
trash dupes.sort_by { |p| File.basename(p).length }[1..-1]
# You could also keep the oldest:
# trash dupes.sort_by { |p| File.mtime(p) }[1..-1]
end
end
## Random cleanup ##
rule 'Add mp3s' do
dir('~/Downloads/*.mp3').each do |path|
if duration_s(path) > 30.0
move(path, '~/Music/iTunes/iTunes Media/Automatically Add to iTunes/')
end
end
end
rule 'Dropbox screenshots' do
dir('~/Desktop/Screen shot *').each do |path|
if 3.days.since?(last_accessed(path))
move(path, '~/Dropbox/Screenshots/')
end
end
end
rule 'Dropbox powerpoints' do
dir('~/Downloads/*.pptx').each do |path|
move(path, '~/Dropbox/Powerpoints/')
end
end
rule 'Dropbox PDFs' do
dir('~/Downloads/*.pdf').each do |path|
move(path, '~/Dropbox/PDFs/')
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment