Class: FirefoxBookmarkParser
- Inherits:
-
BookmarkParser
- Object
- BookmarkParser
- FirefoxBookmarkParser
- Defined in:
- lib/bookmarks.rb
Overview
Firefox bookmark parser (SQLite format)
Instance Attribute Summary
Attributes inherited from BookmarkParser
Instance Method Summary collapse
Methods inherited from BookmarkParser
Constructor Details
This class inherits a constructor from BookmarkParser
Instance Method Details
#parse ⇒ Object
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/bookmarks.rb', line 124 def parse begin require "sqlite3" rescue LoadError puts "Error: ".red + "sqlite3 gem not installed" puts "Run: ".grn + "gem install sqlite3" return end db_path = @file_path # Copy database to temp file if Firefox is running (to avoid lock) if firefox_running? require "tempfile" temp = Tempfile.new(["places", ".sqlite"]) temp.close FileUtils.cp(@file_path, temp.path) db_path = temp.path end begin db = SQLite3::Database.new(db_path) db.results_as_hash = true # Recursive CTE query to build folder paths query = " WITH RECURSIVE bookmark_tree(id, parent, title, url, path) AS (\n -- Start with bookmarks toolbar root\n SELECT b.id, b.parent, b.title, p.url,\n CAST(b.title as TEXT) as path\n FROM moz_bookmarks b\n LEFT JOIN moz_places p ON b.fk = p.id\n WHERE b.parent = (\n SELECT id FROM moz_bookmarks\n WHERE guid = 'toolbar_____'\n )\n\n UNION ALL\n\n -- Recursively get children\n SELECT b.id, b.parent, b.title, p.url,\n CAST(bt.path || '/' || b.title as TEXT) as path\n FROM moz_bookmarks b\n INNER JOIN bookmark_tree bt ON b.parent = bt.id\n LEFT JOIN moz_places p ON b.fk = p.id\n )\n SELECT id, title, path, url\n FROM bookmark_tree\n WHERE url IS NOT NULL\n ORDER BY path, title\n SQL\n\n db.execute(query).each do |row|\n folder_path = row[\"path\"].to_s.split(\"/\")[0..-2].join(\"/\") + \"/\"\n folder_path = \"|\" + folder_path.gsub(/[:,'\"]/, \"-\").downcase\n\n values = [folder_path, row[\"title\"], row[\"url\"], row[\"id\"].to_s]\n if matches_search?(values)\n @allurls << Bookmark.new(\n folder_path,\n row[\"title\"] || \"\",\n row[\"url\"] || \"\",\n row[\"id\"].to_s\n )\n end\n end\n rescue SQLite3::Exception => e\n puts \"Error: \".red + \"Could not read Firefox bookmarks database\"\n puts e.message\n ensure\n db&.close\n temp&.unlink\n end\nend\n" |