Skip to content
This repository has been archived by the owner on May 12, 2020. It is now read-only.

Commit

Permalink
[WEBLINKS] Boost bitly integration, allow referencing links in the pa…
Browse files Browse the repository at this point in the history
…st, Document this functionality
  • Loading branch information
aquarion committed Apr 12, 2012
1 parent 4b19dd6 commit 287d923
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
9 changes: 9 additions & 0 deletions README.textile
Expand Up @@ -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@
Expand All @@ -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!

61 changes: 56 additions & 5 deletions lampstand/reactions/weblink.py
@@ -1,6 +1,7 @@
import re, time
import lampstand.reactions.base
import bitly_api
import urlparse

def __init__ ():
pass
Expand All @@ -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)
Expand All @@ -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):
Expand Down

0 comments on commit 287d923

Please sign in to comment.