-
Notifications
You must be signed in to change notification settings - Fork 1
/
opensub.el
154 lines (131 loc) · 5.89 KB
/
opensub.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
;;; opensub.el --- Search and download from open-subtitles -*- lexical-binding: t; -*-
;; Copyright (C) 2023 Daniel Fleischer
;; Author: Daniel Fleischer <danflscr@gmail.com>
;; Keywords: multimedia
;; Homepage: https://github.com/danielfleischer/opensub
;; Version: 1.0.0
;; Package-Requires: ((emacs "25.1"))
;; This program 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 of the License, or
;; (at your option) any later version.
;; This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Package to query and download subtitles from opensubtitles.com.
;;
;; Set up an account in opensubtitles.com; get an API key and set it
;; in `opensub-api-key'.
;;
;; To run, call `opensub', provide a query string such as "matrix" or
;; "office s04e13". Select the relevant file among the options, and it
;; will be downloaded to `opensub-download-directory'.
;;; Code:
(require 'cl-lib)
(require 'json)
(require 'webjump)
(defgroup opensub nil
"Search and fetch subtitles from opensubtitles.com."
:group 'multimedia)
(defcustom opensub-api-key ""
"API key for opensubtitles.com."
:type 'string)
(defcustom opensub-download-directory "~/Downloads/"
"Directory where subtitles will be downloaded to."
:type 'directory)
(defcustom opensub-languages '("en")
"Languages to search for; 2 letter codes."
:type '(repeat string))
(defcustom opensub-column-width 72
"Width of the column in the completion list."
:type 'integer)
(defvar opensub--json-fn
(if (version<= "27.1" emacs-version)
(lambda () (json-parse-buffer :object-type 'alist))
#'json-read)
"JSON parsing function.")
(defun opensub--curl (url)
"Given a URL, return parsed json."
(with-current-buffer
(url-retrieve-synchronously url)
(goto-char (point-min))
(search-forward "\n\n")
(delete-region (point-min) (point))
(funcall opensub--json-fn)))
(defun opensub--curl-retrieve (query)
"Run a QUERY on opensubtitles.com. Return parsed json."
(let* ((clean-query (webjump-url-encode query))
(url-request-method "GET")
(url-request-extra-headers
`(("Api-Key" . ,opensub-api-key)
("Content-Type" . "application/json")))
(url (format (concat "https://api.opensubtitles.com/api/v1/subtitles?query=%s"
(format "&languages=%s"
(string-join (sort opensub-languages #'string<) ",")))
clean-query)))
(opensub--curl url)))
(defun opensub--annotation (cand)
(let* ((rest (cdr (assoc cand minibuffer-completion-table))))
(concat " "
(propertize (alist-get 'language rest) 'face 'success) " "
(propertize (string-pad (alist-get 'rating rest) 4) 'face 'warning) " "
(propertize (alist-get 'full_name rest)))))
(defun opensub--get-file-id (results)
"Given query RESULTS, return the file id of user-selected item."
(let* ((items (alist-get 'data results))
(completion-extra-properties (list :annotation-function 'opensub--annotation))
(options (cl-mapcar
(lambda (item)
`(,(substring (string-pad (let-alist item .attributes.release)
opensub-column-width ? )
0 opensub-column-width)
. ((language . ,(let-alist item .attributes.language))
(rating . ,(number-to-string (let-alist item .attributes.ratings)))
(year . ,(number-to-string (let-alist item .attributes.feature_details.year)))
(full_name . ,(let-alist item .attributes.feature_details.movie_name)))))
items))
(option (string-trim-right (completing-read "Select: " options)))
(item (cl-find-if (lambda (x)
(string-search option (let-alist x .attributes.release)))
items)))
(alist-get 'file_id (aref (let-alist item .attributes.files) 0))))
(defun opensub--curl-get-download-info (media-id)
"Given a MEDIA-ID, generate a downloadable link.
Returns alist with link and file name.
Uses a POST call to generate a time-limited link.
May fail if exceeds daily usage limits."
(let* ((url-request-method "POST")
(url-request-extra-headers
`(("Api-Key" . ,opensub-api-key)
("Content-Type" . "application/json")))
(url-request-data
(format "{\"file_id\": %s}" media-id))
(response
(opensub--curl "https://api.opensubtitles.com/api/v1/download")))
(if (alist-get 'link response) response
(user-error (alist-get 'message response)))))
(defun opensub--download-subtitle (item)
"Given ITEM information, downloads subtitle to downloads dir."
(let* ((link (alist-get 'link item))
(filename (concat (file-name-as-directory
opensub-download-directory)
(alist-get 'file_name item))))
(unless (url-copy-file link filename)
(error "Couldn't download the subtitle. Try again"))
filename))
;;;###autoload
(defun opensub ()
"Run opensub."
(interactive)
(let* ((query (read-from-minibuffer "Search a show/movie: "))
(results (opensub--curl-retrieve query))
(item-id (opensub--get-file-id results))
(item (opensub--curl-get-download-info item-id))
(filename (opensub--download-subtitle item)))
(message "Subtitle downloaded: `%s'" filename)))
(provide 'opensub)
;;; opensub.el ends here