moved configuration to external file, plus a little refactoring
This commit is contained in:
parent
a00e703068
commit
c3954e31a5
6
atomstrom.conf.sample
Executable file
6
atomstrom.conf.sample
Executable file
@ -0,0 +1,6 @@
|
|||||||
|
[database]
|
||||||
|
engine = mysql
|
||||||
|
user = atomstrom
|
||||||
|
password = atomstrom
|
||||||
|
hostname = localhost
|
||||||
|
database = atomstrom
|
51
atomstrom.py
51
atomstrom.py
@ -10,6 +10,7 @@ import sys
|
|||||||
import urllib
|
import urllib
|
||||||
import hn
|
import hn
|
||||||
import html2text
|
import html2text
|
||||||
|
import ConfigParser
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
|
||||||
Base = declarative_base()
|
Base = declarative_base()
|
||||||
@ -124,15 +125,6 @@ class Entry(Base):
|
|||||||
self.lastfetched = datetime.datetime.now()
|
self.lastfetched = datetime.datetime.now()
|
||||||
|
|
||||||
|
|
||||||
engine = create_engine('mysql://atomstrom:mdRTR4b8PLDqRSA4@localhost/atomstrom')
|
|
||||||
Base.metadata.create_all(engine)
|
|
||||||
|
|
||||||
Session = sessionmaker(bind=engine)
|
|
||||||
session = Session()
|
|
||||||
|
|
||||||
#session.add(Feed('http://www.heise.de/newsticker/heise-atom.xml', 1, 0, 0, 1))
|
|
||||||
#session.add(Feed('http://blog.schatenseite.de/feed/', 1, 0, 0, 1))
|
|
||||||
|
|
||||||
def send_mail(sender, subject, body):
|
def send_mail(sender, subject, body):
|
||||||
print 'Sender: %s' % sender.decode('latin-1')
|
print 'Sender: %s' % sender.decode('latin-1')
|
||||||
print 'Subject: %s' % subject.decode('latin-1')
|
print 'Subject: %s' % subject.decode('latin-1')
|
||||||
@ -149,7 +141,7 @@ def get_entry_text(entry):
|
|||||||
text = 'no text, sorry'
|
text = 'no text, sorry'
|
||||||
return text
|
return text
|
||||||
|
|
||||||
def mail_daily_digest():
|
def mail_daily_digest(session):
|
||||||
print 'mailing daily digest...'
|
print 'mailing daily digest...'
|
||||||
sender = 'atomstrom'
|
sender = 'atomstrom'
|
||||||
body = ''
|
body = ''
|
||||||
@ -174,7 +166,7 @@ def mail_single_entry(feed, feedinfo, entry):
|
|||||||
body = body + 'link: [%s]\n' % entry.link
|
body = body + 'link: [%s]\n' % entry.link
|
||||||
send_mail(sender, subject, body)
|
send_mail(sender, subject, body)
|
||||||
|
|
||||||
def mail_single_entries():
|
def mail_single_entries(session):
|
||||||
print 'mailing single entries...'
|
print 'mailing single entries...'
|
||||||
for feed, feedinfo, entry in session.query(Feed, Feedinfo, Entry).filter(Feed.id==Feedinfo.feed_id).filter(Feed.id==Entry.feed_id).filter(Feed.enabled==1).filter(Feed.daily==0).all():
|
for feed, feedinfo, entry in session.query(Feed, Feedinfo, Entry).filter(Feed.id==Feedinfo.feed_id).filter(Feed.id==Entry.feed_id).filter(Feed.enabled==1).filter(Feed.daily==0).all():
|
||||||
mail_single_entry(feed, feedinfo, entry)
|
mail_single_entry(feed, feedinfo, entry)
|
||||||
@ -192,7 +184,7 @@ def fetch_full_page(link):
|
|||||||
text = html2text.html2text(html)
|
text = html2text.html2text(html)
|
||||||
return text.encode('latin-1', 'replace')
|
return text.encode('latin-1', 'replace')
|
||||||
|
|
||||||
def process_feed_entry(feed, entry):
|
def process_feed_entry(session, feed, entry):
|
||||||
#query = session.query(Entry).filter_by(feed_id=feed.id, title=entry.title.encode('latin-1', 'replace'))
|
#query = session.query(Entry).filter_by(feed_id=feed.id, title=entry.title.encode('latin-1', 'replace'))
|
||||||
title = entry.title.encode('latin-1', 'replace')
|
title = entry.title.encode('latin-1', 'replace')
|
||||||
link = entry.link.encode('latin-1', 'replace')
|
link = entry.link.encode('latin-1', 'replace')
|
||||||
@ -219,7 +211,7 @@ def process_feed_entry(feed, entry):
|
|||||||
feed.entry.append(thisentry)
|
feed.entry.append(thisentry)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
def fetch_single_feed(feed):
|
def fetch_single_feed(session, feed):
|
||||||
print 'fetching %s' % feed.url
|
print 'fetching %s' % feed.url
|
||||||
parser = feedparser.parse(feed.url)
|
parser = feedparser.parse(feed.url)
|
||||||
print 'processing feed info...'
|
print 'processing feed info...'
|
||||||
@ -236,17 +228,36 @@ def fetch_single_feed(feed):
|
|||||||
entries_total = 0
|
entries_total = 0
|
||||||
for entry in parser.entries:
|
for entry in parser.entries:
|
||||||
entries_total = entries_total + 1
|
entries_total = entries_total + 1
|
||||||
entries_new = entries_new + process_feed_entry(feed, entry)
|
entries_new = entries_new + process_feed_entry(session, feed, entry)
|
||||||
session.commit()
|
session.commit()
|
||||||
print 'updated %d of %d entries' % (entries_new, entries_total)
|
print 'updated %d of %d entries' % (entries_new, entries_total)
|
||||||
|
|
||||||
def fetch_all_feeds():
|
def fetch_all_feeds(session):
|
||||||
print 'fetching all feeds...'
|
print 'fetching all feeds...'
|
||||||
for feed in session.query(Feed).filter_by(enabled=1).order_by(Feed.id):
|
for feed in session.query(Feed).filter_by(enabled=1).order_by(Feed.id):
|
||||||
fetch_single_feed(feed)
|
fetch_single_feed(session, feed)
|
||||||
print
|
print
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
config = ConfigParser.ConfigParser()
|
||||||
|
config.read('atomstrom.conf')
|
||||||
|
|
||||||
|
dbconnectstring = '%s://%s:%s@%s/%s' % (
|
||||||
|
config.get('database', 'engine'),
|
||||||
|
config.get('database', 'user'),
|
||||||
|
config.get('database', 'password'),
|
||||||
|
config.get('database', 'hostname'),
|
||||||
|
config.get('database', 'database'),
|
||||||
|
)
|
||||||
|
engine = create_engine(dbconnectstring)
|
||||||
|
Base.metadata.create_all(engine)
|
||||||
|
|
||||||
|
Session = sessionmaker(bind=engine)
|
||||||
|
session = Session()
|
||||||
|
|
||||||
|
#session.add(Feed('http://www.heise.de/newsticker/heise-atom.xml', 1, 0, 0, 1, 1))
|
||||||
|
#session.add(Feed('http://blog.schatenseite.de/feed/', 1, 0, 0, 1, 1))
|
||||||
|
|
||||||
parser = OptionParser()
|
parser = OptionParser()
|
||||||
parser.add_option("-f", "--fetch", action="store_true", dest="fetch", default=False, help="fetch all feeds")
|
parser.add_option("-f", "--fetch", action="store_true", dest="fetch", default=False, help="fetch all feeds")
|
||||||
parser.add_option("-s", "--single", action="store_true", dest="single", default=False, help="send single mails")
|
parser.add_option("-s", "--single", action="store_true", dest="single", default=False, help="send single mails")
|
||||||
@ -254,10 +265,10 @@ if __name__ == '__main__':
|
|||||||
(options, args) = parser.parse_args()
|
(options, args) = parser.parse_args()
|
||||||
|
|
||||||
if options.fetch:
|
if options.fetch:
|
||||||
fetch_all_feeds()
|
fetch_all_feeds(session)
|
||||||
if options.single:
|
if options.single:
|
||||||
mail_single_entries()
|
mail_single_entries(session)
|
||||||
if options.daily:
|
if options.daily:
|
||||||
mail_daily_digest()
|
mail_daily_digest(session)
|
||||||
|
|
||||||
session.commit()
|
session.commit()
|
||||||
|
Loading…
Reference in New Issue
Block a user