📄 queryrecord.cs
字号:
using System;
using System.IO;
using System.Text;
namespace SECompare.Structure
{
/// <summary>
/// Class QueryRecord is used to store a query result record, including title, URL, snippet etc.
/// The directory structure is following:
/// -----[DataRoot]
/// |_____[A]~[Z]: Hash mapping. The first letter of query string.
/// |_____[Query words] : for example, "cnn.com","CSU" etc.
/// |_____[Google/Yahoo/MSN]: Search Engine name.
/// |_____"info.txt": record information about result records (differs from query and engine, has not been completed yet).
/// |_____"1","2",...,"1000": (Rank number) Files that store search result records.
/// |_____"1.html","2.html",...,"1000.html": (Rank number.html) Files that store HTML code of corresponding URL.
///
///The format of the files (like "1","2",...,"1000") is following:
///--------------------------------------------------------
///*Title
///[Title: Store title of result.]
///*URL
///[URL : Store URL of result.]
///*Snippet
///[Snippet: Store Snippet of result.]
///--------------------------------------------------------
///
///NOTICE:
///1. Seperate line is started with "*" and description of next row.
///2. HTML code of taget page is saved as "1.html","2.html"...,"1000.html".
///
/// </summary>
public class QueryRecord
{
private String mQuery;
private String mEngine;
private String mRank; //Rank index number
private String mURL;
private String mSnippet;
private String mTitle;
private String mHTML;
public String URL
{
get
{
return this.mURL;
}
}
public String Title
{
get
{
return this.mTitle;
}
}
public String Snippet
{
get
{
return this.mSnippet;
}
}
public int Rank
{
set
{
this.mRank = value.ToString();
}
get
{
return Convert.ToInt32(this.mRank);
}
}
public String HTML
{
get
{
return this.mHTML;
}
}
/// <summary>
/// Initial a record of query results.
/// </summary>
/// <param name="query">Query words</param>
/// <param name="engine">Search engine name</param>
/// <param name="rank">Rank number</param>
/// <param name="title">Title of the page</param>
/// <param name="url">URL of the page</param>
/// <param name="snippet">Snippet of the page</param>
public QueryRecord(String query,String engine,String rank,String title,String url,String snippet)
{
this.mQuery = query;
this.mEngine = engine;
this.mRank = rank;
this.mTitle = title;
this.mURL = url;
this.mSnippet = snippet;
}
public QueryRecord(String query,EngineSet engine,String rank,String title,String url,String snippet)
{
this.mQuery = query;
this.mEngine = EngineSetting.GetName(engine);
this.mRank = rank;
this.mTitle = title;
this.mURL = url;
this.mSnippet = snippet;
}
/// <summary>
/// Get stored file name according to the directory naming standard
/// </summary>
/// <param name="query">Query Words</param>
/// <param name="engine">Engine Name</param>
/// <param name="rank">Rank number</param>
/// <returns></returns>
public static String GetPath(String query,String engine,String rank)
{
return QueryRecord.GetPath(query,engine) + "\\" + rank;
}
public static String GetPathOfHTML(String query,String engine,String rank)
{
return QueryRecord.GetPath(query,engine) + "\\" + rank + ".html";
}
private static String GetPath(String query,String engine)
{
StringBuilder path = new StringBuilder((new Config.Config()).DataRoot);
path.Append("\\" + QueryRecord.FilenameEncode(query.Substring(0,1)));
path.Append("\\" + QueryRecord.FilenameEncode(query));
path.Append("\\" + engine);
return path.ToString();
}
/// <summary>
/// Encode the words to a string, which could be a standard file name.
/// </summary>
/// <param name="originalName"></param>
/// <returns></returns>
public static String FilenameEncode(String originalName)
{
StringBuilder Words = new StringBuilder(originalName);
//use '0' as ESC
Words = Words.Replace("0", "00"); //This line must be run at first, before following replacements.
Words = Words.Replace(@"\","01");
Words = Words.Replace(@"/","02");
Words = Words.Replace(":", "03");
Words = Words.Replace("*", "04");
Words = Words.Replace("?", "05");
Words = Words.Replace("\"","06");
Words = Words.Replace("<", "07");
Words = Words.Replace(">", "08");
Words = Words.Replace("|", "09");
return Words.ToString();
}
/// <summary>
/// Check if the result of search (query,engine,rank) exists.
/// </summary>
/// <param name="query"></param>
/// <param name="engine"></param>
/// <param name="rank"></param>
/// <returns></returns>
public static bool Exists(String query,String engine,int rank)
{
if(query==null||query.Equals(""))
return false;
String file = QueryRecord.GetPath(query,engine,rank.ToString());
if(!File.Exists(file))
return false;
else
return true;
}
/// <summary>
/// Check if HTML code of search (query,engine,rank) exists.
/// </summary>
/// <param name="query"></param>
/// <param name="engine"></param>
/// <param name="rank"></param>
/// <returns></returns>
public static bool ExistsHTML(String query,String engine,int rank)
{
String html = QueryRecord.GetPathOfHTML(query,engine,rank.ToString());
if(!File.Exists(html))
return false;
else
return true;
}
/// <summary>
/// Load a record.
/// </summary>
/// <param name="query">Query Words</param>
/// <param name="engine">Engine Name</param>
/// <param name="rank">Rank number</param>
/// <returns>Loaded QueryRecord</returns>
public static QueryRecord Load(String query,String engine,int rank)
{
if(QueryRecord.Exists(query,engine,rank)==false)
return null;
//Get File Path
String file = QueryRecord.GetPath(query,engine,rank.ToString());
StreamReader reader = new StreamReader(file);
String line;
line = reader.ReadLine();
//Read Title
StringBuilder title = new StringBuilder(64);
while(line!=null&&line.StartsWith("*"))
{
line = reader.ReadLine();
}
while(line!=null&&!line.StartsWith("*"))
{
title.Append(line);
//read next line until read "*" line
line = reader.ReadLine();
}
//Read URL
StringBuilder URL = new StringBuilder(64);
while(line!=null&&line.StartsWith("*"))
{
line = reader.ReadLine();
}
while(line!=null&&!line.StartsWith("*"))
{
URL.Append(line);
//read next line until read "*" line
line = reader.ReadLine();
}
//Read Snippet
StringBuilder snippet = new StringBuilder(512);
while(line!=null&&line.StartsWith("*"))
{
line = reader.ReadLine();
}
while(line!=null&&!line.StartsWith("*"))
{
snippet.Append(line);
//read next line until read the end
line = reader.ReadLine();
}
reader.Close();
//Return value
return new QueryRecord(query,engine,rank.ToString(),title.ToString().Trim(),URL.ToString().Trim(),snippet.ToString());
}
/// <summary>
/// Load HTML code.
/// Because this function will cost some time, it is not contained in constructor function.
/// If you want to load HTML, you should call this function after "new QueryRecord()".
/// </summary>
public void LoadHTML()
{
//Read HTML
StringBuilder HTML = new StringBuilder(4096);
String path = QueryRecord.GetPathOfHTML(this.mQuery,this.mEngine,this.mRank);
StreamReader reader = new StreamReader(path,System.Text.Encoding.UTF8);
String line = reader.ReadLine();
while(line!=null)
{
HTML.Append(line);
//read next line until read the end
line = reader.ReadLine();
}
reader.Close();
this.mHTML = HTML.ToString();
}
/// <summary>
/// Check if QueryRecord obj equals to QueryRecord this.
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
return false;
if( ((QueryRecord)obj).mURL.Equals(this.mURL) )
return true;
return false;
}
public override int GetHashCode()
{
return this.Rank * base.GetHashCode();
}
/// <summary>
/// Save Record As new rank, where the older record will be overwrited.
/// If the record has not been saved yet, do nothing.
/// </summary>
/// <param name="NewRank">new rank number</param>
public void SaveAsNewRank(int NewRank)
{
if(!QueryRecord.Exists(this.mQuery,this.mEngine,Convert.ToInt32(this.mRank)))
return;
if(this.mRank.Equals(NewRank.ToString()))
return;
String OldFile = QueryRecord.GetPath(this.mQuery,this.mEngine,this.mRank);
String NewFile = QueryRecord.GetPath(this.mQuery,this.mEngine,NewRank.ToString());
if(File.Exists(NewFile))
File.Delete(NewFile);
File.Move(OldFile,NewFile);
this.mRank = NewRank.ToString();
}
/// <summary>
/// Delete this record from hard disk forever.
/// </summary>
public void Delete()
{
QueryRecord.Delete(this.mQuery,this.mEngine,this.mRank);
}
public static void Delete(String Query,String Engine,String Rank)
{
String file = QueryRecord.GetPath(Query,Engine,Rank);
if(File.Exists(file))
File.Delete(file);
file = QueryRecord.GetPathOfHTML(Query,Engine,Rank);
if(File.Exists(file))
File.Delete(file);
}
public static void Delete(String Query,String Engine)
{
String path = QueryRecord.GetPath(Query,Engine);
if(Directory.Exists(path))
Directory.Delete(path,true);
}
/// <summary>
/// Save the record to file.
/// If the record exists, it will be overwrite.
/// </summary>
public void Save()
{
String filepath = QueryRecord.GetPath(this.mQuery,this.mEngine,this.mRank);
//Save Title, URL and Snippet
FileInfo file = new FileInfo(filepath);
if(!file.Exists)
{
file.Directory.Create();
FileStream fs = file.Create();
fs.Close();
}
StreamWriter w = new StreamWriter(file.FullName,false);
w.WriteLine("*Title");
w.WriteLine(this.Title);
w.WriteLine("*URL");
w.WriteLine(this.URL);
w.WriteLine("*Snippet");
w.WriteLine(this.Snippet);
w.Close();
}
/// <summary>
/// Save HTML code.
/// Because this function will cost some time and may be not necessary, it is not contained in Save().
/// If you want to save HTML, you should call this function.
/// </summary>
/// <param name="HTML">HTML code to save.</param>
public void SaveHTML(String HTML)
{
String filepath = QueryRecord.GetPathOfHTML(this.mQuery,this.mEngine,this.mRank);
//Write file
FileInfo file = new FileInfo(filepath);
if(!file.Exists)
{
FileStream fs = file.Create();
fs.Close();
}
StreamWriter w = new StreamWriter(file.FullName,false,System.Text.Encoding.UTF8);
w.Write(HTML);
w.Close();
}
public override String ToString()
{
StringBuilder sb = new StringBuilder(4096);
sb.Append(this.mRank+". ");
sb.Append(this.mTitle+"\n");
sb.Append(this.mSnippet+"\n");
sb.Append("[URL] "+this.mURL+" \r\n\r\n");
return sb.ToString();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -