aboutsummaryrefslogtreecommitdiff
path: root/pl/track_episodes.pl
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-02-15 15:38:57 +0100
committerJohn Ankarström <john@ankarstrom.se>2022-02-15 15:38:57 +0100
commit09ec144a99f1234a37d04854bdfb81485540be97 (patch)
treece3050a062a76dd12980b08ef36eb7ac35b7e6f8 /pl/track_episodes.pl
parentfed19b9942575e7c0360a0d77a3c544afdbaeb6c (diff)
downloadEpisodeBrowser-09ec144a99f1234a37d04854bdfb81485540be97.tar.gz
Put C code and Prolog code in separate directories.
Diffstat (limited to 'pl/track_episodes.pl')
-rw-r--r--pl/track_episodes.pl96
1 files changed, 96 insertions, 0 deletions
diff --git a/pl/track_episodes.pl b/pl/track_episodes.pl
new file mode 100644
index 0000000..ec1785b
--- /dev/null
+++ b/pl/track_episodes.pl
@@ -0,0 +1,96 @@
+:- module(track_episodes, [update_tracked_episodes/0,
+ toggle_episode/1,
+ forget_episode/1,
+ most_recently_watched/1,
+ watched/1]).
+
+:- use_module(library(dcg/basics)).
+:- use_module(library(registry)).
+:- use_module(library(persistency)).
+:- use_module(local_episodes).
+
+attach :-
+ absolute_file_name('track_episodes.db', F, [access(write)]),
+ db_attach(F, []).
+
+detach :-
+ db_detach.
+
+:- persistent episode_watched(episode:integer, watched:atom).
+
+% Helpers.
+
+swapbc(P, A, B, C, D) :- call(P, A, C, B, D).
+compound(F, A, B, T) :- T =.. [F, A, B].
+
+% Get data from registry.
+
+file(N) --> "File Name ", integer(N).
+
+pos(N) --> "File Position ", integer(N).
+
+mpc_reg(Phrase, Value) :-
+ phrase(Phrase, Name),
+ atom_codes(Name1, Name),
+ registry_get_key(current_user/software/'mpc-hc'/'mpc-hc'/settings,
+ Name1, Value).
+
+% Create episode-position list.
+
+epipos(L) :- epipos_x(0, L).
+
+epipos_x(N, L) :-
+ ( catch((mpc_reg(file(N), File),
+ mpc_reg(pos(N), Pos)),
+ _,
+ fail)
+ -> M is N + 1,
+ ( atom_codes(File, File1),
+ findall(Ep, phrase(local_episode(Ep), File1), Eps)
+ -> atom_codes(Pos, Pos1),
+ phrase(integer(Pos2), Pos1),
+ maplist(swapbc(compound, '-', Pos2), Eps, Eps1),
+ append(Eps1, T, L),
+ epipos_x(M, T)
+ ; epipos_x(M, L))
+ ; L = []
+ ).
+
+considered_watched(_-Pos) :-
+ Pos > 11000000000.
+
+% Update database.
+
+update_tracked_episodes :-
+ epipos(All),
+ include(considered_watched, All, Watched),
+ maplist(add, Watched).
+
+add(Ep-_) :- episode_watched(Ep, _), !.
+add(Ep-_) :- assert_episode_watched(Ep, true).
+
+forget_episode(Ep) :-
+ retractall_episode_watched(Ep, true),
+ retractall_episode_watched(Ep, false).
+
+toggle_episode(Ep) :-
+ episode_watched(Ep, true), !,
+ retractall_episode_watched(Ep, true),
+ assert_episode_watched(Ep, false).
+
+toggle_episode(Ep) :-
+ episode_watched(Ep, false), !,
+ retractall_episode_watched(Ep, false),
+ assert_episode_watched(Ep, true).
+
+toggle_episode(Ep) :-
+ assert_episode_watched(Ep, true).
+
+% Utility.
+
+most_recently_watched(Ep) :-
+ findall(N, episode_watched(N, true), All),
+ sort(0, @>, All, [Ep|_]).
+
+watched(Ep) :-
+ episode_watched(Ep, true).