Skip to content

Commit

Permalink
Fixed parsing fight's method
Browse files Browse the repository at this point in the history
  • Loading branch information
czogori committed Nov 23, 2019
1 parent b2712d8 commit c4560a4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ erl_crash.dump

# Ignore package tarball (built via "mix hex.build").
sherdog_parser-*.tar
.iex.exs
.vscode/
.iex.exs
7 changes: 6 additions & 1 deletion lib/sherdog_parser/event_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ defmodule SherdogParser.EventParser do
{"td", _, [_, _, time]}
] = Floki.find(html, "div.module.fight_card > div.content.event > div.footer td")

method =
method
|> String.trim()
|> Fight.method()

%Fight{
fighter_a_id: fighter_a_id,
fighter_a_name: fighter_a_name,
Expand All @@ -78,7 +83,7 @@ defmodule SherdogParser.EventParser do
result: fighter_a_result |> Fight.get_result(),
referee: referee |> String.trim(),
round: round |> String.trim() |> String.to_integer(),
method: Fight.method(method),
method: method,
time: Fight.parse_time(time)
}
end
Expand Down
19 changes: 17 additions & 2 deletions lib/sherdog_parser/fight.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule SherdogParser.Fight do
field(:event_date, Date.t())
end

def method("Draw"), do: ""
def method("Draw"), do: {"Draw", nil}

def method(m) do
[_, major, minor] =
Expand All @@ -33,10 +33,25 @@ defmodule SherdogParser.Fight do
def get_result("win"), do: :a
def get_result("loss"), do: :b
def get_result("draw"), do: :draw
def get_result("NC"), do: :no_contest
def get_result("nc"), do: :no_contest

def parse_time(time) do
[minute, second] = time |> String.trim() |> String.split(":")
{:ok, time} = Time.new(0, minute |> String.to_integer(), second |> String.to_integer())

{:ok, time} =
Time.new(
0,
time_part_to_integer(minute),
time_part_to_integer(second)
)

time
end

defp time_part_to_integer(""), do: 0

defp time_part_to_integer(time_part)
when is_binary(time_part),
do: String.to_integer(time_part)
end

0 comments on commit c4560a4

Please sign in to comment.