aboutsummaryrefslogtreecommitdiff
path: root/ttrss.el
blob: 15a652fd9f5cfa10b38a705b145aaee3494f51db (plain) (blame)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
;;; ttrss.el --- Tiny Tiny RSS elisp bindings

;; Copyright (C) 2013 Pedro Silva

;; Author: Pedro Silva <psilva+git@pedrosilva.pt>
;; Created: 01 April 2013
;; Version: 1.7.5
;; Keywords: news, local
;; Homepage: https://github.com/pedros/ttrss.el
;; Package-Requires: ((emacs "23.1"))

;; This file is not part of GNU Emacs.

;; ttrss 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.

;; ttrss 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 ttrss.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Introduction

;; ttrss.el provides an interface to the Tiny Tiny RSS API. It targets
;; API level 5, and has been developed and tested against version
;; 1.7.5. As such, the versioning on this file tracks that of ttrss
;; itself.  You can read more about Tiny Tiny RSS at
;; <http://tt-rss.org/redmine/projects/tt-rss/wiki>.

;; Scope

;; The current version of ttrss.el covers all methods as documented at
;; <http://tt-rss.org/redmine/projects/tt-rss/wiki/JsonApiReference>.
;; It purposefully doesn't offer any additional higher level methods.

;; Installation

;; (add-to-list 'load-path "/path/to/parent/directory")
;; (require 'ttrss)

;; Usage

;; (setq ttrss-address "http://example.com/ttrss/api/"
;;       ttrss-user "user"
;;       ttrss-password "pass")

;; (let ((ttrss-sid (ttrss-login ttrss-address ttrss-user ttrss-password)))
;;   (if (ttrss-logged-in-p ttrss-address ttrss-sid)
;;       (with-current-buffer (switch-to-buffer "*ttrss reader*")
;; 	(erase-buffer)
;; 	(message "Logged in to %s as %s with password %s\n"
;; 		 ttrss-address
;; 		 ttrss-user
;; 		 ttrss-password)
;; 	(message "Server running version %s and API level %d\n"
;; 		 (ttrss-get-version ttrss-address ttrss-sid)
;; 		 (ttrss-get-api-level ttrss-address ttrss-sid))
;; 	(message "There are %s unread articles in %d feeds"
;; 		 (ttrss-get-unread ttrss-address ttrss-sid)
;; 		 (length (ttrss-get-feeds ttrss-address ttrss-sid :unread_only t)))
;; 	(insert "The grand <b>*ttrss* reader!</b>\n\n---\n\n")
;; 	(mapcar (lambda (pl) (insert (format "%s\n%s\n\n%s\n---\n"
;; 					     (plist-get pl :title)
;; 					     (plist-get pl :link)
;; 					     (plist-get pl :content))))
;; 		(ttrss-get-headlines ttrss-address ttrss-sid :feed_id -3 :limit 3 :show_content t))
;; 	(html2text))
;;     (message "Login failed")))

;; Design

;; I was motivated to write this library to support a new backend for
;; Gnus, 'nnttrss.el'. Its design is kept as functional as possible.
;; That means that it makes no use of global state, amongst other
;; things.

;; TODO

;; * Interactive versions of the methods employing global state:
;;   You'll note that there are, in fact, a number of variables defined
;;   at the top-level, namely 'ttrss-address', 'ttrss-user',
;;   'ttrss-password', 'ttrss-sid', 'ttrss-api-level', and
;;   'ttrss-server-version'. These are defined in anticipation of the
;;   next release, in which I plan to introduce interactive counterparts
;;   of most methods that do employ global state.

;; * variable 'nnttrss-debug':
;;   - Trace methods in the mini-buffer
;;   - Keep a log buffer with requests and responses data structures

;;; Change Log:

;; Version 0.0.1: First public release of this package.


;;; Code:

(eval-and-compile
  (autoload 'url-retrieve "url")
  (autoload 'url-retrieve-synchronously "url")
  (autoload 'json-read "json")
  (autoload 'json-encode "json"))

(defgroup ttrss nil
  "Tiny Tiny RSS elisp bindings"
  :group 'external
  :group 'communication
  :link '(url-link :tag "Tiny Tiny RSS"
		   "http://tt-rss.org/redmine/projects/tt-rss/wiki"))

(defcustom ttrss-address "http://localhost"
  "Address of the tt-rss server."
  :type 'string
  :group 'ttrss
  :link '(info-link "(url)http/https"))

(defcustom ttrss-user ""
  "Username to use for authentication to the tt-rss server."
  :type 'string
  :group 'ttrss)

(defcustom ttrss-password ""
  "Password to use for authentication to the tt-rss server."
  :type 'string
  :group 'ttrss)

(defvar ttrss-sid nil
  "Current session id, if any, set after successful login.")

(defvar ttrss-api-level nil
  "API version level, increased with each API functionality change.")

(defvar ttrss-server-version nil
  "Server version number.")


;;; Utilities

(defun ttrss-post-request (address property &rest content)
  "Post to ADDRESS and possibly retrieve PROPERTY from the response to CONTENT.
CONTENT must be a data structure that `json-encode' knows how to
encode as a JSON object.

Returns the JSON response as a property list (or, optionally, the
PROPERTY in the property list) if the response status is 0, nil
otherwise."
  (let ((url-request-method "POST")
	(url-request-data (json-encode content)))
    (with-current-buffer (url-retrieve-synchronously address)
      (ttrss-parse-request nil property))))

(defun ttrss-post-request-asynchronously (address &rest content)
  "Asynchronously post to ADDRESS the key-value pairs CONTENT.
Like 'ttrss-post-request', but perform request asynchronously
with 'ttrss-parse-request' as a callback."
  (let ((url-request-method "POST")
	(url-request-data (json-encode content)))
    (url-retrieve address 'ttrss-parse-request)))

(defun ttrss-parse-request (url-status &rest property)
  "Parse a url response buffer with URL-STATUS.
Return a property list of the response, or, optionally, the
PROPERTY value of said property list."
  (let ((json-object-type 'plist)
	(json-false nil))
    (goto-char (point-min))
    (search-forward-regexp "\n\n")
    (let* ((response (json-read))
	   (ttrss-status (plist-get response :status))
	   (content (plist-get response :content)))
      (if (= ttrss-status 1)
	  (user-error "API status error: %s"
		      (mapconcat 'downcase
				 (split-string (plist-get content :error) "_")
				 " "))
	(let ((value (or (plist-get content (car property)) content)))
	  value)))))


;;; Session management

(defun ttrss-login (address user password)
  "Login to the server at ADDRESS using USER and PASSWORD.
Returns a session id string."
  (ttrss-post-request address
		      :session_id
		      :op "login"
		      :user user
		      :password password))

(defun ttrss-logout (address sid)
  "Logout of the server at ADDRESS using SID.
Returns a status string (typically 'OK')."
  (ttrss-post-request address
		      :status
		      :op "logout"
		      :sid sid))

(defun ttrss-logged-in-p (address sid)
  "Return t if there is a valid session at ADDRESS with SID."
  (ttrss-post-request address
		      :status
		      :op "isLoggedIn"
		      :sid sid))


;;; Server statistics

(defun ttrss-get-api-level (address sid)
  "Return the API level at ADDRESS using SID."
  (ttrss-post-request address
		      :level
		      :op "getApiLevel"
		      :sid sid))

(defun ttrss-get-version (address sid)
  "Return the server version at ADDRESS using SID."
  (ttrss-post-request address
		      :version
		      :op "getVersion"
		      :sid sid))

(defun ttrss-get-config (address sid &optional property)
  "Return configuration property list of server at ADDRESS using SID.
Optionally, retrieve PROPERTY only.  The property list members are:

'icons_dir'
     Path to icons on the server filesystem.

'icons_url'
     Path to icons when requesting them over http.

'daemon_is_running'
     Whether update daemon is running.

'num_feeds'
     Amount of subscribed feeds."
  (ttrss-post-request address
		      property
		      :op "getConfig"
		      :sid sid))

(defun ttrss-get-pref (address sid preference)
  "Return value, at ADDRESS using SID, given by PREFERENCE.
PREFERENCE must be one of the following strings:

'allow_duplicate_posts'
    Allow duplicate posts.

'auto_assign_labels'
    Assign articles to labels automatically.

'blacklisted_tags'
    Blacklisted tags.

'cdm_auto_catchup'
    Automatically mark articles as read.

'cdm_expanded'
    Automatically expand articles in combined mode.

'combined_display_mode'
    Combined feed display.

'confirm_feed_catchup'
    Confirm marking feed as read.

'default_article_limit'
    Amount of articles to display at once.

'default_update_interval'
    Default interval between feed updates.

'digest_catchup'
    Mark articles in e-mail digest as read.

'digest_enable'
    Enable e-mail digest.

'digest_preferred_time'
    Try to send digests around specified time.

'enable_api_access'
    Enable external API.

'enable_feed_cats'
    Enable feed categories.

'feeds_sort_by_unread'
    Sort feeds by unread articles count.

'fresh_article_max_age'
    Maximum age of fresh articles (in hours).

'hide_read_feeds'
    Hide feeds with no unread articles.

'hide_read_shows_special'
    Show special feeds when hiding read feeds.

'long_date_format'
    Long date format.

'on_catchup_show_next_feed'
    On catchup show next feed.

'purge_old_days'
    Purge articles after this number of days.

'purge_unread_articles'
    Purge unread articles.

'reverse_headlines'
    Reverse headline order (oldest first).

'short_date_format'
    Short date format.

'show_content_preview'
    Show content preview in headlines list.

'sort_headlines_by_feed_date'
    Sort headlines by feed date.

'ssl_cert_serial'
    Login with an SSL certificate.

'strip_images'
    Do not embed images in articles.

'strip_unsafe_tags'
    Strip unsafe tags from articles.

'user_css_theme'
    Select theme.

'user_stylesheet'
    Customize stylesheet.

'user_timezone'
    User timezone.

'vfeed_group_by_feed'
    Group headlines in virtual feeds."
  (ttrss-post-request address
		      :value
		      :op "getPref"
		      :sid sid
		      :pref_name preference))

(defun ttrss-get-unread (address sid)
  "Return number of unread articles at ADDRESS using SID."
  (ttrss-post-request address
		      :unread
		      :op "getUnread"
		      :sid sid))


;;; Feed listings

(defun ttrss-get-counters (address sid &optional output-mode)
  "Return list of property lists of counters at ADDRESS using SID.
OUTPUT-MODE is a string of what kind of counter to return:
feed (\"f\"), category (\"c\"), label (\"l\"), or tag (\"t\").
The default is \"flc\".  The property list members are:

'counter'
    Number of articles in the feed.

'id'
    Feed ID.

'has_img'
    Feed has a thumbnail image available (optional).

'updated'
    Timestamp of last update in \"HH:MM\" format (optional).

'kind'
    Type of feed (optional)."
  (ttrss-post-request address
		      nil
		      :op "getCounters"
		      :sid sid))

(defun ttrss-get-feeds (address sid &rest params)
  "Return a list of property lists of feeds at ADDRESS using SID.
PARAMS is any number of the following property-value pairs:

'cat_id'
    Return feeds under category CAT_ID (integer).

'unread_only'
    Only return feeds which have unread articles (boolean).

'limit'
    Limit amount of feeds returned to this value (integer).

'offset'
    Skip this amount of feeds first (integer).

'include_nested'
    Include child categories (boolean).

The property list's members are:

'last_updated'
    Timestamp of last update in unix epoch format.

'cat_id'
    Category ID.

'order_id'
    Sort index of the feed, if any.

'feed_url'
    URL of subscription.

'unread'
    Number of unread articles in feed.

'title'
    Title of feed.

'id'
    Feed ID.

'has_icon'
    Feed has a thumbnail image available.

The following feed IDs are special:

 0: Uncategorized
-1: Virtual (starred, published, archived, fresh)
-2: Labels
-3: All feeds excluding virtual feeds and labels
-4: All feeds."
  (apply 'ttrss-post-request
	 address
	 nil
	 :op "getFeeds"
	 :sid sid
	 params))

(defun ttrss-get-categories (address sid &rest params)
  "Return list of category plists at ADDRESS using SID.
PARAMS is any number of the following key-value pairs:

'unread_only'
    Only return categories which have unread articles (boolean).

'enable_nested'
    Nested mode: flatten everything into topmost categories (boolean).

'include_empty'
    Include empty categories (boolean)."
  (apply 'ttrss-post-request
	 address
	 nil
	 :op "getCategories"
	 :sid sid
	 params))

(defun ttrss-get-labels (address sid &optional article-id)
  "Return a list of label property lists at ADDRESS using SID.
Optionally, checks whether ARTICLE-ID has been set to any of the
labels.  The property list's members are:

'id'
    Label ID.  Note that this is an internal database ID of the label
    (feed ID = -11 - label ID).

'caption'
    Description text.

'fg_color'
    Foreground color of label in web interface.

'bg_color'
    Background color of label in web interface.

'checked'
    Set to t if ARTICLE-ID has this label."
  (ttrss-post-request address
		      nil
		      :op "getLabels"
		      :sid sid))


;;; Feed manipulation

(defun ttrss-update-feed (address sid feed-id)
  "Ask server at ADDRESS using SID to update FEED-ID.
This operation is not performed in the background by the server,
so it might take considerable time and, potentially, be aborted
by the HTTP server.  Therefore, this function performs the
request asynchronously.  A message is printed in the mini-buffer
area when the request completes."
  (ttrss-post-request-asynchronously address
				     :op "updateFeed"
				     :sid sid
				     :feed_id feed-id))

(defun ttrss-set-article-label (address sid article-ids label-id
					&optional assign)
  "Update, at ADDRESS using SID, ARTICLE-IDS with LABEL-ID.
Assign labels if ASSIGN is t, remove otherwise.  Returns number
of articles updated."
  (ttrss-post-request address
		      :updated
		      :op "setArticleLabel"
		      :sid sid
		      :article-ids (mapconcat (lambda (i) (format "%d" i))
					      article-ids
					      ",")
		      :label_id label-id
		      :assign assign))


;;; Article listings

(defun ttrss-get-headlines (address sid &rest params)
  "Return a list of headline plists at ADDRESS using SID.
PARAMS is any number of the following key-value pairs:

'feed_id'
    Only output articles for this feed (integer).
    The following feed IDs are special:

     0: Archived
    -1: Starred
    -2: Published
    -3: Fresh
    -4: All articles
    less than -10: Labels

'limit'
    Limits the amount of returned articles (integer).

'skip'
    Skip this amount of feeds first (integer).

'filter'
     Currently unused (string).

'is_cat'
    Requested feed_id is a category (boolean).

'show_excerpt'
    Include article excerpt in the output (boolean).

'show_content'
    Include full article text in the output (boolean).

'view_mode'
     All_articles, unread, adaptive, marked, updated (string).

'include_attachments'
    Include article attachments (boolean).

'since_id'
    Articles with id greater than since_id (integer).

'include_nested'
    Include articles from child categories (boolean).

'order_by'
    Override default sort order: 'date_reverse' or 'feed_dates' (string).

'search'
    Search query (string).

'search_mode'
    (string).

'match_on'
    (string)."
  (apply 'ttrss-post-request
	 address
	 nil
	 :op "getHeadlines"
	 :sid sid
	 params))

(defun ttrss-get-article (address sid &rest article-ids)
  "Return article property list from ADDRESS using SID with ARTICLE-IDS.
Returns nil or an empty list if those IDs can't be found.  The
property list's members are:

'attachments'
    List of RSS enclosures.  Each enclosure is a
    property list with the following members:

    'content_type'
        MIME type of the attachment.

    'id'
        Attachment ID.

    'duration'
        ???.

    'content_url'
        URL of the attachment.

    'post_id'
        Parent article ID.

    'title'
        Title of the attachment.

'title'
      Article title.

'labels'
      Article labels.

'published'
      Whether article is in 'Published' virtual feed.

'link'
      URL of the article.

'content'
      HTML content of the article.

'id'
      Article ID.

'marked'
      Whether article is in 'Starred' virtual feed.

'unread'
      Whether article is in 'Fresh' virtual feed.

'comments'
      ...

'Author'
      Article author..

'updated'
      Timestamp of last update in unix epoch format..

'feed_id'
      Feed ID."
  (when article-ids
    (ttrss-post-request address
			nil
			:op "getArticle"
			:sid sid
			:article_id (mapconcat (lambda (i) (format "%d" i))
					       article-ids
					       ","))))


;;; Article manipulation

(defun ttrss-update-article (address sid article-ids &rest params)
  "Update, at ADDRESS using SID, ARTICLE-IDS based on PARAMS.
Returns number of articles updated.  PARAMS is any number of the
following key-value pairs:

'mode'
    0: false, 1: true, 2: toggle.

'field'
    0: starred, 1: published, 2: unread, 3: article.

'data'
    Optional data parameter when setting note field (string)."
  (apply 'ttrss-post-request
	 address
	 nil
	 :op "updateArticle"
	 :sid sid
	 :article_ids article-ids
	 params))



(defun ttrss-catchup-feed (address sid feed-id)
  "Catchup, at ADDRESS using SID, FEED-ID.
Returns a status string (typically 'OK')."
  (ttrss-post-request address
		      :status
		      :op "catchupFeed"
		      :sid sid
		      :feed_id feed-id))

(defun ttrss-share-to-published (address sid title url content)
  "Publish, at ADDRESS using SID, an article with TITLE,  URL, and CONTENT.
Returns a status string (typically 'OK')."
  (ttrss-post-request address
		      :status
		      :op "shareToPublished"
		      :sid sid
		      :title title
		      :url url
		      :content content))

(defun ttrss-subscribe-to-feed (address sid feed-url &rest params)
  "Subscribe, at ADDRESS using SID, to FEED-URL with PARAMS.
Returns 0 if the feed already exists, 1 if successfully added,
and nil otherwise.  PARAMS is any number of the following
key-value pairs:

'category-id'
    Category ID to place feed into (defaults to 0: Uncategorized)

'login'
    Username to use for basic HTTP authentication at FEED-URL.

'password'
    Password to use for basic HTTP authentication at FEED-URL."
  (plist-get (apply 'ttrss-post-request
		    address
		    :status
		    :op "subscribeToFeed"
		    :sid sid
		    :feed_url feed-url
		    params)
	     :code))

(defun ttrss-unsubscribe-feed (address sid feed-id)
  "Unsubscribe, at ADDRESS using SID, from FEED-ID."
  (ttrss-post-request address
		      :status
		      :op "unsubscribeFeed"
		      :sid sid
		      :feed_id feed-id))

(defun ttrss-get-feed-tree (address sid &optional include-empty)
  "Return, at ADDRESS using SID, full tree of categories and feeds.
Optionally INCLUDE-EMPTY categories."
  (ttrss-post-request address
		      nil
		      :op "getFeedTree"
		      :sid sid
		      :include_empty include-empty))

(provide 'ttrss)
;;; ttrss.el ends here