meeseeks bookmarks; also bookmarks
This commit is contained in:
@ -19,7 +19,8 @@ defmodule SomethingErlang.AwfulApi do
|
|||||||
|> Thread.compile()
|
|> Thread.compile()
|
||||||
end
|
end
|
||||||
|
|
||||||
def bookmarks(user) do
|
def bookmarks(page, user) do
|
||||||
Bookmarks.compile(1, user)
|
Client.bookmarks_doc(page, user)
|
||||||
|
|> Bookmarks.compile()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,59 +1,80 @@
|
|||||||
defmodule SomethingErlang.AwfulApi.Bookmarks do
|
defmodule SomethingErlang.AwfulApi.Bookmarks do
|
||||||
|
import Meeseeks.CSS
|
||||||
|
|
||||||
require Logger
|
require Logger
|
||||||
|
|
||||||
alias SomethingErlang.AwfulApi.Client
|
def compile(html) do
|
||||||
|
for thread <- Meeseeks.all(html, css("tr.thread")) do
|
||||||
|
thread_id =
|
||||||
|
Meeseeks.attr(thread, "id")
|
||||||
|
|> String.split("thread")
|
||||||
|
|> List.last()
|
||||||
|
|> String.to_integer()
|
||||||
|
|
||||||
def compile(page, user) do
|
|
||||||
doc = Client.bookmarks_doc(page, user)
|
|
||||||
html = Floki.parse_document!(doc)
|
|
||||||
|
|
||||||
for thread <- Floki.find(html, "tr.thread") do
|
|
||||||
parse(thread)
|
parse(thread)
|
||||||
|
|> Map.put(:id, thread_id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse(thread) do
|
def parse(thread) do
|
||||||
%{
|
for col <- Meeseeks.all(thread, css("td:not(.star)")),
|
||||||
title: Floki.find(thread, "td.title") |> inner_html() |> Floki.raw_html(),
|
class = Meeseeks.attr(col, "class") |> String.split() |> List.first(),
|
||||||
icon: Floki.find(thread, "td.icon") |> inner_html() |> Floki.raw_html(),
|
|
||||||
author: Floki.find(thread, "td.author") |> inner_html() |> Floki.text(),
|
|
||||||
replies: Floki.find(thread, "td.replies") |> inner_html() |> Floki.text(),
|
|
||||||
views: Floki.find(thread, "td.views") |> inner_html() |> Floki.text(),
|
|
||||||
rating: Floki.find(thread, "td.rating") |> inner_html() |> Floki.raw_html(),
|
|
||||||
lastpost: Floki.find(thread, "td.lastpost") |> inner_html() |> Floki.raw_html()
|
|
||||||
}
|
|
||||||
|
|
||||||
for {"td", [{"class", class} | _attrs], children} <- Floki.find(thread, "td"),
|
|
||||||
String.starts_with?(class, "star") == false,
|
|
||||||
into: %{} do
|
into: %{} do
|
||||||
case class do
|
{String.to_atom(class), thread_data(class, col)}
|
||||||
<<"title", _rest::binary>> ->
|
|
||||||
{:title, children |> Floki.raw_html()}
|
|
||||||
|
|
||||||
<<"icon", _rest::binary>> ->
|
|
||||||
{:icon, children |> Floki.raw_html()}
|
|
||||||
|
|
||||||
<<"author", _rest::binary>> ->
|
|
||||||
{:author, children |> Floki.text()}
|
|
||||||
|
|
||||||
<<"replies", _rest::binary>> ->
|
|
||||||
{:replies, children |> Floki.text() |> String.to_integer()}
|
|
||||||
|
|
||||||
<<"views", _rest::binary>> ->
|
|
||||||
{:views, children |> Floki.text() |> String.to_integer()}
|
|
||||||
|
|
||||||
<<"rating", _rest::binary>> ->
|
|
||||||
{:rating, children |> Floki.raw_html()}
|
|
||||||
|
|
||||||
<<"lastpost", _rest::binary>> ->
|
|
||||||
{:lastpost, children |> Floki.raw_html()}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp inner_html(node) do
|
defp thread_data("icon", td) do
|
||||||
node
|
img = Meeseeks.one(td, css("img"))
|
||||||
|> List.first()
|
|
||||||
|> Floki.children()
|
%{
|
||||||
|
icon: Meeseeks.attr(img, "src"),
|
||||||
|
title: Meeseeks.attr(img, "alt")
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp thread_data("title", td) do
|
||||||
|
last_seen = Meeseeks.one(td, css(".lastseen .count"))
|
||||||
|
info = Meeseeks.one(td, css(".info"))
|
||||||
|
|
||||||
|
%{
|
||||||
|
new_posts: Meeseeks.text(last_seen) |> String.to_integer(),
|
||||||
|
thread_title: Meeseeks.text(Meeseeks.one(info, css(".thread_title")))
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp thread_data("author", td) do
|
||||||
|
Meeseeks.text(td)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp thread_data("replies", td) do
|
||||||
|
Meeseeks.text(td)
|
||||||
|
|> String.to_integer()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp thread_data("views", td) do
|
||||||
|
Meeseeks.text(td)
|
||||||
|
|> String.to_integer()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp thread_data("rating", td) do
|
||||||
|
img = Meeseeks.one(td, css("img"))
|
||||||
|
|
||||||
|
%{
|
||||||
|
icon: Meeseeks.attr(img, "src"),
|
||||||
|
title: Meeseeks.attr(img, "title")
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp thread_data("lastpost", td) do
|
||||||
|
date = Meeseeks.one(td, css(".date"))
|
||||||
|
author = Meeseeks.one(td, css(".author"))
|
||||||
|
|
||||||
|
%{
|
||||||
|
date: Meeseeks.text(date),
|
||||||
|
author: Meeseeks.text(author)
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp thread_data(class, _td), do: Logger.error("#{inspect(class)} not found")
|
||||||
end
|
end
|
||||||
|
@ -12,7 +12,6 @@ defmodule SomethingErlang.AwfulApi.Thread do
|
|||||||
thread =
|
thread =
|
||||||
Meeseeks.one(html, css("#thread"))
|
Meeseeks.one(html, css("#thread"))
|
||||||
|
|
||||||
|
|
||||||
thread_id =
|
thread_id =
|
||||||
Meeseeks.attr(thread, "class")
|
Meeseeks.attr(thread, "class")
|
||||||
|> String.split(":")
|
|> String.split(":")
|
||||||
@ -74,7 +73,6 @@ defmodule SomethingErlang.AwfulApi.Thread do
|
|||||||
date
|
date
|
||||||
|> String.split(~r{[\s,:]}, trim: true)
|
|> String.split(~r{[\s,:]}, trim: true)
|
||||||
|> Enum.drop(2)
|
|> Enum.drop(2)
|
||||||
|> dbg()
|
|
||||||
|
|
||||||
month =
|
month =
|
||||||
1 +
|
1 +
|
||||||
|
@ -55,8 +55,8 @@ defmodule SomethingErlang.Grover do
|
|||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_call({:show_bookmarks, _page_number}, _from, state) do
|
def handle_call({:show_bookmarks, page_number}, _from, state) do
|
||||||
bookmarks = AwfulApi.bookmarks(state.user)
|
bookmarks = AwfulApi.bookmarks(page_number, state.user)
|
||||||
{:reply, bookmarks, state}
|
{:reply, bookmarks, state}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
42
lib/something_erlang_web/live/bookmarks_live.ex
Normal file
42
lib/something_erlang_web/live/bookmarks_live.ex
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
defmodule SomethingErlangWeb.BookmarksLive do
|
||||||
|
use SomethingErlangWeb, :live_view
|
||||||
|
|
||||||
|
alias SomethingErlang.Grover
|
||||||
|
|
||||||
|
def render(assigns) do
|
||||||
|
~H"""
|
||||||
|
<div :if={@bookmarks}>
|
||||||
|
<.bookmark
|
||||||
|
:for={thread <- @bookmarks}
|
||||||
|
id={thread.id}
|
||||||
|
title={thread.title.thread_title}
|
||||||
|
post_count={thread.replies}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
|
||||||
|
defp bookmark(assigns) do
|
||||||
|
assigns = assign(assigns, :pages, trunc(assigns.post_count / 40))
|
||||||
|
|
||||||
|
~H"""
|
||||||
|
<div>
|
||||||
|
<.link href={~p"/thread/#{@id}"}><%= @title %></.link>
|
||||||
|
<.link href={~p"/thread/#{@id}?page=#{@pages}"}><%= @pages %></.link>
|
||||||
|
</div>
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
|
||||||
|
def mount(_params, session, socket) do
|
||||||
|
user =
|
||||||
|
socket.assigns.current_user
|
||||||
|
|> Map.put(:bbpassword, session["bbpassword"])
|
||||||
|
|
||||||
|
Grover.mount(user)
|
||||||
|
|
||||||
|
{:ok,
|
||||||
|
socket
|
||||||
|
|> assign(:page_title, "Bookmarks")
|
||||||
|
|> assign(:bookmarks, Grover.get_bookmarks!(1) |> dbg())}
|
||||||
|
end
|
||||||
|
end
|
@ -28,6 +28,8 @@ defmodule SomethingErlangWeb.Router do
|
|||||||
on_mount: [{SomethingErlangWeb.UserAuth, :ensure_authenticated}] do
|
on_mount: [{SomethingErlangWeb.UserAuth, :ensure_authenticated}] do
|
||||||
live("/thread", ThreadLive)
|
live("/thread", ThreadLive)
|
||||||
live("/thread/:id", ThreadLive)
|
live("/thread/:id", ThreadLive)
|
||||||
|
live("/bookmarks", BookmarksLive)
|
||||||
|
live("/bookmarks/:id", BookmarksLive)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user