Skip to content

Instantly share code, notes, and snippets.

@mashiro
Created November 14, 2009 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mashiro/234695 to your computer and use it in GitHub Desktop.
Save mashiro/234695 to your computer and use it in GitHub Desktop.
import clr
import re
from System.Net import WebClient
from Misuzilla.Applications.TwitterIrcGateway import Status, Statuses, User, Users, Utility
from Misuzilla.Applications.TwitterIrcGateway.AddIns.DLRIntegration import DLRIntegrationAddIn, DLRBasicConfiguration, DLRContextHelper
username = "your bit.ly username"
apikey = "your bit.ly apikey"
re_shorten = re.compile(r"<shortUrl>(.+)</shortUrl>")
re_url = re.compile(r"s?https?://[-_.!~*'()a-zA-Z0-9;/?:@&=+$,%#]+")
def OnPreSendUpdateStatus(sender, e):
try:
if e.Text.Length < 140:
return
for long_url in re_url.findall(e.Text):
short_url = ShortenUrl(long_url)
e.Text = e.Text.replace(long_url, short_url)
except:
pass
def ShortenUrl(long_url):
request_url = "http://api.bit.ly/shorten?version=2.0.1&format=xml&longUrl=%s&login=%s&apiKey=%s" % (Utility.UrlEncode(long_url), username, apikey)
client = WebClient()
result = client.DownloadString(request_url)
return re_shorten.search(result).group(1)
def OnBeforeUnload(sender, e):
CurrentSession.PreSendUpdateStatus -= OnPreSendUpdateStatus
CurrentSession.PreSendUpdateStatus += OnPreSendUpdateStatus
CurrentSession.AddInManager.GetAddIn[DLRIntegrationAddIn]().BeforeUnload += OnBeforeUnload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment