Skip to content
Browse files

[WEBLINKS] Boost bitly integration, allow referencing links in the pa…

…st, Document this functionality
  • Loading branch information...
1 parent 4b19dd6 commit 287d923c1378a8b6271468c1b9f8ddff68e0377a @aquarion committed
Showing with 65 additions and 5 deletions.
  1. +9 −0 README.textile
  2. +56 −5 lampstand/reactions/weblink.py
View
9 README.textile
@@ -170,6 +170,14 @@ h2. Weblinks
Lampstand watches for people mentioning links on channel, which then get archived at "Maelfroth.org/Links":http://www.maelfroth.org/links.php
+You can request that lampstand create a http://foip.me link for any URL mentioned using these commands:
+
+@Lampstand: Shorten That Link@ (shortens the last URL mentioned)
+
+@Lampstand: Shorten Username's link@ (Shortens the last link Username mentioned)
+
+@Lampstand: Shorten this link: http://www.maelfroth.org@ (Makes a short link for http://maelfroth.org)
+
h2. What Is
@Lampstand: Define the world as a place where we live@
@@ -196,3 +204,4 @@ h2. Who was / Last seen
10:54 [Aquarion] Lampstand: Have you seen Aquarion?
10:54 [Lampstand] I last saw Aquarion on Sat, 08 March 08 at 10:54 saying Hello!
+
View
61 lampstand/reactions/weblink.py
@@ -1,6 +1,7 @@
import re, time
import lampstand.reactions.base
import bitly_api
+import urlparse
def __init__ ():
pass
@@ -9,14 +10,20 @@ class Reaction(lampstand.reactions.base.Reaction):
__name = 'Weblink'
def __init__(self, connection):
- self.channelMatch = [re.compile('.*https?\:\/\/', re.IGNORECASE), re.compile('%s: Shorten that( URL)?' % connection.nickname, re.IGNORECASE)]
+ self.channelMatch = [
+ re.compile('%s: Shorten that( URL)?' % connection.nickname, re.IGNORECASE), #0
+ re.compile('%s: Shorten (.*?)\'s? (link|url)' % connection.nickname, re.IGNORECASE), #1
+ re.compile('%s: Shorten this (link|url): (.*)$' % connection.nickname, re.IGNORECASE), #2
+ re.compile('.*https?\:\/\/', re.IGNORECASE)] #3
self.dbconnection = connection.dbconnection
self.bitly = bitly_api.Connection(connection.config.get("bitly","username"), connection.config.get("bitly","apikey"))
self.lastlink = {}
def channelAction(self, connection, user, channel, message, matchindex):
- if matchindex == 0:
+ print "[WEBLINK] Activated, matchindex is %d" % matchindex
+
+ if matchindex == 3: # Weblink
print "[WEBLINK] That looks like a weblink : %s" % message
links = self.grabUrls(message)
@@ -30,21 +37,65 @@ def channelAction(self, connection, user, channel, message, matchindex):
self.lastlink[channel] = {'id': cursor.lastrowid, 'url': links[0] }
self.dbconnection.commit()
- elif matchindex == 1:
+
+ elif matchindex == 0: # Shorten That
print "[WEBLINK] Shortening URL"
print self.lastlink
surl = self.bitly.shorten(self.lastlink[channel]['url'])
- output = "%s: %s" % (user, surl['url'])
-
+ url_split = urlparse.urlparse(self.lastlink[channel]['url'])
+ output = "%s: %s link shortened to %s" % (user, url_split[1], surl['url'])
connection.msg(channel,output.encode("utf-8"))
cursor = self.dbconnection.cursor()
cursor.execute('update urllist set shorturl = %s where id = %s', ( surl['url'], self.lastlink[channel]['id'] ) )
self.dbconnection.commit()
+ elif matchindex == 1: # SHorten user's url
+ for module in connection.channelModules:
+ if module.__name == "Memory":
+ memory = module;
+ matches = self.channelMatch[matchindex].findall(message)[0]
+ print matches
+ result = memory.search(channel, matches[0], "http");
+ print result
+
+ if len(result) == 0:
+ output = "%s: I've no idea which link you mean" % user
+ connection.msg(channel,output.encode("utf-8"))
+ else:
+ links = self.grabUrls(result[-1]['message'])
+
+ for link in links:
+ surl = self.bitly.shorten(link)
+ url_split = urlparse.urlparse(link)
+ output = "%s: %s link shortened to %s" % (user, url_split[1], surl['url'])
+ connection.msg(channel,output.encode("utf-8"))
+
+
+ elif matchindex == 2: # Shorten this URL
+ print "[WEBLINK] Shortening requested URL : %s" % message
+ links = self.grabUrls(message)
+
+ if len(links) == 0:
+ print "[WEBLINK] No links found"
+ connection.msg(channel, "%s: I see no links in that" % user)
+ return
+
+ print links
+
+ cursor = self.dbconnection.cursor()
+ for link in links:
+ print link
+ now = time.time()
+ surl = self.bitly.shorten(link)
+ cursor.execute('insert into urllist (time, username, message, channel, shorturl) values (%s, %s, %s, %s, %s)', (now, user, message, channel, surl['url']) )
+ url_split = urlparse.urlparse(link)
+ output = "%s: %s link shortened to %s" % (user, url_split[1], surl['url'])
+ connection.msg(channel,output.encode("utf-8"))
+
def grabUrls(self, text):

0 comments on commit 287d923

Please sign in to comment.
Something went wrong with that request. Please try again.