From 388e5bf6f5c189c1f5657fded5246bb3b77a9dd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Sun, 3 Apr 2022 01:09:09 +0200 Subject: Correctly fetch remote data for WPS episodes. --- pl/episode_data.pl | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/pl/episode_data.pl b/pl/episode_data.pl index 1bdde79..f71e2cb 100644 --- a/pl/episode_data.pl +++ b/pl/episode_data.pl @@ -4,6 +4,7 @@ lookup_episode_local/3, lookup_episode_remote/3]). +:- use_module(library(clpfd)). :- use_module(library(dcg/basics)). :- use_module(library(http/http_open)). :- use_module(library(sgml)). @@ -44,11 +45,12 @@ retract_episode(Ep) :- % Parsing. -padding(Ep) --> { Ep < 10 }, "00". -padding(Ep) --> { Ep >= 10, Ep < 100 }, "0". -padding(Ep) --> { Ep >= 100 }. +padding(Ep) --> { Ep #< 10 }, "00". +padding(Ep) --> { Ep #>= 10, Ep #< 100 }, "0". +padding(Ep) --> { Ep #>= 100 }. episode_number(Ep) --> padding(Ep), integer(Ep). +episode_number(Ep) --> padding(Ep), integer(Ep), "WPS", integer(_). % Database updating. @@ -56,10 +58,9 @@ ensure :- episode_name_data(_, _, _), !. ensure :- update. update :- - remote(R0), !, - findall(Ep-Name-Data, (xpath(R0, //tr, R), - remote_episode(R, Ep), - remote_episode_name_data(R, Ep, Name, Data)), + findall(Ep-Name-Data, (remote_row(R), + row_episode(R, Ep), + row_episode_name_data(R, Ep, Name, Data)), ENDs), !, maplist(update, ENDs). @@ -68,18 +69,19 @@ update(Ep-Name-Data) :- assert_episode_name_data(Ep, Name, Data). % Remote retrieval. -remote(R) :- +remote_row(R) :- catch(http_load_html( 'https://www.detectiveconanworld.com/wiki/Next_Conan%27s_Hint', - R), + R0), _, - fail). + fail), !, + xpath(R0, //tr, R). -remote_episode(R, Ep) :- +row_episode(R, Ep) :- xpath(R, td(index(1),text), T), - atom_number(T, Ep). + atom_phrase(episode_number(Ep), T). -remote_episode_name_data(R, Ep, Name, Data) :- +row_episode_name_data(R, Ep, Name, Data) :- xpath(R, td(index(1),text), T), atom_phrase(episode_number(Ep), T), xpath(R, td(index(2),text), Name), -- cgit v1.2.3