Skip to content

Instantly share code, notes, and snippets.

@mnutt
Created May 7, 2010 06:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save mnutt/393141 to your computer and use it in GitHub Desktop.
Save mnutt/393141 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# sfcurl
#
# This curl that automatically uses your safari/webkit cookies. Use it like you would
# normally use curl. Report bugs to michael@nuttnet.net. Or better yet, fork it from:
#
# http://gist.github.com/gists/393141
#
# Also for firefox: http://gist.github.com/393140
require 'rubygems'
require 'fileutils'
require 'uri'
require 'time'
require 'plist'
url = ARGV.select{|a| a =~ /http/ }.first
host = URI.split(url)[2]
top_level_host = host.split('.').slice(-2, 2).join('.')
cookie_file = "#{ENV['HOME']}/Library/Cookies/cookies.plist"
db = Plist.parse_xml(cookie_file)
File.open("/tmp/.sfcurl_cookie", "w") do |f|
db.each do |cookie|
next unless cookie['Domain'] =~ /#{top_level_host}$/
cookie_string = [cookie['Domain'],
"TRUE",
cookie['Path'],
"FALSE",
Time.parse(cookie['Expires'].to_s).to_i.to_s,
cookie['Name'],
cookie['Value']].join("\t")
f.write(cookie_string + "\n")
end
end
cmd = "curl " + ARGV.join(' ') + " --cookie /tmp/.sfcurl_cookie"
Kernel.exec(cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment