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 

Upload to TwitPic with httpQuery

 
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: Fri Aug 21, 2009 12:37 pm    Post subject: Upload to TwitPic with httpQuery Reply with quote

Code:
;===============================================================================
;
; TITLE:        TwitPicUpload.ahk
; VERSION:      v0.1.0
; LINK:         http://www.autohotkey.com/forum/viewtopic.php?p=290238#290238
;
; AUTHOR:       Voltron43
; DATE:         08/21/2009
; HOMEPAGE:     http://www.autohotkey.net/~Voltron43/
;
; ------------------------------------------------------------------------------
;
; DESCRIPTION:  Upload pics to TwitPic.
;
;               Inspired by example IV from httpQuery
;               http://www.autohotkey.com/forum/topic33506.html
;
; ------------------------------------------------------------------------------
;
; SYNTAX:       TwitPicUpload([filepath, TwitMessage])
;
; ------------------------------------------------------------------------------

#include httpQuery.ahk

sTw_Username := ""
sTw_Password := ""

result := TwitPicUpload()

Gui,Add,Edit,w400 h400, % result
Gui,Show
   
Return

GuiClose:
GuiEscape:
   ExitApp

TwitPicUpload(image="",sMessage="") {
    Global sTw_Username,sTw_Password
    If (!image)
        FileSelectFile, image, %A_ScriptDir%
    FileGetSize,size,%image%
    SplitPath,image,OFN
    FileRead,img,%image%
    VarSetCapacity(placeholder,size,32)
    boundary := makeProperBoundary()
    post := "--" boundary "`nContent-disposition: form-data; name=""username"";`n`n"
        . sTw_Username . "`n--" . boundary . "`nContent-disposition: form-data; name=""password"";`n`n"
        . sTw_Password . "`n--" . boundary . "`nContent-disposition: form-data; name=""message"";`n`n"
        . sMessage . "`n--" . boundary . "`nContent-disposition: form-data; name=""media""; filename="""
        . ofn """`nContent-type: " MimeType(img) "`nContent-Transfer-Encoding: binary`n`n"
        . placeholder "`n--" boundary "--"
    headers:="Content-type: multipart/form-data, boundary=" boundary "`nContent-Length: " strlen(post)
    DllCall("RtlMoveMemory","uInt",(offset:=&post+strlen(post)-strlen(Boundary)-size-5)
         ,"uInt",&img,"uInt",size)
    sURL := (!sMessage) ? "http://twitpic.com/api/upload" : "http://twitpic.com/api/uploadAndPost"
    size := httpQuery(result:="",sURL,post,headers)
    VarSetCapacity(result,-1)
    Return, result
}

makeProperBoundary(){
   Loop,26
      n .= chr(64+a_index)
   n .= "0123456789"
   Loop,% StrLen(A_Now) {
      Random,rnd,1,% StrLen(n)
      Random,UL,0,1
      b .= RegExReplace(SubStr(n,rnd,1),".$","$" (round(UL)? "U":"L") "0")
   }
   Return b
}

MimeType(ByRef Binary) {
   MimeTypes:="424d image/bmp|4749463 image/gif|ffd8ffe image/jpeg|89504e4 image/png|4657530"
          . " application/x-shockwave-flash|49492a0 image/tiff"
   @:="0123456789abcdef"
   Loop,8
      hex .= substr(@,(*(a:=&Binary-1+a_index)>>4)+1,1) substr(@,((*a)&15)+1,1)
   Loop,Parse,MimeTypes,|
      if ((substr(hex,1,strlen(n:=RegExReplace(A_Loopfield,"\s.*"))))=n)
         Mime := RegExReplace(A_LoopField,".*?\s")
   Return (Mime!="") ? Mime : "application/octet-stream"
}

_________________
My Scripts
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
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