-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconsult-web-google.el
111 lines (93 loc) · 4.69 KB
/
consult-web-google.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
;;; consult-web-google.el --- Consulting Google -*- lexical-binding: t -*-
;; Copyright (C) 2024 Armin Darvish
;; Author: Armin Darvish
;; Maintainer: Armin Darvish
;; Created: 2024
;; Version: 0.1
;; Package-Requires: ((emacs "28.1") (consult "1.1"))
;; Homepage: https://github.com/armindarvish/consult-web
;; Keywords: convenience
;;; Commentary:
;;; Code:
(require 'consult-web)
(defvar consult-web-google-search-url "https://www.google.com/search")
(defvar consult-web-google-customsearch-api-url "https://www.googleapis.com/customsearch/v1")
(defcustom consult-web-google-customsearch-key nil
"Key for Google custom search API
See URL `https://developers.google.com/custom-search/' and URL `https://developers.google.com/custom-search/v1/introduction' for details"
:group 'consult-web
:type '(choice (const :tag "API Key" string)
(function :tag "Custom Function")))
(defcustom consult-web-google-customsearch-cx nil
"CX for Google custom search API
See URL `https://developers.google.com/custom-search/' and URL `https://developers.google.com/custom-search/v1/introduction' for details"
:group 'consult-web
:type '(choice (const :tag "CX String" string)
(function :tag "Custom Function")))
(cl-defun consult-web--google-fetch-results (input &rest args &key count page filter &allow-other-keys)
"Fetches search results for INPUT from “Google custom search” service.
COUNT is passed as num in query parameters.
(* PAGE COUNT) is passed as start in query paramters.
Refer to URL `https://programmablesearchengine.google.com/about/' and `https://developers.google.com/custom-search/' for more info.
"
(let* ((count (or (and (integerp count) count)
(and count (string-to-number (format "%s" count)))
consult-web-default-count))
(page (or (and (integerp page) page)
(and page (string-to-number (format "%s" page)))
consult-web-default-page))
(filter (or (and (integerp filter) filter)
(and filter (string-to-number (format "%s" filter)))
1))
(filter (if (member filter '(0 1)) filter 1))
(count (min count 10))
(page (+ (* page count) 1))
(params `(("q" . ,(replace-regexp-in-string " " "+" input))
("key" . ,(consult-web-expand-variable-function consult-web-google-customsearch-key))
("cx" . ,(consult-web-expand-variable-function consult-web-google-customsearch-cx))
("gl" . "en")
("filter" . ,(format "%s" filter))
("num" . ,(format "%s" count))
("start" . ,(format "%s" page))))
(headers '(("Accept" . "application/json")
("Accept-Encoding" . "gzip")
("User-Agent" . "consult-web (gzip)"))))
(funcall consult-web-retrieve-backend
consult-web-google-customsearch-api-url
:params params
:headers headers
:parser
(lambda ()
(goto-char (point-min))
(let* ((results (gethash "items" (json-parse-buffer)))
(items (mapcar (lambda (item) `(:url ,(format "%s" (gethash "link" item)) :title ,(format "%s" (gethash "title" item)) :snippet ,(string-trim (format "%s" (gethash "snippet" item))))) results)))
(cl-loop for a in items
collect
(let ((table (make-hash-table :test 'equal)))
(puthash :url
(plist-get a :url) table)
(puthash :search-url (consult-web--make-url-string consult-web-google-search-url params '("key" "cx" "gl"))
table)
(puthash :title
(plist-get a :title) table)
(puthash :source "Google"
table)
(puthash :query input
table)
(puthash :snippet (plist-get a :snippet) table)
table
)
))))))
(consult-web-define-source "Google"
:narrow-char ?g
:face 'consult-web-engine-source-face
:request #'consult-web--google-fetch-results
:preview-key consult-web-preview-key
:search-history 'consult-web--search-history
:selection-history 'consult-web--selection-history
:dynamic 'both
)
;;; provide `consult-web-google' module
(provide 'consult-web-google)
(add-to-list 'consult-web-sources-modules-to-load 'consult-web-google)
;;; consult-web-google.el ends here