0

I quite like helm-google-suggest, but I would like it even more if it didn't connect to Google. Once you have your candidate, searching for it in a better search engine is as simple as this:

(setq helm-google-suggest-search-url "https://duckduckgo.com/lite/?q=%s&kp=1")

However, generating the suggestions themselves is not so easy. The DuckDuckGo API documentation is sparse, to say the least. The only thing I could find is https://ac.duckduckgo.com/ac/?q=%s,
which generates the suggestions, but does not accept the format=xml query that the other DuckDuckGo APIs accept. Instead, it always returns a JSON string.

Since helm-google-suggest expects its URL to return XML. This doesn't work:

(setq helm-google-suggest-url "https://duckduckgo.com/ac/?q=%s")  

An obvious solution would be to hack my own function that regex-replaces the JSON string to valid XML and hands it back to Helm, but before I do that, is there an easier way?

1 Answer 1

0

The code is specific to google, so what you're trying to do won't work. Here's something you can try based on the original helm-google-suggest command.

(defun helm-ddg-suggest-fetch (input)
  (let ((json-array-type 'list))
    (with-current-buffer
        (url-retrieve-synchronously
         (format "https://duckduckgo.com/ac/?q=%s" input) t)
       (goto-char url-http-end-of-headers)
       (json-read))))
    
(defun helm-ddg-suggest-set-candidates ()
  (let ((suggestions (mapcar 'cdar (helm-ddg-suggest-fetch helm-pattern))))
    (if (member helm-pattern suggestions) suggestions
      (append suggestions
              (list (cons (format "Search for '%s' on DuckDuckGo" helm-input)
                          helm-input))))))

(defun helm-ddg-suggest ()
  (interactive)
  (helm-other-buffer
   (helm-build-sync-source "DuckDuckGo Suggest"
     :candidates 'helm-ddg-suggest-set-candidates
     :action (lambda (s)
               (browse-url 
                (format helm-surfraw-duckduckgo-url 
                        (url-hexify-string s))))
     :match-dynamic t
     :requires-pattern 3)
   "*helm duckduckgo*"))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.