Skip to content

Commit 637fdab

Browse files
committed
Add link response
1 parent 169f60a commit 637fdab

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

lib/post.ml

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ type t = {
2222
feed : Feed.t;
2323
author : string;
2424
email : string;
25-
description : Nethtml.document list;
25+
content : Nethtml.document list;
26+
mutable link_response : (string, string) result option;
2627
}
2728

2829
let rec len_prefix_of_html html len =
@@ -167,7 +168,7 @@ let post_of_atom ~(feed : Feed.t) (e : Syndic.Atom.entry) =
167168
let date =
168169
match e.published with Some _ -> e.published | None -> Some e.updated
169170
in
170-
let description =
171+
let content =
171172
match e.content with
172173
| Some (Text s) -> html_of_text s
173174
| Some (Html (xmlbase, s)) -> html_of_text ?xmlbase s
@@ -187,18 +188,21 @@ let post_of_atom ~(feed : Feed.t) (e : Syndic.Atom.entry) =
187188
feed;
188189
author = author.name;
189190
email = "";
190-
description;
191+
content;
192+
link_response = None;
191193
}
192194

193195
let post_of_rss2 ~(feed : Feed.t) it =
194-
let title, description =
196+
let title, content =
195197
match it.Syndic.Rss2.story with
196198
| All (t, xmlbase, d) -> (
197199
( t,
198200
match it.content with
199201
| _, "" -> html_of_text ?xmlbase d
200202
| xmlbase, c -> html_of_text ?xmlbase c ))
201-
| Title t -> (t, [])
203+
| Title t ->
204+
let xmlbase, c = it.content in
205+
(t, html_of_text ?xmlbase c)
202206
| Description (xmlbase, d) -> (
203207
( "",
204208
match it.content with
@@ -221,8 +225,9 @@ let post_of_rss2 ~(feed : Feed.t) it =
221225
feed;
222226
author = feed.name;
223227
email = string_of_option it.author;
224-
description;
228+
content;
225229
date = it.pubDate;
230+
link_response = None;
226231
}
227232

228233
let posts_of_feed c =
@@ -237,7 +242,7 @@ let string_of_html html =
237242
Buffer.contents buffer
238243

239244
let mk_entry post =
240-
let content = Syndic.Atom.Html (None, string_of_html post.description) in
245+
let content = Syndic.Atom.Html (None, string_of_html post.content) in
241246
let contributors =
242247
[ Syndic.Atom.author ~uri:(Uri.of_string post.feed.url) post.feed.name ]
243248
in
@@ -271,3 +276,18 @@ let get_posts ?n ?(ofs = 0) planet_feeds =
271276
let posts = List.sort post_compare posts in
272277
let posts = remove ofs posts in
273278
match n with None -> posts | Some n -> take n posts
279+
280+
(* Fetch the link response and cache it. *)
281+
let fetch_link t =
282+
match (t.link, t.link_response) with
283+
| None, _ -> None
284+
| Some _, Some (Ok x) -> Some x
285+
| Some _, Some (Error _) -> None
286+
| Some link, None -> (
287+
try
288+
let response = Http.get (Uri.to_string link) in
289+
t.link_response <- Some (Ok response);
290+
Some response
291+
with _exn ->
292+
t.link_response <- Some (Error "");
293+
None)

lib/river.ml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,16 @@ let date post = post.Post.date
2929
let feed post = post.Post.feed
3030
let author post = post.Post.author
3131
let email post = post.Post.email
32-
let content post = Post.string_of_html post.Post.description
33-
let meta_description post = Meta.description (content post)
34-
let seo_image post = Meta.preview_image (content post)
35-
let create_atom_entries = Post.mk_entries
32+
let content post = Post.string_of_html post.Post.content
33+
34+
let meta_description post =
35+
match Post.fetch_link post with
36+
| None -> None
37+
| Some response -> Meta.description response
38+
39+
let seo_image post =
40+
match Post.fetch_link post with
41+
| None -> None
42+
| Some response -> Meta.preview_image response
43+
44+
let create_atom_entries = Post.mk_entries

0 commit comments

Comments
 (0)