Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[Library] bit.ly API


  • Please log in to reply
32 replies to this topic
Voltron43
  • Members
  • 76 posts
  • Last active: May 06 2011 07:48 PM
  • Joined: 27 Mar 2009
NAME:
bitlyAPI.ahkVERSION:
v0.1.3DESCRIPTION
A wrapper/library for the bit.ly API: http://bit.ly/apidocs

bit.ly allows users to shorten, share, and track links (URLs). Reducing the URL length makes sharing easier. bit.ly can be accessed through our website, bookmarklets and a robust and open API. bit.ly is also integrated into several popular third-party tools such as Tweetdeck.

FEATURES:
• Proxy Support
• Shorten long URLs
• Shorten multiple long URLs with one API call
• Store shortened URLs in user history
• Expand bit.ly URLs
• Get stats of bit.ly URLs
• Get a list of bit.ly API error codes
• Return specified nodes of fetched XML dataFUNCTIONS:
bitly_shorten() - Given a long url, returns a shorter one.
bitly_expand() - Given a long url, returns a shorter one.
bitly_info() - Given a bit.ly url or hash, return information about that page, such as the long source url, ...
bitly_stats() - Given a bit.ly url or hash, return traffic and referrer data.
bitly_errors() - Get a list of bit.ly API error codes.
bitly_recent() - Get a list of user bit.ly URLsREQUIREMENTS:
httpQuery

Note: httpQuery is included in the bitlyAPI.ahkDIRECTIONS:
1. Sign up for an account with bit.ly.
2. Grab the long API key from the account page and enter it in the bit.ly User Info section along with the API login
3. Optional - Enter in proxy user info if behind a proxy

Global Variables used for bit.ly account and/or proxy:
• login
• apiKey
• Proxy_UserName
• Proxy_Password
• Proxy_Address
• Proxy_Port

Note: Uses default login of bitlyapidemo if no login is specifiedEXAMPLES:
Example I: Get the bit.ly short URL and save it to the user history.
#Include bitlyAPI.ahk

login := "bitlyapidemo"
apiKey := "R_0da49e0a9118ff35f52f629d2d71bf07"

val := bitly_shorten("http://cnn.com",1)
MsgBox, % val
Example II: Get the long URL from a bit.ly URL.
#Include bitlyAPI.ahk

login := "bitlyapidemo"
apiKey := "R_0da49e0a9118ff35f52f629d2d71bf07"

val := bitly_expand("http://bit.ly/BEMuf")
MsgBox, % val
Example III: Get the small thumbnail url from the bit.ly URL
#Include bitlyAPI.ahk

login := "bitlyapidemo"
apiKey := "R_0da49e0a9118ff35f52f629d2d71bf07"

val := bitly_info("http://bit.ly/BEMuf","thumbnail/small")
MsgBox, % val
Example IV: Get the list of referrers of the bit.ly URL
#Include bitlyAPI.ahk

login := "bitlyapidemo"
apiKey := "R_0da49e0a9118ff35f52f629d2d71bf07"

val := bitly_stats("http://bit.ly/BEMuf","referrers")
MsgBox, % val
Example V: Get the list of API errorsMessages.
#Include bitlyAPI.ahk

login := "bitlyapidemo"
apiKey := "R_0da49e0a9118ff35f52f629d2d71bf07"

val := bitly_errors("errorMessage")
MsgBox, % val

Example VI: Get the entire recent.rss XML data for the user bitlyapidemo.
#Include bitlyAPI.ahk

login := "bitlyapidemo"
apiKey := "R_0da49e0a9118ff35f52f629d2d71bf07"

val := bitly_recent("bitlyapidemo")
MsgBox, % val
Note: bitly_recent() does not return the list of referrers parsed.DOCUMENTATION
• <!-- m -->https://ahknet.autoh...ron43/#bitlyAPI<!-- m -->bitlyAPI v0.1.3DOWNLOADTODO:
[ ] Error checks[ ] j.mp support
[ ] support for links without <!-- m -->http://www<!-- m -->. prefix?
CHANGELOG:
v0.1.3 - 9/29/09
• Handle special character in bitly_shorten() - #
v0.1.2 - 6/21/09
• Added keyword function to bitly_shorten() function
• Added getURL() support funtion
• Replace "&" with "&" in returned bitly_expand() url
v0.1.1 - 5/29/09
• Handle special characters in bitly_shorten() URL - &?+


SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
Interesting, thanks!

Although not your fault, but perhaps you could "fix it"*, it fails with URLs with an ampersand (&) example:

