0001import os,socket
0002
0003HOME = "~/.fmTorrent"
0004CONF = """\
0005# the config uses python syntax.. have phun :)
0006import re
0007
0008# Filters ... They are then associated w/ Channels, see below
0009Filter_vtv = "(?P<Show>.*)\s\d+x\d+\s\(HDTV\-TCM\)*"
0010
0011# if links found in RSS don't point directly to torrents, you can
0012# figure out how to guess the torrent url. Here is an example:
0013def getTorrentFromPage(pageUrl):
0014    import re
0015    return re.sub('http\://foo\.com/torrent/(\d+)',
0016                  r'http://foo.com/get_torrent/\1', pageUrl)
0017
0018# Please no ?!*# or things like this in channel names
0019# urls can refer to full RSS path (if you know it) .. or not :)
0020Channels = { 'Alias' : ('http://www.mininova.org/rss.xml?sub=118', '1d', [Filter_vtv,], getTorrentFromPage),
0021             # ...
0022           }
0023# explanation: here we update Alias feeds every day. And we apply the VTV filter to
0024# its content, because we only want VTV releases ...
0025#              Got the trick ? :)
0026# warning: refresh rates have to respect the
0027#          rule (nd:mh:om) .. colon is mandatory
0028
0029# directory storing old feeds by channel
0030CacheDir = "%(home)s/cache"
0031
0032Encoding = "iso8859-1"
0033
0034# keywords white-list -- stores a list of strings.
0035# Use it when you want to filter content of RSS feeds
0036# example:
0037# WhiteListKeywords = [ 'foo', 'bar', 'python' ]
0038WhiteListKeywords = [ ]
0039
0040# keywords black-list -- stores a list of strings that you don't want to see
0041# If any RSS feed item matches one of those keywords, it won't be showed to you
0042# example:
0043# BlackListKeywords = [ 'porn', 'p0rn', 'java' ]
0044BlackListKeywords = [ ]
0045
0046"""
0047
0048RC = os.path.join(HOME,"config.py")
0049
0050class Config:
0051
0052    def __init__(self, rcFile=RC):
0053        """ Config management. When running iAggregator
0054            for 1st time, this generates a default rc file. """
0055        self.rc = rc = os.path.expanduser(rcFile)
0056        home = os.path.expanduser(HOME)
0057        if not os.path.exists(home): os.mkdir(home)
0058        if not os.path.exists(rc):
0059            conf = CONF % {'host': socket.getfqdn(),
0060                           'home': HOME }
0061            f = open(rc,'w')
0062            f.write(conf)
0063            f.close()
0064            self._warn(rc)
0065        else: execfile(rc,globals(),locals())
0066        self.channels = locals()['Channels']
0067        c = self.cacheDir = os.path.expanduser(locals()['CacheDir'])
0068        if not os.path.exists(c):
0069            os.mkdir(c)
0070        self.whiteList = locals()['WhiteListKeywords']
0071        self.blackList = locals()['BlackListKeywords']
0072        self.encoding = locals()['Encoding']
0073
0074    def _warn(self, rc):
0075        """ little helper to remind user to configure iAggregator
0076            after self config generation. """
0077        raise RuntimeError, "You need to configure aggregator's rc file: [%s]" % rc
0078        #print "Exiting..."
0079        #import sys; sys.exit()
0080
0081    # Accessors
0082    def getCacheDir(self): return self.cacheDir
0083    def getRcFile(self):   return self.rc
0084    def getChannels(self): return self.channels
0085    def getWhiteListKeywords(self): return self.whiteList
0086    def getBlackListKeywords(self): return self.blackList
0087    def getEncoding(self): return self.encoding
0088
0089if __name__ == '__main__':
0090    c = Config()
0091    print c.getChannels()