aboutsummaryrefslogtreecommitdiff
path: root/pl/episode_data.pl
diff options
context:
space:
mode:
Diffstat (limited to 'pl/episode_data.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,