Spaces:
Running
Running
hugobarauna
commited on
Commit
•
347b321
1
Parent(s):
0664a8c
Create elixir_forum_discourse_rss_opml_generator.livemd
Browse files
public-apps/elixir_forum_discourse_rss_opml_generator.livemd
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!-- livebook:{"app_settings":{"access_type":"public","output_type":"rich","show_source":true,"slug":"elixir-forum-discourse-opml-generator"}} -->
|
2 |
+
|
3 |
+
# ElixirForum Discourse RSS OPML Generator
|
4 |
+
|
5 |
+
```elixir
|
6 |
+
Mix.install([
|
7 |
+
{:kino, "~> 0.12.3"}
|
8 |
+
])
|
9 |
+
```
|
10 |
+
|
11 |
+
## Section
|
12 |
+
|
13 |
+
```elixir
|
14 |
+
defmodule Discourse.OPML do
|
15 |
+
defmodule Behaviour do
|
16 |
+
@callback outline(list(String.t())) :: String.t()
|
17 |
+
end
|
18 |
+
|
19 |
+
def generate(usernames, forum_specific_generator) do
|
20 |
+
outline = forum_specific_generator.outline(usernames)
|
21 |
+
|
22 |
+
"""
|
23 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
24 |
+
<opml version="1.0">
|
25 |
+
<head>
|
26 |
+
<title>RSS OPML Subscriptions</title>
|
27 |
+
</head>
|
28 |
+
<body>
|
29 |
+
#{outline}
|
30 |
+
</body>
|
31 |
+
</opml>
|
32 |
+
"""
|
33 |
+
end
|
34 |
+
|
35 |
+
defmodule ElixirForum do
|
36 |
+
@behaviour Discourse.OPML.Behaviour
|
37 |
+
|
38 |
+
@impl true
|
39 |
+
def outline(usernames) do
|
40 |
+
inner_outlines = Enum.map(usernames, &outlines_for_user/1)
|
41 |
+
|
42 |
+
"""
|
43 |
+
<outline title="ElixirForum" text="ElixirForum">
|
44 |
+
#{inner_outlines}
|
45 |
+
</outline>
|
46 |
+
"""
|
47 |
+
end
|
48 |
+
|
49 |
+
defp outlines_for_user(username) do
|
50 |
+
"""
|
51 |
+
<outline
|
52 |
+
text="Latest posts by @#{username}"
|
53 |
+
title="Latest posts by @#{username}"
|
54 |
+
type="rss"
|
55 |
+
xmlUrl="https://elixirforum.com/users/#{username}/activity.rss"
|
56 |
+
htmlUrl="https://elixirforum.com/u/#{username}/activity"
|
57 |
+
/>
|
58 |
+
<outline
|
59 |
+
text="Latest topics by @#{username}"
|
60 |
+
title="Latest topics by @#{username}"
|
61 |
+
type="rss"
|
62 |
+
xmlUrl="https://elixirforum.com/users/#{username}/activity/topics.rss"
|
63 |
+
htmlUrl="https://elixirforum.com/u/#{username}/activity/topics"
|
64 |
+
/>
|
65 |
+
"""
|
66 |
+
end
|
67 |
+
end
|
68 |
+
end
|
69 |
+
```
|
70 |
+
|
71 |
+
```elixir
|
72 |
+
default_usernames =
|
73 |
+
[
|
74 |
+
"josevalim",
|
75 |
+
"mhanberg",
|
76 |
+
"wojtekmach",
|
77 |
+
"nicd",
|
78 |
+
"dorgan",
|
79 |
+
"axelson",
|
80 |
+
"benwilson512",
|
81 |
+
"LostKobrakai",
|
82 |
+
"Fl4m3Ph03n1x",
|
83 |
+
"NobbZ",
|
84 |
+
"hauleth",
|
85 |
+
"astonj",
|
86 |
+
"michalmuskala",
|
87 |
+
"mbuhot",
|
88 |
+
"ConnorRigby",
|
89 |
+
"derpycoder"
|
90 |
+
]
|
91 |
+
|> Enum.join("\n")
|
92 |
+
|
93 |
+
form =
|
94 |
+
Kino.Control.form(
|
95 |
+
[
|
96 |
+
usernames:
|
97 |
+
Kino.Input.textarea("Elixir forum usernames (one per line)", default: default_usernames)
|
98 |
+
],
|
99 |
+
submit: "Generate OPML"
|
100 |
+
)
|
101 |
+
```
|
102 |
+
|
103 |
+
```elixir
|
104 |
+
output_frame = Kino.Frame.new()
|
105 |
+
|
106 |
+
Kino.listen(form, fn event ->
|
107 |
+
IO.inspect(event)
|
108 |
+
%{origin: origin, data: %{usernames: usernames}} = event
|
109 |
+
|
110 |
+
opml =
|
111 |
+
usernames
|
112 |
+
|> String.split()
|
113 |
+
|> Enum.map(&String.downcase/1)
|
114 |
+
|> Enum.sort()
|
115 |
+
|> Discourse.OPML.generate(Discourse.OPML.ElixirForum)
|
116 |
+
|> Kino.Text.new()
|
117 |
+
|
118 |
+
Kino.Frame.render(output_frame, opml, to: origin)
|
119 |
+
end)
|
120 |
+
|
121 |
+
output_frame
|
122 |
+
```
|