import feedparser
import configparser
import logging
import time
from common import Database, get_config

class FeedHandler():
    def __init__(self):
        logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.INFO)
        config   = get_config()
        self.DB  = Database(config)
        self.con = self.DB.get_connection()

    def update_feed(self, feed_id, feed_url=None):
        if feed_url == None:
            cur = self.con.cursor()
            cur.execute('SELECT url FROM lysr_feed WHERE id=%s', (feed_id,))
            self.con.commit()
            feed_url = cur.fetchone()[0]

        logging.info('Updating feed {}: {}'.format(feed_id, feed_url))

        feed = feedparser.parse(feed_url)
    
        new_entries = 0
        if feed.status is 200:
            try:
                cur = self.con.cursor()
    
                for entry in feed.entries:
                    # Bad HTML is removed by default
                    cur.execute('SELECT id FROM lysr_feed_entry WHERE feed = %s AND guid = %s', (feed_id, entry.link))
                    self.con.commit()
               
                    if cur.rowcount is 0:
                        new_entries += 1
                        cur.execute('INSERT INTO lysr_feed_entry (feed, guid, content, title) VALUES (%s, %s, %s, %s)',
                                (feed_id, entry.link, entry.description, entry.title))
                        self.con.commit()

                
                logging.info('Fetched feed {}, {} new entries found'.format(feed_id, new_entries))
    
            except Exception as e:
                logging.error('Database: {}'.format(str(e).split('\n')[0]))
        else:
            logging.info('Failed to fetch feed {}, status {}'.format(feed_id, feed.status))

        cur = self.con.cursor()

        cur.execute('UPDATE lysr_feed SET last_check=NOW() WHERE id=%s', (feed_id,))
        self.con.commit()

        if new_entries:
            cur.execute('UPDATE lysr_feed SET last_update=NOW() WHERE id=%s', (feed_id,))
        else:
            cur.execute('''UPDATE lysr_feed SET update_interval=LEAST(1.5*update_interval, '7d'::interval) WHERE id=%s''', (feed_id,))
        self.con.commit()


    def update_feeds(self):
        cur = self.con.cursor()
        cur.execute('SELECT id, url FROM lysr_feed WHERE NOW() > last_check + update_interval')
        self.con.commit()

        for feed in cur:
            self.update_feed(*feed)

    def handle_forever(self):
        logging.info('Starting feed handler')
        while True:
            self.update_feeds()
            time.sleep(15)

@@ -285,16 +285,17 @@
    self.
+_
 DB  = Da
@@ -322,16 +322,17 @@
    self.
+_
 con = se
@@ -334,16 +334,17 @@
  = self.
+_
 DB.get_c
@@ -451,32 +451,33 @@
      cur = self.
+_
 con.cursor()%0A   
@@ -559,32 +559,33 @@
            self.
+_
 con.commit()%0A   
@@ -833,32 +833,33 @@
      cur = self.
+_
 con.cursor()%0A   
@@ -1092,32 +1092,33 @@
            self.
+_
 con.commit()%0A   
@@ -1441,32 +1441,33 @@
            self.
+_
 con.commit()%0A%0A  
@@ -1814,32 +1814,33 @@
      cur = self.
+_
 con.cursor()%0A%0A  
@@ -1927,32 +1927,33 @@
 ))%0A        self.
+_
 con.commit()%0A%0A  
@@ -2218,32 +2218,33 @@
 ))%0A        self.
+_
 con.commit()%0A%0A%0A 
@@ -2285,24 +2285,25 @@
  cur = self.
+_
 con.cursor()
@@ -2399,16 +2399,16 @@
 erval')%0A
-
         
@@ -2412,16 +2412,17 @@
    self.
+_
 con.comm

Add underscore to "private" members