Custom Google Search View

    Do you know how much data you download from the network each time to see this page?



    If no browser cache, then 600 kilobytes (in gzip-e) and 38 requests. If with a cache, then 70 kilobytes and 7-8 requests.



    Do you know what 600 kilobytes is? These are both volumes of "War and Peace" by Lev Nikolayevich Tolstoy in the same gzip-e.

    Of course, it is gratifying that Google Inc. feeds us with so much useful information. There is no need to even speak about the search results themselves - here you can find pictures, reference information from Wikipedia, hours of operation, a map, and reviews. But sometimes you want something simple and concise. Without raped links, without selling to a fellow major, third-party marketers of your web campines. In order not to slow down on outdated smartphones and overloaded mobile networks.

    You can, of course, query requests to the search engine through your script, with vivisection of all unnecessary, but, fortunately, Google itself offers an elegant solution - access to your Search API with the delivery of pure results in JSON or XML format.

    Here is part of the JSON response for example:

    Json
    {
     "kind": "customsearch#search",
     "url": {
      "type": "application/json",
      "template": "https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json"
     },
     "queries": {
      "request": [
       {
        "title": "Google Custom Search - habr",
        "totalResults": "327000",
        "searchTerms": "habr",
        "count": 10,
        "startIndex": 1,
        "inputEncoding": "utf8",
        "outputEncoding": "utf8",
        "safe": "off",
        "cx": "0019687164026253:oi9ssr7ci6e"
       }
      ],
      "nextPage": [
       {
        "title": "Google Custom Search - habr",
        "totalResults": "327000",
        "searchTerms": "habr",
        "count": 10,
        "startIndex": 11,
        "inputEncoding": "utf8",
        "outputEncoding": "utf8",
        "safe": "off",
        "cx": "00156687164026253:oi9ssr7ci6e"
       }
      ]
     },
     "context": {
      "title": "v-gornom.ru"
     },
     "searchInformation": {
      "searchTime": 0.531851,
      "formattedSearchTime": "0,53",
      "totalResults": "327000",
      "formattedTotalResults": "327 000"
     },
     "items": [
      {
       "kind": "customsearch#result",
       "title": "Лучшие публикации за сутки / Хабр",
       "htmlTitle": "Лучшие публикации за сутки / \u003cb\u003eХабр\u003c/b\u003e",
       "link": "https://habr.com/",
       "displayLink": "habr.com",
       "snippet": "Хабр – самое крупное в Рунете сообщество людей, занятых в индустрии \nвысоких технологий. Уникальная аудитория, свежая информация, ...",
       "htmlSnippet": "\u003cb\u003eХабр\u003c/b\u003e – самое крупное в Рунете сообщество людей, занятых в индустрии \u003cbr\u003e\nвысоких технологий. Уникальная аудитория, свежая информация, ...",
       "cacheId": "Wg3CgcvcAxUJ",
       "formattedUrl": "https://habr.com/",
       "htmlFormattedUrl": "https://\u003cb\u003ehabr\u003c/b\u003e.com/",
       "pagemap": {
        "cse_thumbnail": [
         {
          "width": "311",
          "height": "162",
          "src": "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcT9MbmejtANdWSuXxH7pxKqWNFFRzPiTQuLmhWk5Tc5-Y-AgUgPrwEz0MV9"
         }
        ],
        "metatags": [
         {
          "viewport": "width=1024",
          "fb:app_id": "444736788986613",
          "og:type": "website",
          "fb:pages": "472597926099084",
          "og:site_name": "Хабр",
          "og:image": "https://habr.com/images/habr.png",
          "og:image:width": "1200",
          "og:image:height": "628",
          "og:title": "Лучшие публикации за сутки / Хабр",
          "og:description": "Хабр – самое крупное в Рунете сообщество людей, занятых в индустрии высоких технологий. Уникальная аудитория, свежая информация, конструктивное общение и коллективное творчество – всё это делает Хабр самым оригинальным IT-проектом в России.",
          "yandex-verification": "71593b225aeafc4e",
          "referrer": "unsafe-url",
          "pocket-site-verification": "ed24b2b9721edf0a282c5b4a3232c4",
          "application-name": "Хабр",
          "msapplication-tilecolor": "#77a2b6",
          "theme-color": "#77a2b6"
         }
        ],
        "cse_image": [
         {
          "src": "https://habr.com/images/habr.png"
         }
        ]
       }
      },
      {
       "kind": "customsearch#result",
       "title": "Хабр — Википедия",
       "htmlTitle": "\u003cb\u003eХабр\u003c/b\u003e — Википедия",
       "link": "https://ru.wikipedia.org/wiki/%D0%A5%D0%B0%D0%B1%D1%80",
       "displayLink": "ru.wikipedia.org",
       "snippet": "Хабр (бывший Хабрахабр) — русскоязычный веб-сайт в формате \nколлективного блога с элементами новостного сайта, созданный для \nпубликации ...",
       "htmlSnippet": "\u003cb\u003eХабр\u003c/b\u003e (бывший \u003cb\u003eХабрахабр\u003c/b\u003e) — русскоязычный веб-сайт в формате \u003cbr\u003e\nколлективного блога с элементами новостного сайта, созданный для \u003cbr\u003e\nпубликации ...",
       "cacheId": "lB7MkUOxB7gJ",
       "formattedUrl": "https://ru.wikipedia.org/wiki/Хабр",
       "htmlFormattedUrl": "https://ru.wikipedia.org/wiki/\u003cb\u003eХабр\u003c/b\u003e",
       "pagemap": {
        "cse_thumbnail": [
         {
          "width": "220",
          "height": "130",
          "src": "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQqJU0itnwYGZAd6Z5aMJO0izPfF7cab6pc40vm0sujOP2dSgkP-UB0Je1T"
         }
        ],
        "metatags": [
         {
          "referrer": "origin",
          "og:image": "https://upload.wikimedia.org/wikipedia/ru/7/7f/Habrahabr_logo.png"
         }
        ],
        "cse_image": [
         {
          "src": "https://upload.wikimedia.org/wikipedia/ru/7/7f/Habrahabr_logo.png"
         }
        ]
       }
      },


    In this article, we’ll write View for Google Custom Search, which will have the power of Google and satisfy our mundane desires.

    To access the Google Custom Search API, you first need to get the ID of your search engine. To do this, you need to register a custom search engine on the cse.google.com page .

    We go to the site, add a search engine, specify an arbitrary site, go to the control panel and get the search engine ID.

    Now you need to get a token to access the Custom Search API.

    Go to console.cloud.google.com(it is assumed that you already have accounts on both sites), create a project, select “API and services” → “Control panel” → “Enable API and services” in the left side menu, search for “Custom Search API” there, choose, enable, then create the credentials - namely, the "API Key". You can limit the use of this key only on your IP, you can not do this.

    Everything. Now we can send Google requests with this line:

    https://www.googleapis.com/customsearch/v1?key={apikey}&cx={sid}&q=query

    Where {apikey} is an access token, {sid} is a search identifier.

    In response, we get neat JSON.

    With the help of a simple php script, we get our View, even with some approximation to the original. First page:

    Search results:



    First page - 2 Kb, with search results - 6 Kb.

    It's not just about minimalism. Google in the JSON response gives a bunch of interesting information and allows you to specify a request with no less than a bunch of parameters. Read more here . You can even do what the main page of the search engine does not allow.

    Of course, there are also disadvantages - a limit of 100 requests per day. The rest is for dollars.

    By the way, you can use our php script on Google too and also absolutely free of charge - at https://appengine.google.com . True, there will already have to suffer considerably - FTP, web file manager and root directories of the site for Google rudiments. Therefore, it is necessary to turn the php script into an application, shove it into the engine and use a special console to cover the clouds.

    The task is actually not trivial. No wonder Google is going to introduce a new certification - Google Certified User.

    Also popular now: