How to use the pmxbot.storage.MongoDBStorage function in pmxbot

To help you get started, we’ve selected a few pmxbot examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github yougov / pmxbot / pmxbot / rolls.py View on Github external
self.db.execute(INDEX_DTC_CREATE_SQL)
        self.db.execute(INDEX_DT_CREATE_SQL)
        self.db.commit()

    def log(self, nick, channel, change):
        INSERT_LOG_SQL = """
            INSERT INTO rolls
            (datetime, channel, nick, change)
            VALUES (?, ?, ?, ?)
            """
        now = datetime.datetime.utcnow()
        self.db.execute(INSERT_LOG_SQL, [now, channel, nick, change])
        self.db.commit()


class MongoDBLogger(ParticipantLogger, storage.MongoDBStorage):
    collection_name = 'rolls'

    def log(self, nick, channel, change):
        self.db.ensure_index(
            [
                ('datetime.d', storage.pymongo.DESCENDING),
                ('channel', storage.pymongo.ASCENDING),
            ]
        )
        now = datetime.datetime.utcnow()
        doc = dict(
            channel=channel,
            nick=nick,
            change=change,
            datetime=logging.MongoDBLogger._fmt_date(now),
        )
github yougov / pmxbot / pmxbot / rss.py View on Github external
def get_seen_feeds(self):
		return [row[0] for row in self.db.execute('select key from feed_seen')]

	def add_entries(self, entries):
		self.db.executemany('INSERT INTO feed_seen (key) values (?)', [(x,) for x in entries])
		self.db.commit()

	def clear(self):
		"Clear all entries"
		self.db.execute('DELETE FROM feed_seen')

	export_all = get_seen_feeds


class MongoDBFeedparserDB(FeedparserDB, storage.MongoDBStorage):
	collection_name = 'feed history'

	def get_seen_feeds(self):
		return [row['key'] for row in self.db.find()]

	def add_entries(self, entries):
		for entry in entries:
			self.db.insert(dict(key=entry))

	def import_(self, item):
		self.add_entries([item])

	def clear(self):
		"Clear all entries"
		self.db.remove()
github yougov / pmxbot / pmxbot / channels.py View on Github external
self.db.commit()

	def load(self):
		SQL = 'SELECT items FROM channels WHERE aspect = ?'
		res = next(self.db.execute(SQL, [self.aspect]), None)
		if not res:
			return
		items, = res
		self[:] = json.loads(items)

	def save(self):
		SQL = "INSERT or REPLACE INTO channels (aspect, items) values (?, ?)"
		self.db.execute(SQL, [self.aspect, json.dumps(self)])


class MongoDBChannels(ChannelList, storage.MongoDBStorage):
	collection_name = 'channels'

	def load(self):
		spec = dict(aspect=self.aspect)
		doc = self.db.find_one(spec) or {}
		self[:] = doc.get('items', [])

	def save(self):
		spec = dict(aspect=self.aspect)
		doc = dict(spec)
		doc.update(items=self)
		self.db.update(spec, doc, upsert=True)
github yougov / pmxbot / pmxbot / quotes.py View on Github external
# Note: also filter on quote not null, for backward compatibility
        query = "SELECT quote FROM quotes WHERE library = ? and quote is not null"
        for row in self.db.execute(query, [self.lib]):
            yield {'text': row[0]}

    def export_all(self):
        query = """
            SELECT quote, library, logid
            from quotes
            left outer join quote_log on quotes.quoteid = quote_log.quoteid
            """
        fields = 'text', 'library', 'log_id'
        return (dict(zip(fields, res)) for res in self.db.execute(query))


class MongoDBQuotes(Quotes, storage.MongoDBStorage):
    collection_name = 'quotes'

    def find_matches(self, thing):
        thing = thing.strip().lower()
        words = thing.split()

        def matches(quote):
            quote = quote.lower()
            return all(word in quote for word in words)

        return [
            row
            for row in self.db.find(dict(library=self.lib)).sort('_id')
            if matches(row['text'])
        ]
github yougov / pmxbot / pmxbot / logging.py View on Github external
fmts = ['%Y-%m-%d %H:%M:%S.%f', '%Y-%m-%d %H:%M:%S']
    for fmt in fmts:
        try:
            dt = datetime.datetime.strptime(dt, fmt)
            break
        except ValueError:
            pass
    else:
        raise
    tz = pytz.timezone('US/Pacific')
    loc_dt = tz.localize(dt)
    record['datetime'] = loc_dt
    return record


