thanhvg / emacs-howdoyou

Search and read stackoverflow and its sisters’ sites

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Select link via Helm

dvzubarev opened this issue · comments

At first, thank you, for your work!
It would be very handy to see all titles of fetched links in helm (or in any other completion framework) and select one of them to render in org-mode buffer. Is it hard to implement this?

This is a good idea.

If you you just want the link list, the variable howdoyou--links is there for you.

If you also want the titles, it's possible to extract them from the html search buffer. But that means we are scraping too much which search engines generally don't allow. I wouldn't want to go that far.

We can extract preview of a title from the links. I will try and test.

(defun helm-howdoyou--print-link (link)
  (promise-chain (howdoyou--promise-dom link)
    (then #'howdoyou--promise-so-answer)
    (then #'howdoyou--print-answer)
    (promise-catch (lambda (reason)
                     (message "catch error in n-link: %s" reason))))
  )

(defun helm-howdoyou--transform-candidate (candidate)
  (if-let* ((title-with-dashes
             (s-with (s-match "questions/[0-9]+/\\([-a-z]+\\)" candidate) cadr)))
      (s-replace "-" " " title-with-dashes)
    ""))

(defun helm-howdoyou--transform-candidates (candidates)
  (-zip-pair
   (mapcar #'helm-howdoyou--transform-candidate candidates)
   candidates))

(defun helm-howdoyou ()
  (interactive)
  (helm :sources (helm-build-sync-source "howdoyou links"
                   :candidates (helm-howdoyou--transform-candidates howdoyou--links)
                   :action (helm-make-actions "print" #'helm-howdoyou--print-link))
        :buffer "*helm howdoyou*"))

Your solution would work too but getting title from the hard links as you may have found is not always right.

But I take back my thought on scraping limit. I saw this python https://github.com/MarioVilas/googlesearch today and they have been scraping a lot more without any issues.

For ivy/counsel users out there, this is how would you get helm-howdoyou equivalent with counsel.

(defun counsel-howdoyou ()
"Select one of the fetched howdoyou links by its title."
  (interactive)
  (ivy-read "Choose links: "
            (helm-howdoyou--transform-candidates howdoyou--links)
            :action (lambda (x)
                      (helm-howdoyou--print-link (cdr x)))
            :caller 'aj/counsel-howdoto)) 

you still need all first 3 functions from above:

  • helm-howdoyou--print-link
  • helm-howdoyou--transform-candidate
  • helm-howdoyou--transform-candidates

but there is no actual dependency on helm inside of them, that is just their name.