-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hackernews.ex
61 lines (48 loc) · 1.31 KB
/
hackernews.ex
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
defmodule Homepage.Hackernews do
def to_hn(url) do
url = Homepage.UriUtil.clean_fragment(url)
{base, query} =
case Req.get!("https://hn.algolia.com/api/v1/search", params: [tags: "story", query: url]) do
# Redirect to submit page
%{body: %{"nbHits" => 0}} ->
title = get_page_title(url)
query = [{"u", url}, {"t", title}]
base = %{
host: "news.ycombinator.com",
path: "/submitlink"
}
{base, query}
# Redirect directly to discussion
%{body: %{"nbHits" => 1, "hits" => [%{"story_id" => id}]}} ->
query = [{"id", id}]
base = %{
host: "news.ycombinator.com",
path: "/item"
}
{base, query}
# Redirect to search page
_ ->
query = [{"q", url}]
base = %{
host: "hn.algolia.com"
}
{base, query}
end
param = URI.encode_query(query, :rfc3986)
%URI{
scheme: "https",
port: 443,
query: param
}
|> struct(base)
|> URI.to_string()
end
defp get_page_title(url) do
%{status: 200, body: body} = Req.get!(url)
Floki.parse_document!(body)
|> Floki.find("head")
|> Floki.find("title")
|> Floki.text()
|> String.trim()
end
end