views:

1626

answers:

6

Using C#, I need to get all Firefox bookmarks for importing them into our database. How can I do this?

I'm aware of the SO question, Read FF 3 bookmarks in Java, but the answers there all seem to revolve around Java database drivers, and I'm not sure that some of those answers aren't Java-specific.

My primary question is, "How can I read Firefox bookmarks in C#?"

Secondary questions: I see \%user profile%\application data\mozilla\firefox\profiles\bookmarkbackups\bookmarks-[date].json files -- can I just parse that? If so, are there any existing parsers for that?

Rhetorical lamenting question: Why can't this be as easy as IE, where I just read the .url files in \%user profile%\favorites? Bah.

+1  A: 

There's a SQLite driver for .Net. Once you get that working I'd imagine the solution would be the same in both .Net and Java.

Spencer Ruport
How about just parsing the .json bookmark backup files? Would that be easier?
Judah Himango
Also, let's say I do go with SQLLite driver for .NET, where is the SQLLite database file?
Judah Himango
Have a look at this blog article which explains more about the FF bookmarks and where they're stored: http://www.lytebyte.com/2008/06/19/understanding-how-and-where-firefox-3-bookmarks-are-saved/
tomlog
Thanks, that helps.
Judah Himango
+2  A: 

Surely it works the same way as suggested in the Java question, just get the SQLite .NET provider and use that to access the FF database file.

tomlog
How about just parsing the .json bookmark backup files? Would that be easier?
Judah Himango
+5  A: 

Use the SQLite driver for .Net and access the file places.sqlite it can be found at
Application Data/Mozilla/Firefox/Profiles/$this_varies/places.sqlite
on my computer. It should not be hard for you to locate on your target computers.


Edit 1:
Here is a snip of code that prints out urls from the database:

using System.Data.SQLite; // downloaded from http://sourceforge.net/projects/adodotnetsqlite

namespace sqlite_test
{
    class Program
    {
        static void Main(string[] args)
        {
            var path_to_db = @"C:\places.sqlite"; // copied here to avoid long path
            SQLiteConnection sqlite_connection = new SQLiteConnection("Data Source=" + path_to_db + ";Version=3;New=True;Compress=True;");

            SQLiteCommand sqlite_command = sqlite_connection.CreateCommand();

            sqlite_connection.Open();

            sqlite_command.CommandText = "select * from moz_places";

            SQLiteDataReader sqlite_datareader = sqlite_command.ExecuteReader();

            while (sqlite_datareader.Read())
            {
                // Prints out the url field from the table:
                System.Console.WriteLine(sqlite_datareader["url"]);
            }
        }
    }
}

Edit 2:
As tip. I really must recommend the SQLite Manager plugin for firefox. It's very useful for working with sqlite databases.

Nifle
Thanks, I've marked this as the correct answer. It looks like the backup files are stored in .json format, but only for FF3 and later.For folks reading this, bottom line is this:-FF3 uses SQLLite to store bookmarks. It uses .json files to store backups of the bookmarks.-FF2 usees the bookmarks.html file to store bookmarks.
Judah Himango
Worth mentioning that actual official website for the SQLite lib is http://sqlite.phxsoftware.com - amazing product, so polished and in the public domain too!
Nathan Ridley
FYI, the SQLite ADO.NET provider you mentioned is not being maintained anymore. I recommend this one instead : http://sqlite.phxsoftware.com/. It is very complete and even supports the Entity Framework.
Thomas Levesque
@Nathan : you beat me by a minute ;)
Thomas Levesque
+1  A: 

I had to rework this slightly for my project http://www.codertakeout.com. Hope this revision helps clarify a few things thanks to some suggestions from around the web.

using System.Data.SQLite;  // need to install sqlite .net driver

String path_to_db = @"C:\Documents and Settings\Jeff\Application Data\Mozilla\Firefox\Profiles\yhwx4xco.default\places.sqlite";
String path_to_temp = System.IO.Path.GetTempFileName();

System.IO.File.Copy(path_to_db, path_to_temp, true);
SQLiteConnection sqlite_connection = new SQLiteConnection("Data Source=" + path_to_temp + ";Version=3;Compress=True;Read Only=True;");

SQLiteCommand sqlite_command = sqlite_connection.CreateCommand();

sqlite_connection.Open();

sqlite_command.CommandText = "SELECT moz_bookmarks.title,moz_places.url FROM moz_bookmarks LEFT JOIN moz_places WHERE moz_bookmarks.fk = moz_places.id AND moz_bookmarks.title != 'null' AND moz_places.url LIKE '%http%';";

SQLiteDataReader sqlite_datareader = sqlite_command.ExecuteReader();

while (sqlite_datareader.Read())
    {
        System.Console.WriteLine(sqlite_datareader[1]);
    }
sqlite_connection.Close();
System.IO.File.Delete(path_to_temp);
jeff
+1  A: 

Visit http://myexps.blogspot.com for the implementation in java.

import java.sql.*;

public class helloWorld {
  public static void main(String[] args) throws Exception {
      Class.forName("org.sqlite.JDBC");
      Connection conn = DriverManager.getConnection("jdbc:sqlite:/home/deepak/.mozilla/firefox/yvf7p20d.default/places.sqlite//");
  if(conn==null)
  {
   System.out.println("ERROR");
  }
  System.out.println(conn.toString());

  Statement stat = conn.createStatement();

  ResultSet rs = stat.executeQuery("select * from moz_bookmarks;");
  while (rs.next()) {
      System.out.println("id = " + rs.getString("id"));
      System.out.println("keyword = " + rs.getString("keyword_id"));
      System.out.println("title = " + rs.getString("title"));
  }
  rs.close();
  conn.close();
  }
}

This will be the java implementation

Deepak
A: 

Old post, but not sure where else to post. This returns the Error: SQLite error no such table: moz_bookmarks

I can connect to the DB obviously and I built the Query using NavCat.

I don't know what's wrong...

 Dim dataPath As String = "%APPDATA%\Mozilla\Firefox\Profiles\dzfuxen3.default\places.sqlite"
    Dim tempPath As [String] = System.IO.Path.GetTempFileName()
    Dim conn As String = "data source =" & tempPath
    Dim sqlConnect As New SQLiteConnection(conn)
    Dim sql As SQLiteCommand
    sql = sqlConnect.CreateCommand
    sqlConnect.Open()

    sql.CommandText() = "SELECT moz_bookmarks.id,moz_bookmarks.type, moz_bookmarks.fk, moz_bookmarks.parent,moz_bookmarks.position, moz_bookmarks.title, moz_bookmarks.keyword_id,moz_bookmarks.folder_type,moz_bookmarks.dateAdded, moz_bookmarks.lastModified FROM moz_bookmarks"
    Dim SQLreader As SQLiteDataReader = sql.ExecuteReader()
    While SQLreader.Read()
        Response.Write("<p>" & SQLreader(1) & "</p>")
    End While

    sql.Dispose()
    sqlConnect.Close()
jeremy
Mozilla may have since changed the table names. Open up the database in a Sqlite viewer and verify the table exists. One viewer is the Sqlite.NET adapter, whose installer installs a Sqlite database viewer for Visual Studio.
Judah Himango