Skip to content

Instantly share code, notes, and snippets.

@chee
Created August 3, 2020 21:20
Show Gist options
  • Save chee/8f2ca3db4501f61585c8b9750322dc4b to your computer and use it in GitHub Desktop.
Save chee/8f2ca3db4501f61585c8b9750322dc4b to your computer and use it in GitHub Desktop.
(defun get-recipe-alist-from-url (url)
(require 'cl)
(require 'w3m)
(with-temp-buffer
(w3m-retrieve url)
(json-read-from-string
(nth 2 (car
(remove-if-not
(lambda (script) (rassoc "application/ld+json" (nth 1 script)))
(dom-by-tag (libxml-parse-html-region (point-min) (point-max)) 'script)))))))
(defun create-org-recipe-from-url () (interactive)
(let*
((url (read-string "what's the url? "))
(recipe (get-recipe-alist-from-url url))
(name (alist-get 'name recipe))
(instructions (alist-get 'recipeInstructions recipe))
(filename (concat "~/notes/recipes/" (replace-regexp-in-string "\\W" "-" (downcase name)) ".org"))
(lines ()))
(add-to-list 'lines (concat "* " name) t)
(add-to-list 'lines (concat ":source: " url))
(add-to-list 'lines "** Ingredients" t)
(add-to-list 'lines
(mapconcat
(lambda (ingredient) (concat "- " ingredient))
(alist-get 'recipeIngredient recipe) "\n") t)
(add-to-list 'lines "** Nutrition" t)
(mapc
(lambda (item)
(let
((field (symbol-name (car item)))
(value (cdr item)))
(unless (string-prefix-p "@" field)
(add-to-list 'lines
(concat "| " field " | " value " |") t))))
(alist-get 'nutrition recipe))
(add-to-list 'lines "** Method" t)
(if (stringp instructions)
(if (string-prefix-p "<" instructions)
(add-to-list 'lines
;;; TODO parse list and make it 1. 2. 3.
(concat "#+begin_example html\n"
instructions "\n#+end_example") t)
(add-to-list 'lines instructions t))
(let ((index 0))
(add-to-list 'lines
(mapconcat
(lambda (instruction) (setq index (1+ index)) (concat () (number-to-string index) ". " (alist-get 'text instruction)))
instructions "\n") t)))
(write-region
(mapconcat 'identity lines "\n")
nil
filename)
(find-file filename)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment