Compare commits

...

9 Commits

Author SHA1 Message Date
Jeff Dallien 115d986044 Add CHANGES file to record differences between versions 2013-07-03 22:09:56 -04:00
Jeff Dallien 4f581183df Increment version now that browser auto detection is merged in. 2013-07-02 23:25:47 -04:00
Jeff Dallien 36ad9f11b2 Fix up browser detector spec and do some refactoring for clarity. 2013-07-02 23:13:22 -04:00
Jeff Dallien eda67c7ea8 Fix load path. 2013-07-02 23:07:52 -04:00
Ben Eills e8a4ab5f22 Added RSpec tests, although these are untested and should be checked over by someone who actually knows anything about RSpec before being pulled 2013-07-01 16:59:25 +01:00
Ben Eills 3217c96dd6 Added cookie-file location guessing
* User may now specify a browser to use
* Or he may choose to guess the best, based on mtime of cookie files
* Command line application accepts flags for this functionality
2013-06-27 20:51:41 +01:00
Jeff Dallien 6423b9bcb5 Again, fix markdown in README. 2012-02-22 20:09:36 -05:00
Jeff Dallien 74b464b40c Fix stupid markdown in README 2012-02-22 20:07:02 -05:00
Jeff Dallien 54e805f17f Update README to mention Chrome. 2012-02-22 20:05:13 -05:00
7 changed files with 157 additions and 19 deletions

16
CHANGES.md Normal file
View File

@ -0,0 +1,16 @@
CHANGES: cookie_extractor
-------------------------
### v0.2.0 2013-07-02
- Browser guessing code contributed by Ben Eills (github.com/beneills):
- Added --guess flag to automatically choose most recently used cookie file
- Added --browser flag to specify browser by name
### v0.1.0 2012-02-23
- Updated to include Chrome/Chromium support.
### v0.0.1 2012-02-20
- Initial version. Supports extracting Firefox cookies.

View File

@ -1,15 +1,26 @@
cookie_extractor
----------------
Extract cookies from Firefox sqlite cookie store (eventually Chrome also) into a wget-compatible cookies.txt file.
Extract cookies from Firefox, Chrome or Chromium sqlite cookie stores into a wget-compatible cookies.txt file.
### Install ###
gem install cookie_extractor
gem install cookie_extractor
### Usage ###
cookie_extractor /path/to/firefox/cookies.sqlite > cookies.txt
cookie_extractor /path/to/firefox/cookies.sqlite > cookies.txt
cookie_extractor --guess # Guess which browser to use and open corresponding file
cookie_extractor --browser chrome|chromium|firefox # Open corresponding DB
Typical locations for the cookies file on Linux are:
* Firefox: *~/.mozilla/firefox/(profile directory)/cookies.sqlite*
* Chrome: *~/.config/google-chrome/Default/Cookies*
* Chromium: *~/.config/chromium/Default/Cookies*
### License ###

View File

@ -2,22 +2,46 @@
require File.expand_path(File.join(File.dirname(__FILE__), "..", "lib", "cookie_extractor"))
# TODO: Locate cookie dbs automatically
filename = ARGV.first
unless filename
puts "Usage: (Firefox) cookie_extractor /path/to/cookies.sqlite"
puts " (Chrome) cookie_extractor /path/to/Cookies"
def usage msg=nil
puts msg if msg
puts "Usage: cookie_extractor /path/to/cookies.sqlite Open a DB file"
puts " cookie_extractor --guess Guess which browser to use and open corresponding file"
puts " cookie_extractor --browser chrome|chromium|firefox Open DB corresponding to a particular browser"
exit
end
if File.exists?(filename)
begin
extractor = CookieExtractor::BrowserDetector.new_extractor(filename)
puts extractor.extract.join("\n")
rescue SQLite3::NotADatabaseException,
CookieExtractor::BrowserNotDetectedException
puts "Error: File '#{filename}' is not a Firefox or Chrome cookie database"
end
else
puts "Error: File '#{filename}' does not exist"
begin
extractor =
case ARGV.first
when '--guess', '-g'
begin
CookieExtractor::BrowserDetector.guess()
rescue CookieExtractor::NoCookieFileFoundException
abort "Error: we couldn't find any supported broswer's cookies"
end
when '--browser', '-b'
browser = ARGV[1]
usage "Error: Please supply a browser name" unless browser
begin
CookieExtractor::BrowserDetector.browser_extractor(browser)
rescue CookieExtractor::InvalidBrowserNameException
abort "Error: '#{browser}' is not a valid browser name"
rescue CookieExtractor::NoCookieFileFoundException
abort "Error: Could not locate cookie file for browser #{browser}"
end
when nil
usage
else
filename = ARGV.first
abort "Error: #{filename} does not exist" unless File.exists?(filename)
CookieExtractor::BrowserDetector.new_extractor(filename)
end
puts extractor.extract.join("\n")
rescue SQLite3::NotADatabaseException,
CookieExtractor::BrowserNotDetectedException
abort "Error: File '#{filename}' is not a Firefox or Chrome cookie database"
end

