Skip to content

Commit

Permalink
Replaced SQLite database with a Marshal database.
Browse files Browse the repository at this point in the history
  • Loading branch information
epitron committed Sep 10, 2009
1 parent 4ca6ceb commit d338610
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 71 deletions.
4 changes: 1 addition & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ begin
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
gem.name = "delicious-cli"
gem.summary = %Q{Delicious.com commandline interface}
gem.description = %Q{A commandline tool which lets you download all your delicious.com links into a local SQLite database and search them (with pretty color-coded results).}
gem.description = %Q{A commandline tool which lets you download all your delicious.com links and search them (with pretty color-coded results).}
gem.email = "[email protected]"
gem.homepage = "http://github.com/epitron/delicious-cli"
gem.authors = ["epitron"]
gem.bindir = 'bin'
gem.files = FileList['lib/**/*.rb']
gem.add_dependency('sequel')
gem.add_dependency('httparty')
gem.add_dependency('colorize')
gem.add_dependency('sqlite3-ruby')
end
Jeweler::GemcutterTasks.new
rescue LoadError
Expand Down
8 changes: 4 additions & 4 deletions lib/delicious-cli/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ def self.posts_all(options={})
[result["posts"]["post"]].flatten # ensure it's an array
end

def self.posts_since(time)
$log.debug "Retrieving links newer than #{time}"
results = posts_all(:fromdt=>time)
results.select { |r| r["time"] != time }
def self.posts_since(time_string)
$log.debug "Retrieving links newer than #{time_string}"
results = posts_all(:fromdt=>time_string)
results.select { |r| r["time"] != time_string }
end

def self.valid_auth?
Expand Down
101 changes: 45 additions & 56 deletions lib/delicious-cli/db.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
require 'sequel'
require 'sequel/extensions/blank'

require 'delicious-cli/settings'
require 'delicious-cli/api'

#################################################################

$log.debug "Loading database..."
DB = Sequel.sqlite(configfile('delicious.db'))
$log.debug "done."

#################################################################

=begin
Sample record:
Expand All @@ -29,43 +20,40 @@
#################################################################

class Database
@@posts = DB[:posts]

@@filename = configfile('delicious.marshal')
@@posts = []

def self.posts
@@posts
end

def self.init!
create_posts! unless DB.table_exists?(:posts)
end

$log.debug "Loading database..."
@@posts = Marshal.load(open(@@filename)) if File.exists? @@filename
$log.debug "done."
end

def self.clear!
DB.drop_table(:posts)
create_posts!
File.delete @@filename
@@posts = []
end

def self.create_posts!
DB.create_table :posts do
primary_key :id

string :href
string :time
string :tag
string :description
text :extended
string :hash
string :meta

index :hash, {:unique=>true}
index [:description, :extended, :tag]
def self.save!
open(@@filename, "w") do |f|
f.write Marshal.dump(@@posts)
end
end

def self.sync(all=false)
all = true if @@posts.empty?

if all
print "* Retrieving all links..."
print " |_ Retrieving all links..."
STDOUT.flush
posts = Delicious.posts_all
else
print "* Retrieving new links..."
print " |_ Retrieving new links..."
STDOUT.flush
posts = Delicious.posts_since(most_recent_time)
end
Expand All @@ -74,44 +62,45 @@ def self.sync(all=false)

puts " (#{posts.size} links found)"

return if posts.empty?
if posts.size == 0
puts
return
end

print "* Inserting links into database..."
print " |_ Processing links..."
posts.each { |post| post["time_string"] = post["time"]; post["time"] = DateTime.parse(post["time_string"]) }
@@posts += posts.sort_by { |post| post["time"] }
puts "done!"

counter = 0
for post in posts
counter += 1
begin
add post
rescue Sequel::DatabaseError => e
$log.debug "error: #{e} adding #{post.inspect}"
end

if counter % 37 == 0
print "."
STDOUT.flush
end
end
print " |_ Saving database..."
save!

puts "done!"
puts
end

def self.most_recent_time
@@posts.order(:time.desc).limit(1).first[:time]
#@@posts.order(:time.desc).limit(1).first[:time]
@@posts.last["time_string"]
end

def self.add(params)
@@posts << params
end

def self.find(words)
sequel_query = @@posts
for word in words
pattern = "%#{word}%"
sequel_query = sequel_query.filter(:extended.like(pattern) | :description.like(pattern) | :tag.like(pattern))

finders = words.map{|word| /#{word}/i }
fields = %w[extended description tag]

@@posts.select do |post|
finders.all? do |finder|
fields.any? do |field|
post[field].match finder
end
end
end
sequel_query.order(:time)
end


end

end
15 changes: 7 additions & 8 deletions lib/delicious-cli/display.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ def hilite(words); self; end

#################################################################

def formatdate(date, width=11)
dt = DateTime.parse(date)
def formatdate(dt, width=11)
time = "%l:%M%P"
date = "%d %b %g"
#dt.strftime("#{date} #{time}")
Expand All @@ -46,14 +45,14 @@ def formatdate(date, width=11)
def display(post, query, indent_size=11)
indent = " " * indent_size

date = formatdate(post[:time], indent_size)
desc = post[:description].hilite(query, :light_white)
ext = post[:extended].hilite(query, :white)
url = post[:href].hilite(query, :light_blue)
tags = post[:tag].hilite(query, :light_cyan)
date = formatdate(post["time"], indent_size)
desc = post["description"].hilite(query, :light_white)
ext = post["extended"].hilite(query, :white)
url = post["href"].hilite(query, :light_blue)
tags = post["tag"].hilite(query, :light_cyan)

puts date + desc
puts indent + ext if post[:extended].any?
puts indent + ext if post["extended"].any?
puts indent + url
puts indent + "(".cyan + tags + ")".cyan
puts
Expand Down

0 comments on commit d338610

Please sign in to comment.