Skip to content

Commit

Permalink
prepare bot commands for pages monitoring (cf #18)
Browse files Browse the repository at this point in the history
  • Loading branch information
RouxRC committed Jan 4, 2016
1 parent 59a8837 commit 55f6360
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 20 deletions.
20 changes: 15 additions & 5 deletions LIST_COMMANDS.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@
> Prints stats on the Twitter account set for the channel.
> > restricted to /TWITTER
# Twitter & RSS Feeds monitoring commands
* (Un)Follow and (Un)Filter available only to GLOBAL_USERS and chan's USERS
# Twitter, RSS Feeds & Webpages monitoring commands
* (Un)Follow, (Un)Filter and Monitor available only to GLOBAL_USERS and chan's USERS
* Others available to anyone
* **Exclude regexp :** `'(u?n?f(ollow|ilter)|list|newsurl|last(tweet|news)|digest)'`
* **Exclude regexp :** `'(u?n?f(ollow|ilter)|u?n?monitor|list|newsurl|last(tweet|news)|digest)'`
* **List :**

+ `follow <name url|text|@user>`
Expand All @@ -168,6 +168,16 @@
> Asks me to stop following and displaying elements from a RSS named &lt;name&gt;, or tweets matching &lt;text&gt; or from &lt;@user&gt;.
> > restricted to /AUTH
+ `monitor <name>`

> Asks me to regularily check and tell if the webpage at &lt;url&gt; and identified as &lt;name&gt; changes.
> > restricted to /AUTH
+ `unmonitor <name>`

> Asks me to stop monitoring changes on the webpage named &lt;name&gt;.
> > restricted to /AUTH
+ `filter <word|@user>`

> Filters the display of tweets or news containing &lt;word&gt; or sent by user &lt;@user&gt;.
Expand All @@ -178,9 +188,9 @@
> Removes a tweets display filter for &lt;word&gt; or &lt;@user&gt;.
> > restricted to /AUTH
+ `list [--chan <channel>] <tweets|news|filters>`
+ `list [--chan <channel>] <tweets|news|filters|pages>`

> Displays the list of filters or news or tweets queries followed for current channel or optional &lt;channel&gt;.
> Displays the list of filters or pages monitored or news or tweets queries followed for current channel or optional &lt;channel&gt;.
+ `newsurl <name>`

Expand Down
40 changes: 25 additions & 15 deletions gazouilleur/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1046,11 +1046,11 @@ def command_stats(self, rest, channel=None, nick=None):
return stats.print_last()


# Twitter & RSS Feeds monitoring commands
# ---------------------------------------
## (Un)Follow and (Un)Filter available only to GLOBAL_USERS and chan's USERS
# Twitter, RSS Feeds & Webpages monitoring commands
# -------------------------------------------------
## (Un)Follow, (Un)Filter and Monitor available only to GLOBAL_USERS and chan's USERS
## Others available to anyone
## Exclude regexp : '(u?n?f(ollow|ilter)|list|newsurl|last(tweet|news)|digest)'
## Exclude regexp : '(u?n?f(ollow|ilter)|u?n?monitor|list|newsurl|last(tweet|news)|digest)'

@inlineCallbacks
def _restart_feeds(self, channel):
Expand All @@ -1065,40 +1065,42 @@ def _restart_feeds(self, channel):

re_url = re.compile(r'\s*(https?://\S+)\s*', re.I)
@inlineCallbacks
def command_follow(self, query, channel=None, nick=None):
def command_follow(self, query, channel=None, nick=None, webpages=False):
"""follow <name url|text|@user> : Asks me to follow and display elements from a RSS named <name> at <url>, or tweets matching <text> or from <@user>./AUTH"""
channel = self.getMasterChan(channel)
url = self.re_url.search(query)
if url and url.group(1):
database = 'news'
database = 'news' if not webpages else 'pages'
name = remove_ext_quotes(query.replace(url.group(1), '').strip().lower())
query = url.group(1)
elif webpages:
returnD("Please specify a url for the page %s you want to monitor (%shelp monitor for more info)." % (query, COMMAND_CHAR_DEF))
else:
database = 'tweets'
name = 'TWEETS: %s' % query
if query == "":
returnD("Please specify what you want to follow (%shelp follow for more info)." % COMMAND_CHAR_DEF)
if len(query) > 300:
returnD("Please limit your follow queries to a maximum of 300 characters")
if database == "news" and name == "":
if database != "tweets" and name == "":
returnD("Please provide a name for this url feed.")
yield self.db['feeds'].update({'database': database, 'channel': channel, 'name': name}, {'database': database, 'channel': channel, 'name': name, 'query': query, 'user': nick, 'timestamp': datetime.today()}, upsert=True)
if database == "news":
query = "%s <%s>" % (name, query)
if database == "tweets":
reactor.callLater(0.5, self._restart_feeds, channel)
else:
query = "%s <%s>" % (name, query)
returnD('«%s» query added to %s database for %s' % (query, database, channel))

re_clean_query = re.compile(r'([()+|$])')
regexp_feedquery = lambda self, x: re.compile(r'^%s$' % self.re_clean_query.sub(r'\\\1', x), re.I)
@inlineCallbacks
def command_unfollow(self, query, channel=None, *args):
def command_unfollow(self, query, channel=None, nick=None, webpages=False):
"""unfollow <name|text|@user> : Asks me to stop following and displaying elements from a RSS named <name>, or tweets matching <text> or from <@user>./AUTH"""
channel = self.getMasterChan(channel)
query = query.strip("«»")
database = 'news'
database = 'news' if not webpages else 'pages'
res = yield self.db['feeds'].remove({'channel': channel, 'name': self.regexp_feedquery(remove_ext_quotes(query)), 'database': database}, safe=True)
if not res or not res['n']:
if not ((res and res['n']) or webpages):
database = 'tweets'
res = yield self.db['feeds'].remove({'channel': channel, 'query': self.regexp_feedquery(query), 'database': database}, safe=True)
if not res or not res['n']:
Expand All @@ -1107,6 +1109,14 @@ def command_unfollow(self, query, channel=None, *args):
reactor.callLater(0.5, self._restart_feeds, channel)
returnD('«%s» query removed from %s database for %s' % (query, database, channel))

def command_monitor(self, query, channel=None, nick=None):
"""monitor <name> : Asks me to regularily check and tell if the webpage at <url> and identified as <name> changes./AUTH"""
return self.command_follow(query, channel, nick, webpages=True)

def command_unmonitor(self, query, channel=None, *args):
"""unmonitor <name> : Asks me to stop monitoring changes on the webpage named <name>./AUTH"""
return self.command_unfollow(query, channel, webpages=True)

@inlineCallbacks
def command_filter(self, keyword, channel=None, nick=None):
"""filter <word|@user> : Filters the display of tweets or news containing <word> or sent by user <@user>./AUTH"""
Expand All @@ -1131,14 +1141,14 @@ def command_unfilter(self, keyword, channel=None, nick=None):

@inlineCallbacks
def command_list(self, database, channel=None, *args):
"""list [--chan <channel>] <tweets|news|filters> : Displays the list of filters or news or tweets queries followed for current channel or optional <channel>."""
"""list [--chan <channel>] <tweets|news|filters|pages> : Displays the list of filters or pages monitored or news or tweets queries followed for current channel or optional <channel>."""
try:
database, channel = self._get_chan_from_command(database, channel)
except Exception as e:
returnD(str(e))
database = database.strip()
if database != "tweets" and database != "news" and database != "filters":
returnD('Please enter either «%slist tweets», «%slist news» or «%slist filters».' % (COMMAND_CHAR_DEF, COMMAND_CHAR_DEF, COMMAND_CHAR_DEF))
if database not in ["tweets", "news", "filters", "pages"]:
returnD('Please enter either «%slist tweets», «%slist news», «%slist pages» or «%slist filters».' % (COMMAND_CHAR_DEF, COMMAND_CHAR_DEF, COMMAND_CHAR_DEF, COMMAND_CHAR_DEF))
if database == "filters":
feeds = assembleResults(self.filters[channel.lower()])
else:
Expand Down
3 changes: 3 additions & 0 deletions gazouilleur/lib/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ def ensure_indexes(db):
yield db['feeds'].ensure_index(sortasc('channel') + sortasc('database') + sortdesc('timestamp'), background=True)
yield db['filters'].ensure_index(sortasc('channel'), background=True)
yield db['filters'].ensure_index(sortasc('channel') + sortasc('keyword') + sortdesc('timestamp'), background=True)
yield db['pages'].ensure_index(sortdesc('_id') + sortasc('channel'), background=True)
yield db['pages'].ensure_index(sortasc('channel') + sortdesc('timestamp'), background=True)
yield db['pages'].ensure_index(sortasc('channel') + sortasc('source') + sortdesc('timestamp'), background=True)
yield db['news'].ensure_index(sortdesc('_id') + sortasc('channel'), background=True)
yield db['news'].ensure_index(sortasc('channel') + sortdesc('timestamp'), background=True)
yield db['news'].ensure_index(sortasc('channel') + sortasc('source') + sortdesc('timestamp'), background=True)
Expand Down

0 comments on commit 55f6360

Please sign in to comment.