View File

@ -1,3 +1,6 @@
$:.unshift(File.dirname(__FILE__)) unless
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
require "cookie_extractor/version"
require "cookie_extractor/common"
require "cookie_extractor/firefox_cookie_extractor"

View File

@ -1,7 +1,40 @@
module CookieExtractor
class BrowserNotDetectedException < Exception; end
class InvalidBrowserNameException < Exception; end
class NoCookieFileFoundException < Exception; end
class BrowserDetector
COOKIE_LOCATIONS = {
"chrome" => "~/.config/google-chrome/Default/Cookies",
"chromium" => "~/.config/chromium/Default/Cookies",
"firefox" => "~/.mozilla/firefox/*.default/cookies.sqlite"
}
# Returns the extractor of the most recently used browser's cookies
# or raise NoCookieFileFoundException if there are no cookies
def self.guess
most_recently_used_detected_browsers.each { |browser, path|
begin
extractor = self.browser_extractor(browser)
rescue BrowserNotDetectedException, NoCookieFileFoundException
# better try the next one...
else
return extractor
end
}
# If we make it here, we've failed...
raise NoCookieFileFoundException, "Couldn't find any browser's cookies"
end
# Open a browser's cookie file using intelligent guesswork
def self.browser_extractor(browser)
raise InvalidBrowserNameException, "Browser must be one of: #{self.supported_browsers.join(', ')}" unless self.supported_browsers.include?(browser)
paths = Dir.glob(File.expand_path(COOKIE_LOCATIONS[browser]))
if paths.length < 1 or not File.exists?(paths.first)
raise NoCookieFileFoundException, "File #{paths.first} does not exist!"
end
self.new_extractor(paths.first)
end
def self.new_extractor(db_filename)
browser = detect_browser(db_filename)
@ -12,6 +45,10 @@ module CookieExtractor
end
end
def self.supported_browsers
COOKIE_LOCATIONS.keys
end
def self.detect_browser(db_filename)
db = SQLite3::Database.new(db_filename)
browser =
@ -27,5 +64,15 @@ module CookieExtractor
def self.has_table?(db, table_name)
db.table_info(table_name).size > 0
end
def self.most_recently_used_detected_browsers
COOKIE_LOCATIONS.select { |browser, path|
Dir.glob(File.expand_path(path)).any?
}.sort_by { |browser, path|
File.mtime(Dir.glob(File.expand_path(path)).first)
}.reverse
end
private_class_method :most_recently_used_detected_browsers
end
end

View File

@ -1,3 +1,3 @@
module CookieExtractor
VERSION = "0.1.0"
VERSION = "0.2.0"
end

View File

@ -41,3 +41,40 @@ describe CookieExtractor::BrowserDetector, "determining the correct extractor to
end
end
end
describe CookieExtractor::BrowserDetector, "guessing the location of the cookie file" do
describe "when no cookie files are found in the standard locations" do
before :each do
Dir.stub!(:glob).and_return([])
end
it "should raise NoCookieFileFoundException" do
lambda { CookieExtractor::BrowserDetector.guess }.
should raise_error(CookieExtractor::NoCookieFileFoundException)
end
end
describe "when multiple cookie files are found in the standard locations" do
before :each do
cookie_locations = CookieExtractor::BrowserDetector::COOKIE_LOCATIONS
Dir.stub!(:glob).and_return([cookie_locations['chrome']],
[],
[cookie_locations['firefox']])
end
describe "and chrome was the most recently used" do
before :each do
File.should_receive(:mtime).twice.and_return(
Time.parse("July 2 2013 00:00:00"),
Time.parse("July 1 2013 00:00:00"))
end
it "should build a ChromeCookieExtractor" do
CookieExtractor::BrowserDetector.
should_receive(:browser_extractor).
once.with("chrome")
CookieExtractor::BrowserDetector.guess
end
end
end
end