http://www.google.com/search?hl=en&q=autohotkey+scripts&btnG=Google+Search&aq=0&oq=autohotkey
becomes
<!-- m -->http://bit.ly/5DnbC<!-- m -->
but it is simply google.com and not the full url


The problem that needs to be "fixed" is in httpQuery I think but haven't
look at it too closely.

* see <!-- m --> ... re=related<!-- m --> :wink:

Voltron43
  • Members
  • 76 posts
  • Last active: May 06 2011 07:48 PM
  • Joined: 27 Mar 2009

The problem that needs to be "fixed" is in httpQuery I think but haven't look at it too closely.

I may be able to fix it by sending the ampersand separated data via POST instead of GET methods. I'll take a look.

Thanks for the feedback!

Voltron43
  • Members
  • 76 posts
  • Last active: May 06 2011 07:48 PM
  • Joined: 27 Mar 2009

Although not your fault, but perhaps you could "fix it"*, it fails with URLs with an ampersand (&)

The special characters in the URL need to be converted to ASCII hex. Example: ? = %3F, & = %26, etc.

I found that your original example works if you use the following URL:
URL := "http://www.google.com/search%3Fhl%3Den%26q%3Dautohotkey%2Bscripts%26btnG%3DGoogle%2BSearch%26aq%3D0%26oq%3Dautohotkey"

It looks like i'm going to have to find a special character to ASCII Hex converter in the forums!

Thanks again for the feedback.

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
This might get you started:
url=http://www.google.com/search?hl=en&q=autohotkey+scripts&btnG=Google+Search&aq=0&oq=autohotkey 



ascii=?&+= ; strings to convert to hex



Loop, Parse, ascii

	{

	replace:=String2Hex(A_LoopField)

	StringReplace, url, url, %A_LoopField%, `%%Replace%, All

	}

MsgBox % Url	

	

; Reference http://www.autohotkey.com/forum/topic4934-15.html#158672 by Laszlo



String2Hex(x) ; Convert a string to hex digits (modified to accommodate new line chars)

{

  prevFmt = %A_FormatInteger%

  SetFormat Integer, H ;this function requires hex format

   Loop Parse, x

      hex .= 0x100+Asc(A_LoopField)

   StringReplace hex, hex, 0x1,,All

   SetFormat Integer, %prevFmt% ;restore original integer formatting

   Return hex

}


Voltron43
  • Members
  • 76 posts
  • Last active: May 06 2011 07:48 PM
  • Joined: 27 Mar 2009

This might get you started:

url=http://www.google.com/search?hl=en&q=autohotkey+scripts&btnG=Google+Search&aq=0&oq=autohotkey 

ascii=?&+= ; strings to convert to hex

Loop, Parse, ascii
	{
	replace:=String2Hex(A_LoopField)
	StringReplace, url, url, %A_LoopField%, `%%Replace%, All
	}
MsgBox % Url	
	
; Reference http://www.autohotkey.com/forum/topic4934-15.html#158672 by Laszlo

String2Hex(x) ; Convert a string to hex digits (modified to accommodate new line chars)
{
  prevFmt = %A_FormatInteger%
  SetFormat Integer, H ;this function requires hex format
   Loop Parse, x
      hex .= 0x100+Asc(A_LoopField)
   StringReplace hex, hex, 0x1,,All
   SetFormat Integer, %prevFmt% ;restore original integer formatting
   Return hex
}


Thanks again. v0.1.1 has been posted.

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
Uhmmm where is download link or code? :?

Voltron43
  • Members
  • 76 posts
  • Last active: May 06 2011 07:48 PM
  • Joined: 27 Mar 2009

Uhmmm where is download link or code? :?

Haha, it was a disappearing act. Thanks again.
DOWNLOAD

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
It works for me!

DHMH
  • Members
  • 230 posts
  • Last active: Mar 25 2012 10:34 AM
  • Joined: 17 Jul 2008
Why should i use your (big) wrapper ? :)
UseApi("http://api.bit.ly/shorten?version=2.0.1&longUrl=" . url "&login=bitlyapidemo&apiKey=MyKey&format=text")

UseApi(api){
	global
	UrlDownloadToFile,%api%,%a_temp%\encrypted.dat
	fileread,su,%a_temp%\encrypted.dat
	FileDelete,%A_temp%\encrypted.dat
	return su
}
???
Greets,
DHMH

n-l-i-d
  • Guests
  • Last active:
  • Joined: --
Nice! 8)

Here are some more url-shortening sites I encountered, simply by looking at the twitter stream for a moment.

I listed them with info, some need login, some have an api, some offer stats, some need auth- or appkeys, etcet.:

url = http://cli.gs/
stats = yes

