Pages

Ads 468x60px

Sunday, August 12, 2012

How to access Opera History in C#

Hi all! Today, I will be continuing my series on getting browser history in C#. I am going to focus on Opera today. Opera is different from Chrome and Firefox, in the fact that it does not use an SQLite Database to store browser history. Instead, it uses a text file. Each URL entry has four lines. The first line is the title, the second is the URL, the third is the time visited in Unix, or Epoch, time, and the last is the frequency. You can find this file in your appdata folder, by typing %appdata% into run, then browsing to Opera/Opera/global_history.dat and opening it with a text editor, such as NotePad++. Here is an example of my file:

The default frequency, for a site that has been visited one time, is -1. If it has been visited more than once, it will rise up into the millions. I am not sure how the frequency corresponds to the number of visits. If you know, please tell me.





To access this in C#, you will need to read the file line by line, and determine if the line is the title, url, etc. I have come up with this code that should help you:
 public IEnumerable GetHistory(bool display)
 {
  // Get Current Users App Data
  string documentsFolder = GetFolder();
  //If directory exists
  if (Directory.Exists(documentsFolder))
  {
   //This is the line number of the history file
   int lineNumber = 1;
   //This is the history file in the documents folder
   string historyFile = documentsFolder + "global_history.dat";
   //URL Variables
   string title = null;
   string url = null;
   int frequency = 0;
   DateTime visited = new DateTime();
   //These variables tell you if each line has been parsed
   //and each part has been assigned
   bool urlAssigned = false;
   bool titleAssigned = false;
   bool frequencyAssigned = false;
   bool visitedAssigned = false;
   //The url to assign to the URL table
   URL u;
   //Create a new StreamReader using the history file
   //to read the file line by line
   System.IO.StreamReader file =
 new System.IO.StreamReader(historyFile);
   //The line that is being read
   string line;
   //While the line that is being read is being read by
   //file.ReadLine, and is not null
   while ((line = file.ReadLine()) != null)
   {
    /*This is a table of what each line is
     * Line 1 = The Title of the URL
     * Line 2 = The URL of the URL
     * Line 3 = The Unix Time of which the URL was visited
     * Line 4 = The frequency of visits, which cannot convert 
     */
    //If the line number is the first, or if the line number minus 1
    //is a multiple of 4, for example, line number 5 - 1 is 4, which 
    //is a multiple of 4
    if (lineNumber == 1 || (lineNumber - 1) % 4 == 0)
    {
     Debug.WriteLine(line);
     title = line;
     titleAssigned = true;
    }
    //If line number is the second, or line number - 2 is a multiple
    //of 4, ex. 6 - 2 = 4, which is a multiple of 4
    if (lineNumber == 2 || (lineNumber - 2) % 4 == 0)
    {
     url = line;
     urlAssigned = true;
    }
    //If the line number is the third, or if line number - 3 is a
    //multiple of 4, ex. 7 - 3 = 4
    if (lineNumber == 3 || (lineNumber - 3) % 4 == 0)
    {
     //If the line is the correct length for Unix time
     if (line.Length == 10)
     {
      //then convert the number to a local datetime
      visited = FromUnixTime(Int64.Parse(line)).ToLocalTime();
     }
     else
      MessageBox.Show("An error occured while parsing the date \n" + line);
     visitedAssigned = true;
     frequencyAssigned = true;
    }
    //If all of the values have been assigned
    if (urlAssigned == true && titleAssigned == true 
&& frequencyAssigned == true && visitedAssigned == true)
    {
     //create a new URL with the values
     u = new URL(url, title, "Opera", visited, frequency);
     //And add the URL to the URLs array
     URLs.Add(u);
     //Reset the assigned values
     urlAssigned = false;
     titleAssigned = false;
     frequencyAssigned = false;
     visitedAssigned = false;

    }

    if (lineNumber == 4000 || file.EndOfStream == true)
    {
     MessageBox.Show("done " + URLs.Count.ToString());
    }
    
    lineNumber++;
   }

  }
  return URLs;
 }

 private static string GetFolder()
 {
  string folder = "\\Opera\\Opera\\";
  string documentsFolder = Environment.GetFolderPath
      (Environment.SpecialFolder.ApplicationData);
  documentsFolder += folder;
  return documentsFolder;
 }
 public DateTime FromUnixTime(long unixTime)
 {
  var epoch = new DateTime(1970, 1, 1, 0, 0, 0);
  return epoch.AddSeconds(unixTime);
 }
Note: To use this, you will need to include:
using System.Diagnostics;
using System.IO;

This script also reuses the URL class from my post on IE history. I have commented on most parts of this, so it should be easy to understand. This is the underlying method on how to get Opera's history with C#.

If you have any more questions or comments, please let me know!

6 comments:

  1. Hi Chris, I understode most of the part of your code. But I am not getting what is URL of which u are creating a new instance of and then assigning all the values to it.
    swati

    ReplyDelete
    Replies
    1. Hi swati,

      I am not sure of exactly which line you are talking about, but what is happening is that on each line, you test if it is the first, second, third, or fourth line in each sequence. You then assign the value of each line in the sequence, and then, after four lines are executed, the new URL is created, and added to a list of URL's. If you need my URL class, see my post on IE history.

      Hope it helped!

      Delete
    2. How To Access Opera History In C >>>>> Download Now

      >>>>> Download Full

      How To Access Opera History In C >>>>> Download LINK

      >>>>> Download Now

      How To Access Opera History In C >>>>> Download Full

      >>>>> Download LINK oH

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Could you please let me know the way to get chrome and mozila history in c#

    ReplyDelete