Permalink
Browse files

Minor clean up in the chmury.rb script

  • Loading branch information...
1 parent 930ed19 commit 9e5bb4764f0a56ac63693ba6618eeaa04851e7b6 @jstepien committed Jul 30, 2011
Showing with 21 additions and 8 deletions.
  1. +21 −8 scripts/chmury.rb
View
@@ -8,8 +8,16 @@ def die x
exit 1
end
+def verbose?
+ ARGV.include? '-v'
+end
+
+def wait?
+ ARGV.include? '-w'
+end
+
def cmd x
- p x if ARGV.include? '-v'
+ p x if verbose?
ret = `#{x}`
die x if $?.exitstatus != 0
ret
@@ -23,16 +31,21 @@ def sha1 x
tmp_file = 'tmp.gif'
json_file = 'images.json'
-Dir.chdir File.expand_path File.dirname __FILE__
-
-images = JSON File.read json_file
-
-if ARGV.include? '-w'
+if wait?
wait = rand 60*5
- puts "Waiting #{wait}s" if ARGV.include? '-v'
+ puts "Waiting #{wait}s" if verbose?
sleep wait
end
+Dir.chdir File.expand_path File.dirname __FILE__
+
+begin
+ images = JSON File.read json_file
+rescue
+ File.open(json_file, 'w').write JSON []
+ images = []
+end
+
cmd "wget \'#{img}\' -qO #{tmp_file}"
new_file = "#{sha1 File.read tmp_file}.jpg"
@@ -46,4 +59,4 @@ def sha1 x
Dir.glob('*.jpg').each { |f| File.unlink f unless images.include? f }
end
-cmd "rm tmp.gif"
+File.unlink 'tmp.gif'

0 comments on commit 9e5bb47

Please sign in to comment.