login = yes
api = yes
apiurl = http://blog.cli.gs/api
authkey = some
appkey = some

---

url = http://digg.com/
stats = yes

login = yes
api = yes
apiurl = http://apidoc.digg.com/ShortURLs
authkey = n/a
appkey = yes

---

url = http://is.gd/
stats = no

login = no
api = no
apiurl = n/a
authkey = n/a
appkey = n/a

---

url = http://lin.cr/
stats = no

login = no
api = no
apiurl = n/a
authkey = n/a
appkey = n/a

---

url = http://migre.me/
stats = no

login = no
api = yes
apiurl = http://migre.me/api-migreme/
authkey = no
appkey = no

---

url = http://ow.ly/url/shorten-url
stats = no

login = no
api = not yet
apiurl = n/a
authkey = n/a
appkey = n/a

---

url = http://ping.fm/
stats = yes

login = yes
api = yes
apiurl = http://groups.google...gfm-developers/
authkey = yes
appkey = yes

---

url = http://tiny.cc/
stats = yes

login = no
api = no
apiurl = n/a
authkey = n/a
appkey = n/a

---

url = http://tinyurl.com/
stats = no

login = no
api = no
apiurl = n/a
authkey = n/a
appkey = n/a

---

url = http://tr.im/
stats = yes

login = no
api = yes
apiurl = http://tr.im/website/api
authkey = yes
appkey = n/a

---

url = http://tweetburner.com/)
stats = no

login = no
api = yes
apiurl = http://tweetburner.com/api
authkey = no
appkey = no

---

url = http://u2s.me/
stats = no

login = no
api = no
apiurl = n/a
authkey = n/a
appkey = n/a

---

url = http://u.nu/
stats = no

login = no
api = yes
apiurl = http://u.nu/unu-api
authkey = no
appkey = no


HTH

DHMH
  • Members
  • 230 posts
  • Last active: Mar 25 2012 10:34 AM
  • Joined: 17 Jul 2008
I wrote a long time ago this little script:
#NoTrayIcon
#SingleInstance, Force
Gui +ToolWindow -Owner +E0x40000
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Gui,Add,GroupBox,x5 y5 w355 h50,URL:

Gui,Add,Edit,x13 y20 w345 vurl
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Gui,Add,GroupBox,x5 y65 w355 h50,Your short URL:

Gui,Add,Edit,w345 x13 y80 ReadOnly vShort
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Gui,Font,,Tahoma
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Gui,Add,GroupBox,y+20 w355 h55 x8,Service:

Gui,Add,Radio,x10 y138 vtinyurl Checked,TinyUrl.com
Gui,Add,Radio,x+3 vtrim,tr.im
Gui,Add,Radio,x+3 varmin,arm.in
Gui,Add,Radio,x+3 vi2h,i2h.de
Gui,Add,Radio,x+3 vshorten,shorten.ws
Gui,Add,Radio,x+3 visgd,is.gd
Gui,Add,Radio,x10 y+5 vShortUrlCom,ShortUrl.com
Gui,Add,Radio,x+1 vTubeURL,TubeURL.com
Gui,Add,Radio,x+1 vxr,xr.com
Gui,Add,Radio,x+1 vbitly,bit.ly
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Gui,Add,Checkbox, x10 y+10 vAuto,Create a copy in clipboard
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Gui,Add,Button,x115 y+5 gGo w60 Default,Create
Gui,Add,Button,x+4 gReset w60,Reset
Gui,Add,Button,x+4 gClipBoard w60,Clipboard

Gui,Add,Text,y200 x10 disabled, © www.xptricks.de

hCurs:=DllCall("LoadCursor","UInt",NULL,"Int",32649,"UInt") ;IDC_HAND
OnMessage(0x200,"WM_MOUSEMOVE") 


Gui,Show,w370 h220,ShortUrlMaker 1.2 English Edition
Return

~LButton::
MouseGetPos,,,,ctrl
IfWinNotActive,ShortUrlMaker 1.2 English Edition
    Return
  If ctrl in Static1
    run,www.xptricks.de
Return
	
Reset:
GuiControl,,Short,
GuiControl,,Url,
Return

WM_MOUSEMOVE(wParam,lParam)
{
  Global hCurs
  MouseGetPos,,,,ctrl
  If ctrl in Static1
    DllCall("SetCursor","UInt",hCurs)
  Return
}

