]> kaliko git repositories - mpd-sima.git/blob - sima/plugins/core/history.py
3380870754b84a12c775dd0075b40173e1f0c0c6
[mpd-sima.git] / sima / plugins / core / history.py
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2013, 2014, 2020 kaliko <kaliko@azylum.org>
3 #
4 #  This file is part of sima
5 #
6 #  sima is free software: you can redistribute it and/or modify
7 #  it under the terms of the GNU General Public License as published by
8 #  the Free Software Foundation, either version 3 of the License, or
9 #  (at your option) any later version.
10 #
11 #  sima is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #  GNU General Public License for more details.
15 #
16 #  You should have received a copy of the GNU General Public License
17 #  along with sima.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 #
20 """Add playing tracks to history
21 """
22
23 # standard library import
24 from time import time
25
26 # third parties components
27
28 # local import
29 from ...lib.plugin import Plugin
30
31 class History(Plugin):
32     """
33     History management
34     """
35     def __init__(self, daemon):
36         Plugin.__init__(self, daemon)
37         self._last_clean = time()
38
39     def shutdown(self):
40         self.log.info('Cleaning database')
41         self.sdb.purge_history()
42
43     def _h_tip(self):
44         hist = self.sdb.fetch_history()
45         if hist:
46             return hist[0]
47         return None
48
49     def callback_player(self):
50         current = self.player.current
51         if not current:
52             if self.player.state == 'play':
53                 self.log.debug('Cannot add "%s" to history (empty or missing file)', current)
54             return
55         last_hist = self._h_tip()
56         if last_hist and last_hist == current:
57             return
58         self.log.debug('add history: "%s"', current)
59         self.sdb.add_history(current)
60         if time() - self._last_clean > 86400:
61             self.shutdown()
62             self._last_clean = time()
63
64
65 # VIM MODLINE
66 # vim: ai ts=4 sw=4 sts=4 expandtab