0001#!/usr/bin/env python
0002
0003from config import Config
0004from feed_collector import FeedCollector, NotFound,        TooYoung, FetchError, ParseError, FormatError
0006import sys, getopt, os, logging
0007
0008class Aggregator:
0009    """ iAggregator uses Config, Mailer & FeedCollector
0010        to work peacefully by collecting some feeds on
0011        locations mentionned on the config file and mail
0012        them to some body. """
0013
0014
0015    def getConfig(self):
0016        return Config()
0017
0018    def getCollector(self):
0019        return FeedCollector(self)
0020
0021    def getFeeds(self):
0022        return self.getConfig().getChannels().keys()
0023
0024    def update(self):
0025        allEntries = {}
0026        for feed in self.getFeeds():
0027            allEntries.update(self.updateFeed(feed))
0028        return allEntries
0029
0030    def updateFeed(self, feed):
0031        entries = {feed: {} }
0032        config = self.getConfig()
0033        encoding = config.getEncoding()
0034        infos = config.getChannels()[feed]
0035        logger = logging.getLogger()
0036
0037        # channel is the name associated with the location url
0038        if len(infos) == 4:
0039            url, refreshRate, filters, mungeTorrentFunc = infos
0040        else:
0041            url, refreshRate, filters = infos
0042            mungeTorrentFunc = lambda u: u
0043        try:
0044            result = self.getCollector().collect(feed,url, refreshRate, filters, mungeTorrentFunc)
0045        except (ParseError,FetchError), e:
0046            logger.debug(e)
0047        except (NotFound, TooYoung), e:
0048            pass
0049        else:
0050            # hey we've found something 
0051            logger.info('Found %s new items in feed [%s]' % (len(result['entries']), feed))
0052            entries[feed] = result['entries']
0053        return entries
0054
0055if __name__ == '__main__':
0056    print Aggregator().update()