[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/xkcd c816cfa893 12/82: Add caching and offline support.
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/xkcd c816cfa893 12/82: Add caching and offline support. |
Date: |
Mon, 12 Dec 2022 21:59:50 -0500 (EST) |
branch: elpa/xkcd
commit c816cfa893c0dcb4315834753239964cf01d5372
Author: Vibhav Pant <vibhavp@gmail.com>
Commit: Vibhav Pant <vibhavp@gmail.com>
Add caching and offline support.
---
README.md | 4 +---
emacs-xkcd.el | 70 +++++++++++++++++++++++++++++------------------------------
2 files changed, 36 insertions(+), 38 deletions(-)
diff --git a/README.md b/README.md
index 2b03fe52e9..703a125860 100644
--- a/README.md
+++ b/README.md
@@ -28,6 +28,4 @@ to your .emacs file.
Some comics using a different image extensions do not seem to load.
#TODO
-Add proper caching and offline support.
-
-Add support for custom faces.
\ No newline at end of file
+Add support for custom faces.
diff --git a/emacs-xkcd.el b/emacs-xkcd.el
index f491124322..00d9c03de4 100644
--- a/emacs-xkcd.el
+++ b/emacs-xkcd.el
@@ -1,22 +1,3 @@
-;;; emacs-xkcd.el --- View xkcd from Emacs
-;;; Copyright 2014 Vibhav Pant <vibhavp@gmail.com>
-
-;; This file is not apart of GNU Emacs.
-
-;; GNU Emacs is free software; you can redistribute it and/or modify
-;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 3, or (at your option)
-;; any later version.
-
-;; GNU Emacs is distributed in the hope that it will be useful,
-;; but WITHOUT ANY WARRANTY; without even the implied warranty of
-;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-;; GNU General Public License for more details.
-
-;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING. If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
(require 'json)
(require 'url)
@@ -27,8 +8,8 @@
(defgroup xkcd nil
"A xkcd reader for Emacs")
-(defcustom xkcd-cache "/tmp/"
- "Directory to cache images to"
+(defcustom xkcd-cache-dir "~/.emacs.d/xkcd/"
+ "Directory to cache images and json files to."
:group 'xkcd
:type 'directory)
@@ -43,23 +24,42 @@
(define-key map (kbd "C-c t") 'xkcd-alt-text)
map))
-(defun xkcd-get-json (url)
- (let ((buffer (url-retrieve-synchronously url))
- (json nil))
- (with-current-buffer buffer
- (goto-char (point-min))
- (re-search-forward "^$")
- (setq json (buffer-substring-no-properties (+ (point) 1) (point-max)))
- (kill-buffer (current-buffer)))
- json))
+(defun xkcd-get-json (url &optional num)
+ (let ((json nil))
+ (let ((file (concat xkcd-cache-dir (number-to-string num) ".json")))
+ (if (file-exists-p file)
+ (with-current-buffer (find-file-literally file) ;; File already
exists in the cache
+ (setq json (buffer-substring-no-properties (point-min) (point-max)))
+ (kill-buffer (current-buffer))
+ json)
+ (let ((buffer (url-retrieve-synchronously url)))
+ (with-current-buffer buffer
+ (goto-char (point-min))
+ (re-search-forward "^$")
+ (setq json (buffer-substring-no-properties (+ (point) 1)
(point-max)))
+ (kill-buffer (current-buffer)))
+ json)))))
(defun xkcd-download (url num)
"Download the image linked by URL. If the file arleady exists, do nothing"
- (let ((name (concat xkcd-cache (number-to-string num) ".png")))
+ ;;check if the cache directory exists
+ (if (not (file-exists-p xkcd-cache-dir))
+ (make-directory xkcd-cache-dir))
+ (let ((name (concat xkcd-cache-dir (number-to-string num) ".png")))
(if (file-exists-p name)
nil
(url-copy-file url name))))
+(defun xkcd-cache-json (num json-string)
+ "Save comic json to cache directory"
+ (let ((name (concat xkcd-cache-dir (number-to-string num) ".json")))
+ (if (file-exists-p name)
+ nil
+ (with-current-buffer (find-file name)
+ (insert json-string)
+ (save-buffer)
+ (kill-buffer (current-buffer))))))
+
(defun xkcd-get (num)
"Get the xkcd number NUM"
(interactive "nEnter comic number: ")
@@ -74,7 +74,7 @@
(let ((out (if (eq num nil)
(xkcd-get-json "http://xkcd.com/info.0.json")
(xkcd-get-json (concat "http://xkcd.com/" (number-to-string num)
- "/info.0.json"))))
+ "/info.0.json") num)))
(img nil)
(num nil)
(title nil))
@@ -88,12 +88,13 @@
(cdr (assoc 'safe_title (json-read-from-string out)))))
(insert (concat title "\n"))
(insert-image (create-image
- (concat xkcd-cache
+ (concat xkcd-cache-dir
(number-to-string
(cdr
(assoc 'num (json-read-from-string out)))) ".png")
'png))
(if (eq xkcd-cur nil)
(setq xkcd-cur (cdr (assoc 'num (json-read-from-string out)))))
+ (xkcd-cache-json num out)
(setq xkcd-alt (cdr (assoc 'alt (json-read-from-string out))))
(read-only-mode)
(message title)))
@@ -112,7 +113,7 @@
"Show random xkcd"
(interactive)
(xkcd-get (random (cdr (assoc 'num (json-read-from-string
- (get-json
"http://xkcd.com/info.0.json")))))))
+ (xkcd-get-json
"http://xkcd.com/info.0.json")))))))
(defun xkcd-get-latest ()
"Get the latest xkcd"
@@ -127,4 +128,3 @@
(provide 'emacs-xkcd)
;;; emacs-xkcd.el ends here
-
- [nongnu] branch elpa/xkcd created (now 80011da2e7), ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 51bf8b0d49 05/82: Rename functions, make json retrieving function smaller., ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd fcedf74f71 01/82: Initial commit, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 620f3417cf 08/82: Update README.md, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd d890179cde 09/82: Add xkcd-download, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd add1413ee9 11/82: Correct typo in readme., ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd c816cfa893 12/82: Add caching and offline support.,
ELPA Syncer <=
- [nongnu] elpa/xkcd 736b1c9abf 02/82: Initial commit, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 0f1abaebf5 14/82: typo: keybinding is t not r, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd f7b5157cdd 03/82: * README.md: Fix directory path, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 7ceb6cfb46 04/82: Update README.md, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 3acd31542b 06/82: Fix up search in xkcd-get-json. Add new line to README.md., ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 3a1d89e234 07/82: Add screenshot, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd ef2ae149e6 10/82: Add TODO to docs., ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 637f3d546e 15/82: Merge pull request #1 from oylenshpeegul/patch-1, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd 51a00504a1 19/82: Release Version 1.0, ELPA Syncer, 2022/12/12
- [nongnu] elpa/xkcd b409d07459 24/82: Add travis support., ELPA Syncer, 2022/12/12