Skip to content

Latest commit

 

History

History
113 lines (88 loc) · 2.25 KB

book_reviews.livemd

File metadata and controls

113 lines (88 loc) · 2.25 KB

Goodreads

Mix.install(
  [
    {:poison, "~> 4.0"},
    {:httpoison, "~> 1.8"},
    {:floki, "~> 0.34.0"},
    {:oauther, "~> 1.1"},
    {:extwitter, "~> 0.12"},
    {:wallaby, "~> 0.30.6"}
  ],
  config: [
    extwitter: [json_library: Poison]
  ]
)

Section

defmodule Goodreads.UserFinder do
  def find_user_url(username) do
    sanitized_username = username |> URI.encode()

    url =
      "https://www.goodreads.com/search?q=#{sanitized_username}&search%5Bsource%5D=goodreads&search_type=people&tab=people"

    %{body: html} = HTTPoison.get!(url)

    Floki.parse_document!(html)
    |> extract_user_url_suffix()
    |> make_goodreads_user_url()
  end

  def get_user_reviews_url(username: username) do
    username |> find_user_url() |> make_user_url_to_reviews_url()
  end

  def get_user_reviews_url(url: url) do
    url |> make_user_url_to_reviews_url()
  end

  def get_reviews(args) do
    url = get_user_reviews_url(args)
    %{body: html} = url |> HTTPoison.get!()

    html
    |> Floki.parse_document!()
  end

  defp extract_user_url_suffix(document) do
    [{"a", [{"href", user_url_suffix}], _} | _] =
      document |> Floki.find("table") |> Floki.find("a")

    user_url_suffix
  end

  defp make_goodreads_user_url(user_url_suffix) do
    "https://www.goodreads.com/" |> Path.join(user_url_suffix)
  end

  defp make_user_url_to_reviews_url(url) do
    url
    |> String.replace(
      "https://www.goodreads.com/user/show/",
      "https://www.goodreads.com/review/list/"
    )
  end
end

Goodreads.UserFinder.find_user_url("bartczukkuba")
user_reviews_doc = Goodreads.UserFinder.get_reviews(username: "bartczukkuba")
ExUnit.start()

defmodule Check do
  use ExUnit.Case, async: true
  use Wallaby.Feature

  import Wallaby.Query, only: [css: 2, text_field: 1, button: 1]

  feature "users can create todos", %{session: session} do
    session
    |> visit("https://www.goodreads.com/review/list/14118501")
    |> find(css(".field review", count: 1))
    |> List.first()
    |> inspect()
    |> IO.puts()
  end
end
Goodreads.UserFinder.get_user_reviews_url(username: "bartczukkuba")
HTTPoison.get!("https://twitter.com/lambdaofgod/following")
user_url_suffix