logo

news_parse_ex

commit: 18972a2a8d9a2c95a56ab13e5ee1d3c0dcafb3db
parent 1f3340d3604fa37fa5f2812c7bfa87b5ae856ba7
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sun,  5 Feb 2023 11:47:42 +0100

Switch from :calendar to :timex to preserve TZ info

Diffstat:

Mlib/rss2_0.ex16+++++++++++++++-
Mmix.exs2+-
Mtest/news_parse_ex_test.exs21++++++++++++++++++++-
3 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/lib/rss2_0.ex b/lib/rss2_0.ex @@ -29,13 +29,27 @@ defmodule NewsParseEx.RSS2_0 do defp get_entry_link(frag), do: XML.string_from_xpath(~s{/item/link}, frag) defp get_entry_id(frag), do: XML.string_from_xpath(~s{/item/guid}, frag) + defp parse_datetime(_str, []) do + :error + end + + defp parse_datetime(str, [format | rest]) when is_bitstring(str) do + with {:ok, _} = dt <- Timex.parse(str, format) do + dt + else + _e -> parse_datetime(str, rest) + end + end + defp get_entry_published(frag) do with {:ok, pubDate} <- XML.string_from_xpath(~s{/item/pubDate}, frag) do - Calendar.DateTime.Parse.rfc822_utc(pubDate) + # RFC1123 is because of SourceHut + parse_datetime(pubDate, ["{RFC822}", "{RFC1123}"]) end end defp get_feed_entry(frag) do + # FIXME: Don't just shove errors away %{} |> Maps.put_if_ok(:title, get_entry_title(frag)) |> Maps.put_if_ok(:id, get_entry_id(frag)) diff --git a/mix.exs b/mix.exs @@ -19,6 +19,6 @@ defmodule NewsParseEx.MixProject do end defp deps do - [{:calendar, "~> 1.0.0"}] + [{:timex, "~> 3.0"}] end end diff --git a/test/news_parse_ex_test.exs b/test/news_parse_ex_test.exs @@ -1,3 +1,7 @@ +# NewsParseEx: RSS/Atom parser +# Copyright © 2022-2023 Haelwenn (lanodan) Monnier <contact+news_parse_ex@hacktivis.me> +# SPDX-License-Identifier: AGPL-3.0-only + defmodule NewsParseExTest do use ExUnit.Case, async: true @@ -131,13 +135,28 @@ defmodule NewsParseExTest do assert(length(parsed.entries) == 20) + # Sat, 30 May 2020 19:17:12 -0600 + # #DateTime<2020-05-30 19:17:12-06:00 -06 Etc/UTC-6> + published = %DateTime{ + year: 2020, + month: 05, + day: 30, + hour: 19, + minute: 17, + second: 12, + std_offset: 0, + utc_offset: -6 * 60 * 60, + time_zone: "Etc/UTC-6", + zone_abbr: "-06" + } + assert( Enum.at(parsed.entries, 0) == %{ id: "https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.7.3", title: "pkgconf-1.7.3", description: "pkgconf 1.7.3.", link: "https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.7.3", - published: ~U[2020-05-31 01:17:12Z] + published: published } ) end