Demo entry 6349812

memes

   

Submitted by anonymous on Feb 27, 2017 at 11:01
Language: Elixir. Code size: 2.8 kB.

defmodule Memes.Scrap do
  HTTPoison.start
  def hes(page \\ 1) do
    url = "http://www.humorenserie.com/ultimos/p/1"
    last_page = HTTPoison.get!(url).body |> last_page
    for n <- last_page..page do
      save_memes n
    end
  end
  defp save_memes(n) do
    url = "http://www.humorenserie.com/ultimos/"
    html = HTTPoison.get!(url <> "/p/#{n}").body
    |> Floki.find("h2")
    |> Floki.find(".story-title a")
    |> Floki.attribute("href")
    |> Enum.map(fn(url) -> Memes.Meme.changeset(%Memes.Meme{},
    %{url: url}) |> Memes.Repo.insert end)
  end
  defp last_page(html) do
    last_page = html
    |> Floki.find(".pager a")
    |> List.last
    |> Floki.attribute("href")
    |> List.to_string
    |> Path.basename
    |> String.to_integer
  end
  def meme(url) do
  html = HTTPoison.get!(url).body
  image = meme_image(html)
  title = meme_title(html)
  likes = meme_likes(html)
  comments = meme_comments(html)
  tags = meme_tags(html)
  video = meme_video(html)
  ref = meme_reference(html)
  author = meme_author(html)
  date = meme_date(html)
  %{
    title: title,
    image: image,
    likes: likes,
    comments: comments,
    tags: tags,
    video: video,
    author: author,
    ref: ref,
    date: date
  }
  end
  defp meme_title(html) do
    html
    |> Floki.find("h2")
    |> Floki.find(".story-title a")
    |> Floki.text
  end

  defp meme_image(html) do
    html
    |> Floki.find(".story-content")
    |> Floki.find("img")
    |> Floki.attribute("src")
    |> List.to_string
  end
  defp meme_video(html) do
    html
    |> Floki.find(".story-content")
    |> Floki.attribute("src")

  end

  defp meme_date(html) do
    html
    |> Floki.find(".story-info")
    |> Floki.text
    |> String.split("\t")
    |> List.last
    |> String.replace_prefix(" el ", "")
  end

  defp meme_author(html) do
    html
    |> Floki.find(".story-info")
    |> Floki.text
    |> String.split("\t")
    |> Enum.fetch!(4)
    |> String.replace_prefix("Por ", "")
  end
  defp meme_likes(html) do
    html
    |> Floki.find(".story-actions")
    |> Floki.find(".btn-vote")
    |> Floki.find("span")
    |> Floki.text
  end
  defp meme_comments(html) do
    html
    |> Floki.find(".story-actions")
    |> Floki.find(".btn-comments")
    |> Floki.find("span")
    |> Floki.text
  end
  defp meme_reference(html) do
    html
    |> Floki.find(".story-reference")
    |> Floki.attribute("href")
    |> Floki.text
  end
  defp meme_tags(html) do
    html
    |> Floki.find(".tags a")
    |> Floki.attribute("href")
    |> Enum.map(fn(x) -> String.replace_prefix(x, "http://www.humorenserie.com/busqueda/0/", "")end)
  end
end

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).