summaryrefslogtreecommitdiff
path: root/examples/rss-reader/tracker_backend.py
blob: 35d8f715ae4999b1aad0d223967c4674f8002be6 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#
# Demo RSS client using tracker as backend
# Copyright (C) 2009 Nokia <ivan.frade@nokia.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#

import dbus
import os

TRACKER = 'org.freedesktop.Tracker1'
TRACKER_OBJ = '/org/freedesktop/Tracker1/Resources'

FALSE = "false"
TRUE = "true"

QUERY_FIRST_ENTRIES = """
    SELECT ?entry ?title ?date ?isRead WHERE {
      ?entry a mfo:FeedMessage ;
         nie:title ?title ;
         nie:contentLastModified ?date .
    OPTIONAL {
       ?entry nmo:isRead ?isRead.
    }
    } ORDER BY DESC(?date) LIMIT %s
    """

SET_URI_AS_READED = """
    DELETE {<%s> nmo:isRead "%s".}
    INSERT {<%s> nmo:isRead "%s".}
    """

QUERY_ALL_SUBSCRIBED_FEEDS ="""
    SELECT ?feeduri ?title COUNT (?entries) AS e WHERE {
       ?feeduri a mfo:FeedChannel ;
                nie:title ?title.
       ?entries a mfo:FeedMessage ;
                nmo:communicationChannel ?feeduri.
    } GROUP BY ?feeduri
"""

QUERY_FOR_URI = """
    SELECT ?title ?date ?isRead ?channel WHERE {
      <%s> a mfo:FeedMessage ;
             nie:title ?title ;
             nie:contentLastModified ?date ;
             nmo:communicationChannel ?channel .
      OPTIONAL {
      <%s> nmo:isRead ?isRead.
      }
    }
"""

QUERY_FOR_TEXT = """
    SELECT ?text WHERE {
    <%s> nie:plainTextContent ?text .
    }
"""

CONF_FILE = os.path.expanduser ("~/.config/rss_tracker/rss.conf")

class TrackerRSS:

    def __init__ (self):
        bus = dbus.SessionBus ()
        self.tracker = bus.get_object (TRACKER, TRACKER_OBJ)
        self.iface = dbus.Interface (self.tracker,
                                     "org.freedesktop.Tracker1.Resources")
        self.invisible_feeds = []
        self.load_config ()
        

    def load_config (self):
        if (os.path.exists (CONF_FILE)):
            print "Loading %s" % (CONF_FILE)
            for line in open (CONF_FILE):
                line = line.replace ('\n','')
                if (len (line) > 0):
                    self.invisible_feeds.append (line)
            print "Hiding feeds from:", self.invisible_feeds
        else:
            if (not os.path.exists (os.path.dirname (CONF_FILE))):
                os.makedirs (os.path.dirname (CONF_FILE))
            f = open (CONF_FILE, 'w')
            f.close ()
        
    def get_post_sorted_by_date (self, amount):
        results = self.iface.SparqlQuery (QUERY_FIRST_ENTRIES % (amount))
        return results

    def set_is_read (self, uri, value):
        if (value):
            dbus_value = TRUE
            anti_value = FALSE
        else:
            dbus_value = FALSE
            anti_value = TRUE

        print "Sending ", SET_URI_AS_READED % (uri, anti_value, uri, dbus_value)
        self.iface.SparqlUpdate (SET_URI_AS_READED % (uri, anti_value, uri, dbus_value))

    def get_all_subscribed_feeds (self):
        """ Returns [(uri, feed channel name, entries, visible)]
        """
        componed = []
        results = self.iface.SparqlQuery (QUERY_ALL_SUBSCRIBED_FEEDS)
        for result in results:
            print "Looking for", result[0]
            if (result[0] in self.invisible_feeds):
                visible = False
            else:
                visible = True
            componed.insert (0, result + [visible])

        componed.reverse ()
        return componed

    def get_info_for_entry (self, uri):
        """  Returns (?title ?date ?isRead)
        """
        details = self.iface.SparqlQuery (QUERY_FOR_URI % (uri, uri))
        if (len (details) < 1):
            print "No details !??!!"
            return None
        if (len (details) > 1):
            print "OMG what are you asking for?!?!?!"
            return None

        info = details [0]
        if (info[3] in self.invisible_feeds):
            print "That feed is not visible"
            return None
        else:
            if (info[2] == TRUE):
                return (info[0], info[1], True)
            else:
                return (info[0], info[1], False)

    def get_text_for_uri (self, uri):
        text = self.iface.SparqlQuery (QUERY_FOR_TEXT % (uri))
        if (text[0]):
            text = text[0][0].replace ("\\n", "\n")
        else:
            text = ""
        return text

    def mark_as_invisible (self, uri):
        self.invisible_feeds.append (uri)

    def mark_as_visible (self, uri):
        self.invisible_feeds.remove (uri)

    def flush_to_file (self):
        f = open (CONF_FILE, 'w')
        for line in self.invisible_feeds:
            f.write (line + "\n")
        f.close ()