ClipBoard:
GuiControlGet,clipboard,,Short
MsgBox,64,Info,Copied into clipboard! `;)
Return

GuiClose:
ExitApp

go:
Gui,Submit,NoHide
if url = 
	{
    MsgBox,16,Error!,Please enter an URL!
	Return
	}
if (i2h){
	chars := SubStr(url,1,4)
	If chars != http
	    url = http://%url%
	UseApi("http://api.i2h.de/v1/?url=" . url)
	GuiControl,,Short,%su%
	Return
}
if (armin){
	UseApi("http://arm.in/arminize/" . url)
	StringTrimLeft,su,su,78
	StringGetPos,pos,su,</arminized_url>
	StringLeft,su,su,%pos%
	GuiControl,,Short,%su%
	Return
}
if (trim){
	UseApi("http://tr.im/api/trim_url.xml?url=" . url)
	StringTrimLeft,su,su,115
	StringGetPos,pos,su,</url>
	StringLeft,su,su,%pos%
	GuiControl,,Short,%su%
	Return
}
if (tinyurl){
	chars := SubStr(url,1,4)
	If chars != http
	    url = http://%url%
	UseApi("http://tinyurl.com/api-create.php?url=" . url)
	GuiControl,,Short,%su%
	Return
}
if (shorten){
	chars := SubStr(url,1,4)
	If chars != http
	    url = http://%url%
	UseApi("http://shorten.ws/?module=ShortURL&file=Add&mode=API&url=" . url)
	GuiControl,,Short,%su%
	Return
}
if (isgd){
	UseApi("http://is.gd/api.php?longurl=" . url)
	GuiControl,,Short,%su%
}
if (ShortUrlCom){
	UseApi("http://shorturl.com/make_shorturl.php?longurl=" . url)
	StringGetPos,pos,su,<font size=1>[<a href=
	StringTrimLeft,su,su,%pos%
	StringGetPos,pos,su,%A_Space% target
	StringLeft,su,su,%pos%
	StringReplace,su,su,<font size=1>[<a href=,
	GuiControl,,Short,%su%
}
if (TubeURL){
	UseApi("http://tubeurl.com/?action=generate_url&url=" . url)
	StringGetPos,pos,su,</strong>: <a href="
	StringTrimLeft,su,su,%pos%
	StringGetPos,pos,su,target="_blank"
	StringLeft,su,su,%pos%
	StringReplace,su,su,</strong>: <a href=",
	StringReplace,su,su,"%A_Space%
	GuiControl,,Short,%su%
}
if (xr){
	Settimer,changebtn,50
	MsgBox,35,Custom Tags,Do you wish to insert a custom Tag?
	IfMsgBox Cancel
	    Return
	Else IfMsgBox No
	{
	UseApi("http://api.xr.com/api?link=" . url)
	StringSplit,link,su,`n
	GuiControl,,Short,%link2%
	}
	Else
		{
		InputBox,customlink,Custom Tag,Please enter a name for your custom Tag!
		if customlink = 
		    Return
		UseApi("http://api.xr.com/api?link=" . url "&custom=" . customlink )
		GuiControl,,Short,%su%
		}
	}
if (bitly){
	UseApi("http://api.bit.ly/shorten?version=2.0.1&longUrl=" . url "&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&format=text")
	GuiControl,,Short,%su%
	}
if (Auto){
    GuiControlGet,clipboard,,Short
	}
Return


UseApi(api){
	global
	UrlDownloadToFile,%api%,%a_temp%\encrypted.dat
	fileread,su,%a_temp%\encrypted.dat
	FileDelete,%A_temp%\encrypted.dat
	return su
}

changebtn:
IfWinNotExist, Custom Tags
    return  ; Keep waiting.
settimer,changebtn,off
ControlSetText, Button1, &Own Link
ControlSetText, Button2, &Random Link
Return
(English Edition, especially for you!)
Posted Image
Greets,
DHMH

n-l-i-d
  • Guests
  • Last active:
  • Joined: --
Here are some more shorteners (the exclusions from tr.im):

http://api.tr.im/api/shorteners.xml

Voltron43
  • Members
  • 76 posts
  • Last active: May 06 2011 07:48 PM
  • Joined: 27 Mar 2009

Why should i use your (big) wrapper ? :)

The point of the API wrapper is to use bitly's entire API and return any node in the returned XML. Also, httpQuery is better than urldownloadtofile because httpQuery allows for proxy support and the data does not have to be written to a file, and then read back. The file is large also because of the function documentation and comments. The file also is able to handle special characters (such as +?&) in the longURLs given to bitly.

n-l-i-d
  • Guests
  • Last active:
  • Joined: --
Could you add the "Optional Custom Name" option for when creating a short url?

For those looking to shorten to a custom keyword, the parameter is: keyword. Set the value to the keyword you desire


8)