Created
December 15, 2012 01:17
-
-
Save bo0ts/4290237 to your computer and use it in GitHub Desktop.
This file contains 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
(require 'url) | |
(defun get-and-parse-json (url) | |
(interactive) | |
(with-current-buffer (url-retrieve-synchronously url) | |
(goto-char (point-min)) | |
(re-search-forward "^$") | |
(json-read))) | |
(defun get-movie-json (title &optional year) | |
(let ((url (concat "http://omdbapi.com/?t=" (url-hexify-string title)))) | |
(if (consp year) | |
(get-and-parse-json (concat url "&y=" (number-to-string year))) | |
(get-and-parse-json url)))) | |
(defun get-poster (url id folder) | |
(let ((filename (concat (file-name-as-directory folder) id ".jpg"))) | |
(with-current-buffer (url-retrieve-synchronously url) | |
(goto-char (point-min)) | |
(re-search-forward "^$") | |
(kill-region (point-min) (point)) | |
(kill-line) ;; necessary to get rid of the empty line | |
(write-file (concat (file-name-as-directory folder) id ".jpg") nil) | |
) | |
filename)) | |
(defun org-from-imdb-json (jsonmovie) | |
(let* ((imdbID (cdr (assoc 'imdbID jsonmovie))) | |
(poster (cdr (assoc 'Poster jsonmovie)))) | |
(concat "* [[imdb:" imdbID "][" (cdr (assoc 'Title jsonmovie)) "]]\n" | |
":PROPERTIES:\n" | |
":Director: " (cdr (assoc 'Director jsonmovie)) "\n" | |
":Year: " (cdr (assoc 'Year jsonmovie)) "\n" | |
":Actors: " (cdr (assoc 'Genre jsonmovie)) "\n" | |
":Genre: " (cdr (assoc 'Actors jsonmovie)) "\n" | |
":Plot: " (cdr (assoc 'Plot jsonmovie)) "\n" | |
(if (string-equal poster "N/A") | |
(identity ":Poster: N/A\n" ) | |
(concat ":Poster: " "[[" (get-poster poster imdbID "~/moviedb/posters/") "]]" "\n")) | |
":END:" | |
))) | |
(defun movie-as-org (title) | |
(interactive "sMovie Title: ") | |
(save-excursion (insert (org-from-imdb-json (get-movie-json title)))) | |
(org-mark-element) | |
(indent-region (region-beginning) (region-end))) | |
(defun movie-from-imdb (imdburl) | |
(interactive "sUrl: ") | |
(let ((imdbid (progn | |
(string-match "http://www.imdb.com/title/\\([^/]*\\)" imdburl) | |
(match-string 1 imdburl)))) | |
(save-excursion (insert (org-from-imdb-json (get-and-parse-json (concat "http://omdbapi.com/?i=" imdbid))))) | |
(org-mark-element) | |
(indent-region (region-beginning) (region-end)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment