Your most awful code

Put it in this thread the code that you wrote and hate the most. The problem for which you did not find a solution in elixir that satisfied you. Aggrevate your supposed enemies, the ones that you have at least some hope left that they can code a bit, and create extra threads especially to annoy them. Try to really piss them off, make it a bullfight. Let them prove they are better programmers than you are. :slight_smile:

3 Likes

This might be it. It’s from a small program i wrote to scrape my school’s website.

def fetch_subject(subject) do
  try do
    new_contents = crawl_for_contents(subject.link, subject, subject.contents)
    {:ok, %{subject | contents: new_contents}}
  rescue
    error ->
      Logger.error("[fetch_subject]\n#{inspect error}")
      :error
  end
end

def crawl_for_contents(folder_url, subject, contents, category \\ nil) do
  {:ok, hrefs} = fetch_hrefs(folder_url)
  case hrefs do
    nil ->
      contents
    _ ->
      Enum.reduce(hrefs, contents, fn href, acc_contents ->
        clean_href = URI.decode(href)
        category = Category.categorize(clean_href, category)
        if String.ends_with?(clean_href, "/") do
          crawl_for_contents(folder_url <> href, subject, acc_contents, category)
        else
          if clean_href in acc_contents[category] do
            acc_contents
          else
            file_href = folder_url <> href
            download(file_href, clean_href, category, subject)
            update_in(acc_contents[category], &[clean_href | &1])
          end
        end
      end)
  end
end
2 Likes