📜 ⬆️ ⬇️

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 Leo 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 talk 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 being sold to a fellow major by 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 an 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 a View for Google Custom Search that 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, register a custom search engine at cse.google.com

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

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

We 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, look for “Custom Search API” there, select, enable, then create credentials - namely, “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:

Searching results:



The first page - 2Kb, with search results - 6Kb.

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 at least 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.

Source: https://habr.com/ru/post/421173/


All Articles