AutoHotkey Community

It is currently May 26th, 2012, 7:05 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 33 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: [Library] bit.ly API
PostPosted: May 26th, 2009, 4:14 am 
Offline

Joined: March 27th, 2009, 12:46 pm
Posts: 76
Location: Dublin, IE
NAME:
    bitlyAPI.ahk
VERSION:
    v0.1.3
DESCRIPTION
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.3TODO:
    [ ] 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 September 22nd, 2009, 10:45 pm, edited 11 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 26th, 2009, 11:18 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
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=yo3uxqwT ... re=related :wink:

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Last edited by SoLong&Thx4AllTheFish on May 28th, 2009, 3:38 pm, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 26th, 2009, 8:06 pm 
Offline

Joined: March 27th, 2009, 12:46 pm
Posts: 76
Location: Dublin, IE
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 29th, 2009, 1:20 am 
Offline

Joined: March 27th, 2009, 12:46 pm
Posts: 76
Location: Dublin, IE
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 29th, 2009, 7:50 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
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
}

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 29th, 2009, 12:46 pm 
Offline

Joined: March 27th, 2009, 12:46 pm
Posts: 76
Location: Dublin, IE
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 29th, 2009, 1:42 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Uhmmm where is download link or code? :?

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 29th, 2009, 2:09 pm 
Offline

Joined: March 27th, 2009, 12:46 pm
Posts: 76
Location: Dublin, IE
HugoV wrote:
Uhmmm where is download link or code? :?

Haha, it was a disappearing act. Thanks again.

_________________
My Scripts


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 29th, 2009, 2:31 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
It works for me!

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 30th, 2009, 11:54 am 
Offline

Joined: July 17th, 2008, 9:46 am
Posts: 225
Why should i use your (big) wrapper ? :)
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 30th, 2009, 12:37 pm 
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.:

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


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: May 30th, 2009, 1:46 pm 
Offline

Joined: July 17th, 2008, 9:46 am
Posts: 225
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!)
Image
Greets,
DHMH


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 30th, 2009, 10:42 pm 
Here are some more shorteners (the exclusions from tr.im):

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


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: May 31st, 2009, 8:24 pm 
Offline

Joined: March 27th, 2009, 12:46 pm
Posts: 76
Location: Dublin, IE
DHMH wrote:
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.

_________________
My Scripts


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 16th, 2009, 10:06 pm 
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


8)


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 33 posts ]  Go to page 1, 2, 3  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: infogulch, Scratch, Yahoo [Bot] and 10 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group