Created
August 30, 2018 09:52
-
-
Save nico202/edf2aaad2193289d94981f1640bbe2b6 to your computer and use it in GitHub Desktop.
giallozzafferano
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; org-chef-giallozafferano.el --- org-chef giallozafferano fetching. -*- lexical-binding: t; -*- | |
;; Copyright (C) 2018 Nicolò Balzarotti | |
;; Author: Nicolò Balzarotti <[email protected]> | |
;; URL: https://github.com/Chobbes/org-chef | |
;; Created: 2018 | |
;; Copyright 2018 Nicolò Balzarotti | |
;; Permission is hereby granted, free of charge, to any person | |
;; obtaining a copy of this software and associated documentation | |
;; files (the "Software"), to deal in the Software without | |
;; restriction, including without limitation the rights to use, copy, | |
;; modify, merge, publish, distribute, sublicense, and/or sell copies | |
;; of the Software, and to permit persons to whom the Software is | |
;; furnished to do so, subject to the following conditions: | |
;; The above copyright notice and this permission notice shall be | |
;; included in all copies or substantial portions of the Software. | |
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
;; SOFTWARE. | |
;;; Commentary: | |
;; Functions for fetching information from giallozafferano.com. | |
;;; Code: | |
(require 'org-chef-utils) | |
(require 'dom) | |
(defun org-chef-giallozafferano-extract-name (dom) | |
"Get the name of a recipe from a giallozafferano DOM." | |
(dom-text (car (dom-by-class dom "^fn$")))) | |
(defun org-chef-giallozafferano-extract-ingredients (dom) | |
"Get the ingredients for a recipe from a giallozafferano DOM." | |
nil) | |
;; This partially works | |
;; (mapcar #'(lambda (n) (concat (replace-regexp-in-string "\\s-+" " " (dom-texts n) ""))) | |
;; (dom-by-class dom "^ingredient$"))) | |
(defun org-chef-giallozafferano-extract-servings (dom) | |
"Get the number of servings for a recipe from a giallozafferano DOM." | |
(dom-text (dom-by-tag (dom-by-class dom "^yield$") 'strong))) | |
(defun org-chef-giallozafferano-extract-prep-time (dom) | |
"Get the amount of prep-time for a recipe from a giallozafferano DOM." | |
(dom-text (dom-by-tag (dom-by-class dom "^preptime$") 'strong))) | |
(defun org-chef-giallozafferano-extract-cook-time (dom) | |
"Get the amount of cook-time for a recipe from a giallozafferano DOM." | |
(dom-text (dom-by-tag (dom-by-class dom "^cooktime$") 'strong))) | |
;; Not provided, is it equal to prep + cook? | |
;; (defun org-chef-giallozafferano-extract-ready-in (dom) | |
;; "Get the total amount of time for a recipe from a giallozafferano DOM." | |
;; (dom-texts (car (dom-elements dom 'itemprop "^totalTime$")))) | |
(defun org-chef-giallozafferano-extract-directions (dom) | |
"Get the directions for a recipe from a giallozafferano DOM." | |
(org-chef-remove-empty-strings | |
(mapcar #'(lambda (n) (string-trim (dom-text n))) | |
(dom-by-tag (dom-by-class dom "^right-push$") 'p)))) | |
(defun org-chef-giallozafferano-fetch (url) | |
"Given an ricette.giallozafferano.it URL, retrieve the recipe information. | |
This returns an alist with the following keys: | |
- name | |
- ingredients | |
- servings | |
- prep-time | |
- cook-time | |
;; - ready-in | |
- directions | |
- source-url" | |
(with-current-buffer (url-retrieve-synchronously url) | |
(let ((dom (libxml-parse-html-region (point-min) (point-max)))) | |
`((name . ,(org-chef-giallozafferano-extract-name dom)) | |
(ingredients . ,(org-chef-giallozafferano-extract-ingredients dom)) | |
(servings . ,(org-chef-giallozafferano-extract-servings dom)) | |
(prep-time . ,(org-chef-giallozafferano-extract-prep-time dom)) | |
(cook-time . ,(org-chef-giallozafferano-extract-cook-time dom)) | |
;; (ready-in . ,(org-chef-giallozafferano-extract-ready-in dom)) | |
(directions . ,(org-chef-giallozafferano-extract-directions dom)) | |
(source-url . ,url)))))) | |
(provide 'org-chef-giallozafferano) | |
;;; org-chef-giallozafferano.el ends here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment