3D Clipboard

A place to discuss 3D Clipboard

Shorten URL with Bitly

Share actions you've created here.
Austin
Posts: 12
Joined: Wed Jul 06, 2011 9:16 pm

Shorten URL with Bitly

Postby Austin » Fri Jul 29, 2011 8:31 pm

Here is a simple Action that will shorten a URL with the Bitly web API

You will need your bitly username and your bitly api_key
Bitly api key available from http://bitly.com/a/your_api_key

Code: Select all

'Austin James
'7/29/2011

 
Const BITLY_USERNAME = "<YOUR_BITLY_USERNAME>"
Const BITLY_API_KEY = "<YOUR_BITLY_API_KEY>"
'Character Set used was found at http://www.blooberry.com/indexdot/html/ ... coding.htm
Const VALID_CHAR_SET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890$-_.+!*'(),"
 
Dim i
Dim c
Dim bitlyURL
Dim encodedURL
Dim inputURL
Dim objHttp
 
inputURL = clipboard.value
 
encodedURL = ""
 
'Encode the URL.  This may have logical errors in it.  This is a rather quick and dirty encode routine.
If Len(inputURL) > 0 Then
' Loop through each character in the URL and encode it
i = 1
Do While i <= Len(inputURL)
        c = Mid(inputURL, i, 1)
        If c = "%" Then
       'If we find an encoded character, keep it
      encodedURL = encodedURL & c & mid(inputURL, i+1,1) & mid(inputURL, i+2,1)
       i = i + 2
        ElseIf InStr(VALID_CHAR_SET, c) = 0 Then 'If current character is not in the valid character set
      ' convert current character to HEX
                  c = Hex(Asc(c))
               
                  'Prepend % and leading zeros (if necessary) to hex code
                  If Len(c) = 1 Then
                     c = "%0" & c
                  Else
                     c = "%" & c
                  End If
   End If
   encodedURL = encodedURL & c
       
   i = i + 1
Loop
 
End If
 
'Format URL to send to Bitly
bitlyURL = "http://api.bitly.com/v3/shorten?login=" & BITLY_USERNAME & "&apiKey=" & BITLY_API_KEY & "&longUrl=" & encodedURL & "&format=txt"
 
Set objHttp = CreateObject("Microsoft.XmlHttp")
'Send request to bitly and place short url on clipboard
objHttp.open "GET", bitlyURL, False
objHttp.send ""
clipboard.value =  objHttp.responseText

I wrote a simple URL encoding functionality in this action. It isn't great but will work for most URLs

Vince
3D Clipboard Author
Posts: 22
Joined: Wed Jul 06, 2011 12:25 am

Re: Shorten URL with Bitly

Postby Vince » Fri Jul 29, 2011 8:42 pm

Very nice. I was waiting for someone to write a URL shortener action. Since dealing with URLs will be common within actions, a future version will have some built-in helper functions so you won't have to write your own encode routines in each action.

Thanks Austin.

Austin
Posts: 12
Joined: Wed Jul 06, 2011 9:16 pm

Re: Shorten URL with Bitly

Postby Austin » Fri Jul 29, 2011 10:23 pm

No problem. I was really trying to illustrate that you can make Web API calls and manipulate the clipboard with the returned values. I hope this is an adequate example to show that concept. Web APIs could really extend the functionality of what you do. How many different Google APIs could you call to perform some action? Yahoo! has its APIs. The social media sites have APIs. The possibilities could be endless.

Vince
3D Clipboard Author
Posts: 22
Joined: Wed Jul 06, 2011 12:25 am

Re: Shorten URL with Bitly

Postby Vince » Thu May 03, 2012 6:17 pm

Here is an updated version of the Bitly Action that Austin created that utilizes the new UrlEncode functionality in 3D Clipboard 1.5. It also checks the response status code to determine if the request was successful. Enjoy!

Code: Select all

'Austin James
'7/29/2011

Const BITLY_HOSTNAME = "api.bitly.com"          ' Or api.j.mp
Const BITLY_USERNAME = "<YOUR_BITLY_USERNAME>"
Const BITLY_API_KEY = "<YOUR_BITLY_API_KEY>"
 
Dim bitlyURL
Dim objHttp
 
'Format URL to send to Bitly
bitlyURL = "http://" & BITLY_HOSTNAME & "/v3/shorten" & _
        "?login="   & Clipboard.UrlEncode(BITLY_USERNAME) & _
        "&apiKey="  & Clipboard.UrlEncode(BITLY_API_KEY) & _
        "&longUrl=" & Clipboard.UrlEncode(Clipboard.Value) & _
        "&format=txt"
 
'Send request to bitly
Set objHttp = CreateObject("Microsoft.XmlHttp")
objHttp.open "GET", bitlyURL, False
objHttp.send ""
 
'Were we successful?
If objHttp.status = 200 Then
        ' Place short URL on clipboard
        Clipboard.Value = objHttp.responseText
Else
        ' There was an error.  Display a message to the user.
        MsgBox("An error was returned from " & _
                BITLY_HOSTNAME & ":" & vbCrLf & vbCrLf & _
                "Status: "&objHttp.status & vbCrLf & _
                "Response Text: "&objHttp.responseText)
End If
 

Vince
3D Clipboard Author
Posts: 22
Joined: Wed Jul 06, 2011 12:25 am

Re: Shorten URL with Bitly

Postby Vince » Thu May 03, 2012 6:44 pm

Here's a JScript version of the same script for fun:

Code: Select all

// Idea by Austin James
// 7/29/2011
 
var BITLY_HOSTNAME = "api.bitly.com";           // Or api.j.mp
var BITLY_USERNAME = "<YOUR_BITLY_USERNAME>";
var BITLY_API_KEY = "<YOUR_BITLY_API_KEY>";
 
// Format URL to send to Bitly
var bitlyURL = "http://" + BITLY_HOSTNAME + "/v3/shorten" +
        "?login="   + Clipboard.UrlEncode(BITLY_USERNAME) +
        "&apiKey="  + Clipboard.UrlEncode(BITLY_API_KEY) +
        "&longUrl=" + Clipboard.UrlEncode(Clipboard.Value) +
        "&format=txt";
 
// Send request to bitly
var objHttp = new ActiveXObject("Microsoft.XmlHttp");
objHttp.open("GET", bitlyURL, false);
objHttp.send();
 
// Were we successful?
if (objHttp.status == 200) {
        // Place short URL on clipboard
        Clipboard.Value = objHttp.responseText;
} else {
        // There was an error.  Display a message to the user.
        new ActiveXObject("WScript.Shell").Popup("An error was returned from " +
                BITLY_HOSTNAME + ":\r\n\r\n" +
                "Status: " + objHttp.status + "\r\n" +
                "Response Text: " + objHttp.responseText,
                30, "Error", 48);
}

Austin
Posts: 12
Joined: Wed Jul 06, 2011 9:16 pm

Re: Shorten URL with Bitly

Postby Austin » Thu May 03, 2012 11:05 pm

So, why did you leave my name in the comments? I don't think I was the author of that code. :ugeek:

Vince
3D Clipboard Author
Posts: 22
Joined: Wed Jul 06, 2011 12:25 am

Re: Shorten URL with Bitly

Postby Vince » Fri May 04, 2012 3:59 pm

You wrote the important bits. I just ported it. :)


Return to “Actions”

 

 

cron