aboutsummaryrefslogtreecommitdiff
path: root/pl
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-03-06 22:37:19 +0100
committerJohn Ankarström <john@ankarstrom.se>2022-03-06 22:37:19 +0100
commit06de1a12afaa2d3f4940290f87604eea9031c86d (patch)
tree685d64eb0036cdc5535dfcc374ed12b1a4b78d13 /pl
parent23df5644c2ce8209a8387257f8d6942736008663 (diff)
downloadEpisodeBrowser-06de1a12afaa2d3f4940290f87604eea9031c86d.tar.gz
Update data for all episodes at once.
Previously, a remote request would be made once per episode.
Diffstat (limited to 'pl')
-rw-r--r--pl/episode_data.pl53
1 files changed, 40 insertions, 13 deletions
diff --git a/pl/episode_data.pl b/pl/episode_data.pl
index a1b3a25..c6a5f3a 100644
--- a/pl/episode_data.pl
+++ b/pl/episode_data.pl
@@ -19,37 +19,64 @@ attach :-
detach :-
db_detach.
+% Interface.
+
+lookup_episode(Ep, Name, Data) :- lookup_episode_local(Ep, Name, Data), !.
+lookup_episode(Ep, Name, Data) :- lookup_episode_remote(Ep, Name, Data).
+
+lookup_episode_local(Ep, Name, Data) :-
+ episode_name_data(Ep, Name, Data).
+lookup_episode_remote(Ep, Name, Data) :-
+ update, !,
+ episode_name_data(Ep, Name, Data).
+
+
retract_episode(Ep) :-
( episode_name_data(Ep, _, _)
-> retractall_episode_name_data(Ep, _, _)
; true
).
+% Parsing.
+
padding(Ep) --> { Ep < 10 }, "00".
padding(Ep) --> { Ep >= 10, Ep < 100 }, "0".
padding(Ep) --> { Ep >= 100 }.
episode_number(Ep) --> padding(Ep), integer(Ep).
-lookup_episode(Ep, Name, Data) :- lookup_episode_local(Ep, Name, Data), !.
-lookup_episode(Ep, Name, Data) :- lookup_episode_remote(Ep, Name, Data).
+% Database updating.
-lookup_episode_local(Ep, Name, Data) :-
- episode_name_data(Ep, Name, Data).
+update :-
+ remote(R0), !,
+ findall(Ep-Name-Data, (xpath(R0, //tr, R),
+ remote_episode(R, Ep),
+ remote_episode_name_data(R, Ep, Name, Data)),
+ ENDs), !,
+ maplist(update, ENDs).
-lookup_episode_remote(Ep, Name, Data) :-
+update(Ep-Name-Data) :- episode_name_data(Ep, Name, Data), !.
+update(Ep-Name-Data) :- assert_episode_name_data(Ep, Name, Data).
+
+% Remote retrieval.
+
+remote(R) :-
catch(http_load_html(
'https://www.detectiveconanworld.com/wiki/Next_Conan%27s_Hint',
- D),
+ R),
_,
- fail),
- xpath(D, //tr, D1),
- xpath(D1, td(index(1),text), T),
+ fail).
+
+remote_episode(R, Ep) :-
+ xpath(R, td(index(1),text), T),
+ atom_number(T, Ep).
+
+remote_episode_name_data(R, Ep, Name, Data) :-
+ xpath(R, td(index(1),text), T),
atom_phrase(episode_number(Ep), T),
- xpath(D1, td(index(2),text), Name),
- xpath(D1, td(index(3),text), Hint),
- Data = ['Hint'(Hint)],
- assert_episode_name_data(Ep, Name, Data).
+ xpath(R, td(index(2),text), Name),
+ xpath(R, td(index(3),text), Hint),
+ Data = ['Hint'(Hint)].
http_load_html(URL, DOM) :-
setup_call_cleanup(http_open(URL, In,