def upadteFeedItemsWithDBInfo(self): """ Обновляет информацию об интересности и рейтинги записи для записей фида на основании информации из БД. Используется после загрузки записей из сети для правильного отображения информации """ EntryInfo = collections.namedtuple('EntryInfo', 'ID, interesting, rating, data') connection = sqlite3.connect(settings.entriesDb()) cursor = connection.cursor() unreadEntries = {} try: cursor.execute("SELECT ID, IsInteresting, Rating, Data FROM Entries WHERE IsRead=0") unreadEntries = {ei.ID:ei for ei in map(EntryInfo._make, cursor.fetchall())} finally: connection.close() for item in self._feed.getItems(): id = item.id.split("/")[-1] if id in unreadEntries: item.interesting = unreadEntries[id].interesting item.rating = unreadEntries[id].rating id_prefix = 'tag:google.com,2005:reader/item/' missingIds = [] for entryId in unreadEntries: id = id_prefix + entryId if not id in self._feed.itemsById: missingIds.append(entryId) for id in missingIds: item = libgreader.Item(self._reader, json.loads(unreadEntries[id].data), self._feed) item.interesting = unreadEntries[id].interesting item.rating = unreadEntries[id].rating
def saveEntries(self): """ Сохранение записей в БД """ connection = sqlite3.connect(settings.entriesDb()) try: cursor = connection.cursor() cursor.execute('SELECT ID FROM Entries') known_ids = tuple(i[0] for i in cursor.fetchall()) for item in self._feed.getItems(): dbId = item.id.split('/')[-1] if dbId in known_ids: # Обновляем запись, если она уже есть sql = 'UPDATE Entries SET IsInteresting=? WHERE ID=?' cursor.execute(sql, (item.interesting, dbId)) else: # Добавляем запись, если её ещё нет (blog, title) = item.title.split('/', 1) values = (dbId, title.strip(), item.data['author'], blog.strip(), item.data['updated'], item.data['published'], item.isRead(), item.interesting, 0, json.dumps(item.data)) sql = 'INSERT INTO Entries VALUES ({})'.format(','.join(['?']*10)) cursor.execute(sql, values) connection.commit() finally: connection.close()
def updateDb(self): """ Обновляет информацию о текущих записях в БД """ connection = sqlite3.connect(settings.entriesDb()) cursor = connection.cursor() try: for i in self._feed.getItems(): id = i.id.split('/')[-1] if i.isRead(): cursor.execute('UPDATE Entries SET IsRead=? WHERE ID=?', (True, id)) if i.interesting: cursor.execute('UPDATE Entries SET IsInteresting=? WHERE ID=?', (True, id)) connection.commit() finally: connection.close()
def setItemRating(self): """ Устанавливаем рейтинг текущей записи """ a = self.sender() rating = len(a.text()) id = self._currentEntry().id.split("/")[-1] connection = sqlite3.connect(settings.entriesDb()) cursor = connection.cursor() try: cursor.execute('UPDATE Entries SET Rating=? WHERE ID=?', (rating, id)) connection.commit() if settings.markAsReadWhenGivingStars(): self.markCurrentAsRead() finally: connection.close()
def loadEntries(self): """" Загрузка записей из БД """ connection = sqlite3.connect(settings.entriesDb()) self._feed = libgreader.Feed(self._reader, 'Хабрахабр', 'feed/http://habrahabr.ru/rss/') self._reader.feedsById['feed/http://habrahabr.ru/rss/'] = self._feed self._categories = {} try: cursor = connection.cursor() file = QFile(':/sql/DbScheme.sql') if file.open(QIODevice.ReadOnly | QIODevice.Text): s = QTextStream(file) cursor.executescript(s.readAll()) cursor.execute('SELECT Data, IsInteresting FROM Entries WHERE IsRead=?', (False,)) for (data, interesting) in cursor.fetchall(): item = libgreader.Item(self._reader, json.loads(data), self._feed) item.interesting = interesting finally: connection.close() self.splitItemsIntoCategories() self._updateWindowTitle()