AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[Library] bit.ly API
Goto page 1, 2  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Voltron43



Joined: 27 Mar 2009
Posts: 76
Location: Dublin, IE

PostPosted: Tue May 26, 2009 4:14 am    Post subject: [Library] bit.ly API Reply with quote

NAME:
    bitlyAPI.ahk

VERSION:
    v0.1.3

DESCRIPTION

http://bit.ly/pages/about/ wrote:
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 data

FUNCTIONS:
    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 URLs

REQUIREMENTS:
    httpQuery

    Note: httpQuery is included in the bitlyAPI.ahk

DIRECTIONS:
    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 specified

EXAMPLES:
    Example I: Get the bit.ly short URL and save it to the user history.
Code:
#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.
Code:
#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
Code:
#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
Code:
#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.
Code:
#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.
Code:
#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

bitlyAPI v0.1.3
TODO:
    [ ] Error checks

[ ] j.mp support
[ ] support for links without http://www. 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 - &?+

_________________
My Scripts


Last edited by Voltron43 on Tue Sep 22, 2009 10:45 pm; edited 11 times in total
Back to top
View user's profile Send private message Visit poster's website
hugov



Joined: 27 May 2007
Posts: 3250

PostPosted: Tue May 26, 2009 11:18 am    Post subject: Reply with quote

Interesting, thanks!

Although not your fault, but perhaps you could "fix it"*, it fails with URLs with an ampersand (&) example:
Quote:
http://www.google.com/search?hl=en&q=autohotkey+scripts&btnG=Google+Search&aq=0&oq=autohotkey
becomes
http://bit.ly/5DnbC
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 http://www.youtube.com/watch?v=yo3uxqwTxk0&feature=related Wink
_________________
Tut 4 Newbies
TF : Text file & string lib, TF Forum


Last edited by hugov on Thu May 28, 2009 3:38 pm; edited 2 times in total
Back to top
View user's profile Send private message Visit poster's website
Voltron43



Joined: 27 Mar 2009
Posts: 76
Location: Dublin, IE

PostPosted: Tue May 26, 2009 8:06 pm    Post subject: Reply with quote

HugoV wrote:
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!
_________________
My Scripts
Back to top
View user's profile Send private message Visit poster's website
Voltron43



Joined: 27 Mar 2009
Posts: 76
Location: Dublin, IE

PostPosted: Fri May 29, 2009 1:20 am    Post subject: Reply with quote

HugoV wrote:
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:
Code:
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.
_________________
My Scripts
Back to top
View user's profile Send private message Visit poster's website
hugov



Joined: 27 May 2007
Posts: 3250

PostPosted: Fri May 29, 2009 7:50 am    Post subject: Reply with quote

This might get you started:
Code:
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
}

_________________
Tut 4 Newbies
TF : Text file & string lib, TF Forum
Back to top
View user's profile Send private message Visit poster's website
Voltron43



Joined: 27 Mar 2009
Posts: 76
Location: Dublin, IE

PostPosted: Fri May 29, 2009 12:46 pm    Post subject: Reply with quote

HugoV wrote:
This might get you started:
Code:
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.
_________________
My Scripts
Back to top
View user's profile Send private message Visit poster's website
hugov



Joined: 27 May 2007
Posts: 3250

PostPosted: Fri May 29, 2009 1:42 pm    Post subject: Reply with quote

Uhmmm where is download link or code? Confused
_________________
Tut 4 Newbies
TF : Text file & string lib, TF Forum
Back to top
View user's profile Send private message Visit poster's website
Voltron43



Joined: 27 Mar 2009
Posts: 76
Location: Dublin, IE

PostPosted: Fri May 29, 2009 2:09 pm    Post subject: Reply with quote

HugoV wrote:
Uhmmm where is download link or code? Confused

Haha, it was a disappearing act. Thanks again.

_________________
My Scripts
Back to top
View user's profile Send private message Visit poster's website
hugov



Joined: 27 May 2007
Posts: 3250

PostPosted: Fri May 29, 2009 2:31 pm    Post subject: Reply with quote

It works for me!
_________________
Tut 4 Newbies
TF : Text file & string lib, TF Forum
Back to top
View user's profile Send private message Visit poster's website
DHMH



Joined: 17 Jul 2008
Posts: 209

PostPosted: Sat May 30, 2009 11:54 am    Post subject: Reply with quote

Why should i use your (big) wrapper ? Smile
Code:
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
_________________
Tips & Tricks for Windows XP and Vista... only german! sorry!
German Computerboard
Back to top
View user's profile Send private message Visit poster's website
n-l-i-d
Guest





PostPosted: Sat May 30, 2009 12:37 pm    Post subject: Reply with quote

Nice! Cool

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.:

Quote:
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.com/group/pingfm-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://twurl.nl/ (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
Back to top
DHMH



Joined: 17 Jul 2008
Posts: 209

PostPosted: Sat May 30, 2009 1:46 pm    Post subject: Reply with quote

I wrote a long time ago this little script:
Code:
#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!)

Greets,
DHMH
_________________
Tips & Tricks for Windows XP and Vista... only german! sorry!
German Computerboard
Back to top
View user's profile Send private message Visit poster's website
n-l-i-d
Guest





PostPosted: Sat May 30, 2009 10:42 pm    Post subject: Reply with quote

Here are some more shorteners (the exclusions from tr.im):

http://api.tr.im/api/shorteners.xml
Back to top
Voltron43



Joined: 27 Mar 2009
Posts: 76
Location: Dublin, IE

PostPosted: Sun May 31, 2009 8:24 pm    Post subject: Reply with quote

DHMH wrote:
Why should i use your (big) wrapper ? Smile

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.
_________________
My Scripts
Back to top
View user's profile Send private message Visit poster's website
n-l-i-d
Guest





PostPosted: Tue Jun 16, 2009 10:06 pm    Post subject: Reply with quote

Could you add the "Optional Custom Name" option for when creating a short url?

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


Cool
Back to top
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group