class MongoDBLogger(Logger, storage.MongoDBStorage):
    collection_name = 'logs'

    def _message(self, channel, nick, msg):
        self.db.create_index('datetime.d')
        self.db.create_index('channel')
        now = datetime.datetime.utcnow()
        doc = dict(
            channel=channel, nick=nick, message=msg, datetime=self._fmt_date(now)
        )
        res = self.db.insert_one(doc)
        self._add_recent(doc, res.inserted_id)

    def _add_recent(self, doc, logged_id):
        "Keep a tab on the most recent message for each channel"
        spec = dict(channel=doc['channel'])
        doc['ref'] = logged_id
github yougov / pmxbot / pmxbot / karma.py View on Github external
KEYS_SQL = "SELECT karmakey from karma_keys where karmaid = ?"
        value = self.db.execute(VALUE_SQL, [id]).fetchall()[0][0]
        keys_cur = self.db.execute(KEYS_SQL, [id]).fetchall()
        keys = sorted(x[0] for x in keys_cur)
        return keys, value

    def search(self, term):
        query = "SELECT distinct karmaid from karma_keys where karmakey like ?"
        matches = (id for (id,) in self.db.execute(query, '%%' + term + '%%'))
        return (self._lookup(id) for id in matches)

    def export_all(self):
        return self.list()


class MongoDBKarma(Karma, storage.MongoDBStorage):
    collection_name = 'karma'

    def lookup(self, thing):
        thing = thing.strip().lower()
        res = self.db.find_one({'names': thing})
        return res['value'] if res else 0

    def set(self, thing, value):
        thing = thing.strip().lower()
        value = int(value)
        query = {'names': {'$elemMatch': {'$in': [thing]}}}
        oper = {'$set': {'value': value}, '$addToSet': {'names': thing}}
        self.db.update_one(query, oper, upsert=True)

    def change(self, thing, change):
        thing = thing.strip().lower()
github yougov / pmxbot / pmxbot / stack.py View on Github external
"SELECT items FROM stack WHERE topic = ?", [topic]
        ).fetchone()
        if has_entry:
            if items:
                return self.db.execute(
                    "UPDATE stack SET items = ? WHERE topic = ?", [items, topic]
                )
            else:
                return self.db.execute("DELETE FROM stack WHERE topic = ?", [topic])
        else:
            return self.db.execute(
                "INSERT INTO stack (topic, items) VALUES (?, ?)", [topic, items]
            )


class MongoDBStack(Stack, storage.MongoDBStorage):
    collection_name = 'stack'

    def get_topics(self):
        docs = self.db.find({}, {'topic': True})
        return [doc['topic'] for doc in docs]

    def get_items(self, topic):
        doc = self.db.find_one({'topic': topic})
        if doc is None:
            return []
        else:
            return doc["items"]

    def save_items(self, topic, items):
        if items:
            return self.db.update_one(
github yougov / pmxbot / pmxbot / notify.py View on Github external
query = """
            DELETE FROM notify
            WHERE notifyid IN (%s)
            """ % ','.join(
            '?' for x in ids
        )
        self.db.execute(query, ids)

        return messages

    def notify(self, fromnick, tonick, message):
        query = "INSERT INTO notify (fromnick, tonick, message) values (?,?,?)"
        return self.db.execute(query, (fromnick, tonick, message))


class MongoDBNotify(Notify, storage.MongoDBStorage):
    collection_name = 'notify'

    def lookup(self, nick):
        nick = nick.strip().lower()
        messages = []
        res = self.db.find({'tonick': nick}).sort('notifications')
        for msg in res:
            messages.append(msg)
            self.db.remove(msg)

        return messages

    def notify(self, fromnick, tonick, message):
        fromnick = fromnick.strip().lower()
        tonick = tonick.strip().lower()
        notification = {
github yougov / pmxbot / pmxbot / saysomething.py View on Github external
	@abc.abstractmethod
	def update(self, initial, follows):
		"""
		Associate the two words where initial immediately preceeds
		follows.
		"""

	@abc.abstractmethod
	def next(self, initial):
		"""
		Given initial, return a word that would have followed it,
		proportional to the frequency encountered.
		"""


class MongoDBChains(Chains, pmxbot.storage.MongoDBStorage):
	"""
	Store word associations in MongoDB with documents like so:

	{
		'_id': ,
		'begets': 
	}
	"""
	collection_name = 'chains'

	def update(self, initial, follows):
		"""
		Given two words, initial then follows, associate those words
		"""
		filter = dict(_id=initial)
		key = 'begets.' + fields.encode(follows)