aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBlas Oliva <blasoliva@gmail.com>2015-01-19 23:47:42 -0300
committerBlas Oliva <blasoliva@gmail.com>2015-01-19 23:47:42 -0300
commit4eb3d3f29c30515c20dc422b32d977452283e044 (patch)
treeb9ca99f56f23f08f957ead357c7a3c906dbf56e3
parent869813376d170c7f2a6b3a856358b5ac775806c1 (diff)
downloadreebot-4eb3d3f29c30515c20dc422b32d977452283e044.tar.gz
reebot-4eb3d3f29c30515c20dc422b32d977452283e044.zip
Deployment MongoDB
-rwxr-xr-xrbot.py39
-rw-r--r--reemongo.py14
2 files changed, 30 insertions, 23 deletions
diff --git a/rbot.py b/rbot.py
index e27106d..c948688 100755
--- a/rbot.py
+++ b/rbot.py
@@ -8,18 +8,23 @@ import sys
8import threading 8import threading
9import time 9import time
10import os 10import os
11
11from config import * 12from config import *
12from messages import messages 13from messages import messages
14from reemongo import reemongo
13 15
14def main(): 16def main():
15 try: 17 try:
16 c = rBot() 18 db = reemongo()
19 c = rBot(db)
17 except irclib.ServerConnectionError, e: 20 except irclib.ServerConnectionError, e:
18 exit() 21 exit()
19 22
20class rBot: 23class rBot:
21 def __init__(self, connection_interval=60): 24 def __init__(self, db):
22 """ IRC objects constructor """ 25 """ IRC objects constructor """
26 # MongoDB connection
27 self.db = db
23 # Create IRC object and connect to the network 28 # Create IRC object and connect to the network
24 self.irc = irclib.IRC() 29 self.irc = irclib.IRC()
25 self.server = self.irc.server() 30 self.server = self.irc.server()
@@ -78,37 +83,25 @@ class rBot:
78 self.sendmessage( target, messages['hello'] + source ) 83 self.sendmessage( target, messages['hello'] + source )
79 84
80 def feed_refresh(self): 85 def feed_refresh(self):
81 86
82 old_feeds = []
83 new_feeds = []
84 msgqueue = [] 87 msgqueue = []
85 88
86 # Reading old feeds from feeds.log file 89 # Get feeds list from mongo
87 with open( log_path, 'r' ) as f: 90 feed_list = self.db.feed_list.find()
88 old_feeds = [ line.strip() for line in f ]
89 91
90 # Loop over feeds list 92 # Loop over feeds list
91 for feed_source in feed_list: 93 for feed in feed_list:
92 name, source = feed_source.split( '|' ) 94 feeds = feedparser.parse( feed['url'] )
93 feeds = feedparser.parse( source )
94 95
95 # Loop over feeds entries 96 # Loop over feeds entries
96 for entry in feeds.entries: 97 for entry in feeds.entries:
97 link = [ entry.link.encode('utf-8') ] 98 if self.db.log.find_one( { "url" : entry.link.encode( 'utf-8' ) } ) is None:
98 # If link doesn't exists in old feeds add it to msgqueue list 99 self.db.log.insert( { "url" : entry.link.encode( 'utf-8' ) } )
99 if link[0] not in old_feeds: 100 msgqueue.append( feed['name']
100 msgqueue.append( name
101 + " | " + feeds.feed.title.encode( 'utf-8' ) 101 + " | " + feeds.feed.title.encode( 'utf-8' )
102 + " > " + entry.title.encode( 'utf-8' ) 102 + " > " + entry.title.encode( 'utf-8' )
103 + " : " + entry.link.encode( 'utf-8' ) ) 103 + " : " + entry.link.encode( 'utf-8' ) )
104 new_feeds.append(link[0]) 104
105
106 # Insert new feeds into feeds.log
107 nf = open( log_path, "a" )
108 for item in new_feeds:
109 nf.write( "%s\n" % item )
110 nf.close()
111
112 while len( msgqueue ) > 0: 105 while len( msgqueue ) > 0:
113 msgq = msgqueue.pop() 106 msgq = msgqueue.pop()
114 for channel in channels_list: 107 for channel in channels_list:
diff --git a/reemongo.py b/reemongo.py
new file mode 100644
index 0000000..0108d3b
--- /dev/null
+++ b/reemongo.py
@@ -0,0 +1,14 @@
1#!/usr/bin/python
2""" MongoDB connection module """
3
4import pymongo
5from config import db_params
6
7class reemongo:
8 def __init__(self):
9 self.client = pymongo.MongoClient(db_params['uri'])
10 self.db = self.client[db_params['database']]
11 self.feed_list = self.db[db_params['feed_list']]
12 self.log = self.db[db_params['log']]
13
14