aboutsummaryrefslogtreecommitdiff
path: root/track_episodes.pl
blob: ec1785bb1d81a748f8363141f3183f5932d42080 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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).