Google Search API delphi wrapping unit

google search api is much useful,google search API provides many kinds of interface,but no pascal code,i write to a simple unit to wrap google search api,let’s see pascal header


unit DxGoogleSearchApi;

interface
  uses Classes,SysUtils,msxml,uLkJSON,Variants;

{$IF RTLVersion > 14.00}
    {$IF RTLVersion > 19.00}
    {$DEFINE USE_D2009}
    {$IFEND}
{$IFEND}
type
//searchtype        Web  local  video blogs  news   books  pic  patent
  TDxSearchType = (Sh_Web,Sh_Local,Sh_Video,Sh_Blog,Sh_News,Sh_Book,Sh_Image,Sh_patent);

  //search result record
  TDxSearchRecord = class
  private
    RetList: TStringList;
    function GetFieldCount: Integer;
    function GetFields(index: Integer): string;
    function GetValues(index: Integer): string;
  public
    constructor Create;
    procedure FromJsonObj(JsonObj: TlkJSONobject);
    destructor Destroy;override;
    property FieldCount: Integer read GetFieldCount;
    property Fields[index: Integer]: string read GetFields;
    property Values[index: Integer]: string read GetValues;
    function FieldByName(FieldName: string): string;
  end;

  TDxSearchRecords = class
  private
    List: TList;
    FSearchType: TDxSearchType;
    function GetCount: Integer;
    function GetRecords(index: Integer): TDxSearchRecord;
  public
    procedure Clear;
    constructor Create;
    property SearchType: TDxSearchType read FSearchType;
    destructor Destroy;override;
    property Count: Integer read GetCount;
    property Records[index: Integer]: TDxSearchRecord read GetRecords;
  end;

  //search API
  TDxGoogleSearch = class
  private
    FSearchType: TDxSearchType;
    FBigSearchSize: Boolean;
    FSearchStart: Integer;
    FVersion: string;
    HttpReq: IXMLHttpRequest;
    FRecords: TDxSearchRecords;
    Pages: array of Integer;
    FCurSearchInfo: string;
    ClearOld: Boolean;
    FCurPageIndex: Integer;
    function GetPageCount: Integer;
  public
    constructor Create;
    destructor Destroy;override;
    procedure Search(SearchInfo: string);
    property CurPageIndex: Integer read FCurPageIndex;
    function NextSearch: Boolean;//search next page
    property PageCount: Integer read GetPageCount;
    property Records: TDxSearchRecords read FRecords;
    property BigSearchSize: Boolean read FBigSearchSize write FBigSearchSize default true;//rsz param
    property SearchStart: Integer read FSearchStart write FSearchStart default 0;//search start pos,start param
    property Version: string read FVersion write FVersion;
    property SearchType: TDxSearchType read FSearchType write FSearchType default Sh_Web;//search type
  end;

i write an example demo code:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,msxml, StdCtrls,uLkJSON,DxGoogleSearchApi, ComCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    Panel1: TPanel;
    Edit1: TEdit;
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    ComboBox1: TComboBox;
    Button2: TButton;
    Label3: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    Search: TDxGoogleSearch;
  end;

var
  Form1: TForm1;

implementation
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  i,j: Integer;
  Rec: TDxSearchRecord;
  CreateColumn: Boolean;
  Column: TListColumn;
  Item: TListItem;
begin
  Search.SearchStart := 0;
  Search.SearchType := TDxSearchType(ComboBox1.ItemIndex);
  Search.Search(Edit1.Text);
  Label3.Caption := 'Total'+inttostr(Search.PageCount) + 'Page';

  ListView1.Columns.Clear;
  CreateColumn := False;
  for i := 0 to Search.Records.Count - 1 do
  begin
    Rec := Search.Records.Records[i];
    if not CreateColumn then
    begin
      Item := ListView1.Items.Add;
      Item.Caption := Rec.Values[0];
      CreateColumn := True;
      for j := 0 to Rec.FieldCount - 1 do
      begin
        Column := ListView1.Columns.Add;
        Column.Caption := Rec.Fields[j];
        if j <> 0 then
          Item.SubItems.Add(Rec.Values[j]);
      end;
    end
    else
    begin
      Item := ListView1.Items.Add;
      Item.Caption := Rec.Values[0];
      for j := 1 to Rec.FieldCount - 1 do
      begin
         Item.SubItems.Add(Rec.Values[j]);
      end;
    end;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  i,j: Integer;
  item: TListItem;
  Rec: TDxSearchRecord;
begin
  ListView1.Clear;
  Search.NextSearch;
  for i := 0 to Search.Records.Count - 1 do
  begin
    Rec := Search.Records.Records[i];
    Item := ListView1.Items.Add;
    Item.Caption := Rec.Values[0];
    for j := 1 to Rec.FieldCount - 1 do
    begin
       Item.SubItems.Add(Rec.Values[j]);
    end;
  end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Search.Free;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Search := TDxGoogleSearch.Create;
end;

end.

Source Code

You can skip to the end and leave a response. Pinging is currently not allowed.

Leave a Reply


Refresh



*


1 + nine =