copy-pasted lab source, compile successfully, checked atomic functions (is_rss, get_items)

This commit is contained in:
Ivan I. Ovchinnikov 2023-03-27 21:06:28 +03:00
parent d9cf270389
commit e90138636e
1 changed files with 36 additions and 23 deletions

View File

@ -1,8 +1,9 @@
-module(rss_parse).
-export([is_rss2_feed/1, get_feed_items/1]).
-export([is_rss2_feed/1, get_feed_items/1, get_item_time/1, compare_feed_items/2]).
-include_lib("/usr/lib/erlang/lib/xmerl-1.3.26/include/xmerl.hrl").
% В этой функции вызываем функцию `xmerl_scan:file/1`, которая возвращает парсер XML-документа. Затем используем `xmerl_xpath:string/2` для поиска элемента `<rss>` с атрибутом `version` равным "2.0". Если такой элемент найден, то функция возвращает true, иначе false.
is_rss2_feed(Name)->
{Doc,_} = xmerl_scan:file(Name),
case xmerl_xpath:string("/rss[@version='2.0']", Doc) of
@ -10,30 +11,42 @@ is_rss2_feed(Name)->
_ -> true
end.
%% is_rss2_feed(N) ->
%% {M,_} = N, xmerl_xpath:string("/rss[@version='2.0']", M) /= [].
% Возвращает список элементов
get_feed_items(N) ->
{Doc,_} = xmerl_scan:file(N),
{M,_} = Doc, xmerl_xpath:string("//channel/item", M).
xmerl_xpath:string("//channel/item", Doc).
% пока не проверена (не на чем, а функцию поэлементоно разбирающую список есть ощущение, что я буду писать в одном из следующих пунктов)
get_item_time(Item) ->
[M] = Item, [#xmlText{value=Text}] = xmerl_xpath:string("pubDate/text()", M),
{{Year,Month,Date},{Hour,Min,Sec}} = httpd_util:convert_request_date(Text),
calendar:datetime_to_gregorian_seconds({{Year,Month,Date},{Hour,Min,Sec}}).
% @private
% @doc Эта вспомогательная функция просматривает заданный XML элемент
% и удаляет из него сведения о других XML элементах, например содержащиеся в полях
% "parents" или "pos".
%
% @spec extract_xml(Node::xmlAny()) -> xmlAny()
%
extract_xml(Elem = #xmlElement{}) ->
Elem#xmlElement{parents=[], pos=0,
content=lists:map(fun extract_xml/1, Elem#xmlElement.content),
attributes=lists:map(fun extract_xml/1, Elem#xmlElement.attributes)};
extract_xml(Attr = #xmlAttribute{}) ->
Attr#xmlAttribute{parents=[], pos=0};
extract_xml(Text = #xmlText{}) ->
Text#xmlText{parents=[], pos=0};
extract_xml(Comment = #xmlComment{}) ->
Comment#xmlComment{parents=[], pos=0};
extract_xml(Other) ->
Other.
%% -module(rss_parse).
%% -export([is_rss2_feed/1, get_feed_items/1, get_item_time/1, compare_feed_items/2]).
%% -include_lib("c:/PROGRA~1/ERL510~1.3/lib/xmerl-1.3.4/include/xmerl.hrl").
%% get_item_time(Item) ->
%% [M] = Item, [#xmlText{value=Text}] = xmerl_xpath:string("pubDate/text()", M),
%% {{Year,Month,Date},{Hour,Min,Sec}} = httpd_util:convert_request_date(Text),
%% calendar:datetime_to_gregorian_seconds({{Year,Month,Date},{Hour,Min,Sec}}).
%% compare_feed_items(OldItem, NewItem) ->
%% [A] = OldItem, [B] = NewItem,
%% G1 = xmerl_xs:select("guid", A), G2 = xmerl_xs:select("guid", B),
%% T1 = xmerl_xs:select("title", A), T2 = xmerl_xs:select("title", B),
%% L1 = xmerl_xs:select("link", A), L2 = xmerl_xs:select("link", B),
%% ((G1 /= []) andalso (G2 /= []) andalso (G1 == G2)) orelse
%% ((T1 /= []) andalso (T2 /= []) andalso (T1 == T2)) orelse
%% ((L1 /= []) andalso (L2 /= []) andalso (L1 == L2)).
compare_feed_items(OldItem, NewItem) ->
[A] = OldItem, [B] = NewItem,
G1 = xmerl_xs:select("guid", A), G2 = xmerl_xs:select("guid", B),
T1 = xmerl_xs:select("title", A), T2 = xmerl_xs:select("title", B),
L1 = xmerl_xs:select("link", A), L2 = xmerl_xs:select("link", B),
((G1 /= []) andalso (G2 /= []) andalso (G1 == G2)) orelse
((T1 /= []) andalso (T2 /= []) andalso (T1 == T2)) orelse
((L1 /= []) andalso (L2 /= []) andalso (L1 == L2)).