summaryrefslogtreecommitdiffstats
path: root/ymh-emacs/ymh-ebib.el
diff options
context:
space:
mode:
authorYann Herklotz <git@yannherklotz.com>2022-11-27 12:08:11 +0000
committerYann Herklotz <git@yannherklotz.com>2022-11-27 12:08:11 +0000
commitad161b70c317cc766c6ec42064cb6410136c7905 (patch)
tree56b914aabe00e6526e7ba1cdd5aeb9a3ab4da732 /ymh-emacs/ymh-ebib.el
parent3cc9791003364c338cc6d219c5f144210aa76bef (diff)
downloadymh-emacs-ad161b70c317cc766c6ec42064cb6410136c7905.tar.gz
ymh-emacs-ad161b70c317cc766c6ec42064cb6410136c7905.zip
Take more configuration into packages
Diffstat (limited to 'ymh-emacs/ymh-ebib.el')
-rw-r--r--ymh-emacs/ymh-ebib.el113
1 files changed, 113 insertions, 0 deletions
diff --git a/ymh-emacs/ymh-ebib.el b/ymh-emacs/ymh-ebib.el
new file mode 100644
index 0000000..38cc9c0
--- /dev/null
+++ b/ymh-emacs/ymh-ebib.el
@@ -0,0 +1,113 @@
+;;; ymh-ebib.el --- Extensions to the Ebib package -*- lexical-binding: t; -*-
+
+;; Author: Yann Herklotz <git@yannherklotz.com>
+;; Package-Requires: ((emacs "26.1") (ebib "2.0"))
+
+;;; Commentary:
+
+;; This file contains extensions to ebib.
+
+;;; Code:
+
+(require 'ebib)
+
+(defun ymh-ebib-sci-hub-pdf-url (doi)
+ "Get url to the pdf from SCI-HUB using DOI."
+ (setq *doi-utils-pdf-url* (concat "https://sci-hub.hkvisa.net/" doi) ;captcha
+ *doi-utils-waiting* t
+ )
+ ;; try to find PDF url (if it exists)
+ (url-retrieve (concat "https://sci-hub.hkvisa.net/" doi)
+ (lambda (_)
+ (goto-char (point-min))
+ (while (search-forward-regexp
+ "\\(https:\\|sci-hub.hkvisa.net/downloads\\).+download=true'" nil t)
+ (let ((foundurl (match-string 0)))
+ (message foundurl)
+ (if (string-match "https:" foundurl)
+ (setq *doi-utils-pdf-url* foundurl)
+ (setq *doi-utils-pdf-url* (concat "https:" foundurl))))
+ (setq *doi-utils-waiting* nil))))
+ (while *doi-utils-waiting* (sleep-for 0.1))
+ (replace-regexp-in-string "\\\\" "" *doi-utils-pdf-url*))
+
+(defun ymh-ebib-acm-pdf-url (doi)
+ "Retrieve a DOI pdf from the ACM."
+ (concat "https://dl.acm.org/doi/pdf/" doi))
+
+(defun ymh-ebib-ieee-pdf-url (doi)
+ "Retrieve a DOI pdf from the IEEE."
+ (when (string-match "\\.\\([0-9]*\\)$" doi)
+ (let ((doi-bit (match-string 1 doi)))
+ (concat "https://ieeexplore.ieee.org/stampPDF/getPDF.jsp?tp=&arnumber=" doi-bit "&ref="))))
+
+(defun ymh-ebib-springer-pdf-url (doi)
+ "Retrieve a DOI pdf from the Springer."
+ (concat "https://link.springer.com/content/pdf/" doi ".pdf"))
+
+(defun ymh-ebib-arxiv-pdf-url (epr)
+ (concat "https://arxiv.org/pdf/" epr ".pdf"))
+
+(defun ymh-ebib--download-pdf-from-doi (key &optional doi publisher eprint journal organization url)
+ "Download pdf from doi with KEY name."
+ (let ((pub (or publisher ""))
+ (epr (or eprint ""))
+ (jour (or journal ""))
+ (org (or organization ""))
+ (link (or url "")))
+ (url-copy-file (cond
+ ((not doi) link)
+ ((or (string-match "ACM" (s-upcase pub))
+ (string-match "association for computing machinery" (s-downcase pub)))
+ (ymh-ebib-acm-pdf-url doi))
+ ((string-match "arxiv" (s-downcase pub))
+ (ymh-ebib-arxiv-pdf-url epr))
+ ((or (string-match "IEEE" (s-upcase pub))
+ (string-match "IEEE" (s-upcase jour))
+ (string-match "IEEE" (s-upcase org)))
+ (ymh-ebib-ieee-pdf-url doi))
+ ((string-match "springer" (s-downcase pub))
+ (ymh-ebib-springer-pdf-url doi))
+ (t (ymh-ebib-sci-hub-pdf-url doi)))
+ (concat (car ebib-file-search-dirs) "/" key ".pdf"))))
+
+(defun ymh-ebib-download-pdf-from-link (link key)
+ (url-copy-file link
+ (concat (car ebib-file-search-dirs) "/" key ".pdf")))
+
+(defun ymh-ebib-download-pdf-from-downloads (key)
+ (copy-file (concat "~/Downloads/" key ".pdf")
+ (concat (car ebib-file-search-dirs) "/" key ".pdf") t))
+
+(defun ymh-ebib-get-bib-from-doi (doi)
+ "Get the bibtex from DOI."
+ (shell-command (concat "curl -L -H \"Accept: application/x-bibtex; charset=utf-8\" "
+ "https://doi.org/" doi)))
+
+(defun ymh-ebib-download-pdf-from-doi ()
+ "Download a PDF for the current entry."
+ (interactive)
+ (let* ((key (ebib--get-key-at-point))
+ (doi (ebib-get-field-value "doi" key ebib--cur-db 'noerror 'unbraced 'xref))
+ (publisher (ebib-get-field-value "publisher" key ebib--cur-db 'noerror 'unbraced 'xref))
+ (eprinttype (ebib-get-field-value "eprinttype" key ebib--cur-db 'noerror 'unbraced 'xref))
+ (eprint (ebib-get-field-value "eprint" key ebib--cur-db 'noerror 'unbraced 'xref))
+ (journal (ebib-get-field-value "journal" key ebib--cur-db 'noerror 'unbraced 'xref))
+ (journaltitle (ebib-get-field-value "journaltitle" key ebib--cur-db 'noerror 'unbraced 'xref))
+ (organization (ebib-get-field-value "organization" key ebib--cur-db 'noerror 'unbraced 'xref))
+ (url (ebib-get-field-value "url" key ebib--cur-db 'noerror 'unbraced 'xref)))
+ (unless key
+ (error "[Ebib] No key assigned to entry"))
+ (ymh-ebib--download-pdf-from-doi key doi (or publisher eprinttype) eprint (or journal journaltitle) organization url)))
+
+(defun ymh-ebib-ebib-check-file ()
+ "Download a PDF for the current entry."
+ (interactive)
+ (let ((key (ebib--get-key-at-point)))
+ (unless (file-exists-p (concat (car ebib-file-search-dirs) "/" key ".pdf"))
+ (error "[Ebib] No PDF found."))
+ t))
+
+(provide 'ymh-ebib)
+
+;;; ymh-ebib.el ends here