Class: Delish::ImportDelish
- Inherits:
-
Object
- Object
- Delish::ImportDelish
- Defined in:
- lib/import_delish.rb
Overview
{{{
Instance Method Summary collapse
-
#create_database ⇒ Object
{{{ create_database.
-
#delicious_communication(path, &block) ⇒ Object
{{{ delicious_communication(path, &block).
-
#get_all_tags ⇒ Object
{{{ get_all_tags.
-
#initialize(user, pass, db) ⇒ ImportDelish
constructor
{{{ initialize(user, pass, db).
-
#insert_post(url, description, extended, created, accessed, hash, tags) ⇒ Object
{{{ insert_post(url, description, extended, created, accessed, hash, tags).
-
#insert_post_xml_helper(post) ⇒ Object
{{{ insert_post_xml_helper.
-
#set_last_update ⇒ Object
{{{ set_last_update.
-
#update_bookmarks(&block) ⇒ Object
This function is too nesty {{{ update_bookmarks.
-
#update_exists? ⇒ Boolean
{{{ update_exists?.
Constructor Details
#initialize(user, pass, db) ⇒ ImportDelish
{{{ initialize(user, pass, db)
11 12 13 14 15 16 |
# File 'lib/import_delish.rb', line 11 def initialize(user, pass, db) @user = user @pass = pass @db = db @agent = 'delish commandline client #{Version::STRING}' end |
Instance Method Details
#create_database ⇒ Object
{{{ create_database
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/import_delish.rb', line 20 def create_database @db.execute %{ CREATE TABLE posts ( url VARCHAR(16384), description VARCHAR(255), extended VARCHAR(255), created DATETIME, hash CHAR(32) PRIMARY KEY, accessed DATETIME ); } @db.execute %{ CREATE TABLE post_tags ( hash CHAR(32), tag_id INTEGER );} @db.execute %{ CREATE TABLE tags ( tag VARCHAR(255) UNIQUE, id INTEGER PRIMARY KEY AUTOINCREMENT );} @db.execute "CREATE TABLE vars ( lastupdate CHAR(20));" @db.execute "INSERT INTO vars (lastupdate) VALUES ('0000-00-00T00:00:00Z');" end |
#delicious_communication(path, &block) ⇒ Object
{{{ delicious_communication(path, &block)
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/import_delish.rb', line 154 def delicious_communication(path, &block) http = Net::HTTP.new('api.del.icio.us', 443) http.use_ssl = true http.start do |http| request = Net::HTTP::Get.new(path, {'User-Agent' => @agent}) request.basic_auth @user, @pass response = http.request(request) sleep 1 data = REXML::Document.new(response.body) yield(data) end end |
#get_all_tags ⇒ Object
{{{ get_all_tags
89 90 91 92 93 94 |
# File 'lib/import_delish.rb', line 89 def self.delicious_communication('/v1/posts/all') do |data| data.elements.each("/posts/post") { |post| self.insert_post_xml_helper(post) } end self.set_last_update end |
#insert_post(url, description, extended, created, accessed, hash, tags) ⇒ Object
{{{ insert_post(url, description, extended, created, accessed, hash, tags)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/import_delish.rb', line 51 def insert_post(url, description, extended, created, accessed, hash, ) @db.execute( "INSERT INTO posts (url, description, extended, created, accessed, hash) VALUES (?, ?, ?, JULIANDAY(?), JULIANDAY(?), ?);", url, description, extended, created, accessed, hash) .split(' ').each do |tag| @db.execute( "INSERT OR IGNORE INTO tags (tag) VALUES (?);", tag) tag_id = @db.get_first_row( "SELECT id FROM tags WHERE tag = ?;", tag) @db.execute( "INSERT INTO post_tags (hash, tag_id) VALUES (?, ?);", hash, tag_id) end end |
#insert_post_xml_helper(post) ⇒ Object
{{{ insert_post_xml_helper
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/import_delish.rb', line 76 def insert_post_xml_helper(post) self.insert_post( post.attributes['href'], post.attributes['description'], post.attributes['extended'], post.attributes['time'].chop, post.attributes['time'].chop, post.attributes['hash'], post.attributes['tag']) end |
#set_last_update ⇒ Object
{{{ set_last_update
108 109 110 111 |
# File 'lib/import_delish.rb', line 108 def set_last_update self.update_exists? if @last_post.nil? @db.execute("UPDATE vars SET lastupdate = ?;", @last_post) end |
#update_bookmarks(&block) ⇒ Object
This function is too nesty {{{ update_bookmarks
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/import_delish.rb', line 116 def update_bookmarks(&block) return unless self.update_exists? found = false amount = 10 increment = 10 # Maximum for del.icio.us api is 100 until found == true or amount >= 100 self.delicious_communication("/v1/posts/recent?count=#{amount}") do |data| # Remove the ones we already went through elements = data.elements.to_a("/posts/post").slice(increment, amount - increment) elements.each do |post,i| if post.attributes['time'] <= @last_update found = true else yield(post.attributes['href'], post.attributes['description']) unless block.nil? self.insert_post_xml_helper(post) end end end amount += increment end self.set_last_update end |
#update_exists? ⇒ Boolean
{{{ update_exists?
98 99 100 101 102 103 104 |
# File 'lib/import_delish.rb', line 98 def update_exists? self.delicious_communication('/v1/posts/update') do |data| @last_post = data.elements['update'].attributes['time'] @last_update = (@db.get_first_row("SELECT lastupdate FROM vars;"))[0] return @last_post != @last_update end end |