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 

UrlDownloadToVar
Goto page Previous  1, 2, 3
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Fri Dec 14, 2007 2:29 pm    Post subject: Reply with quote

Could i get the url behind a link with this? if not, is there such a script in the forums?

ie link diplayed as LINKTEXT (target="actual URL") ...then again that might need to follow javascript code and aspx playlist too..

im testing alot of ff extensions and greasemonkey scripts to find this but i need ahk anyway for the purpose of streaming with external app so if there is one in ahk that would be awesome
Back to top
bmcclure



Joined: 24 Nov 2007
Posts: 446

PostPosted: Mon Dec 17, 2007 6:40 am    Post subject: Reply with quote

Sean wrote:

Here is a demonstration of Asynchronous, i.e., Event/Callback, alternative.


Hey Sean, this looks nice, but I'm a little fuzzy on one thing: Where in here are the downloaded pages being stored to a variable? I'm trying to adapt this for my needs, and I essentially need to download a webpage into a variable so that I can run commands on it. It seems suited for this task, but I'm just having trouble grasping the COM calls I guess.
_________________
-Ben

SteamLab
SteamLab Wiki

[Broken] - My industrial music [on GarageBand]
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 1281

PostPosted: Mon Dec 17, 2007 7:21 am    Post subject: Reply with quote

bmcclure wrote:
Where in here are the downloaded pages being stored to a variable?

It didn't save the page to the variable, just output the content into DebugViewer. It was a demonstration. I modified it a little to save the content to a (global) variable _ResponseText_. One thing to note: as it's asynchronous, have to set up some mechanism to notify the new content of _ResponseText_, and I used here SetTimer and MsgBox for that.

Code:
#Persistent
sURL :=   "http://www.autohotkey.com/"

OnExit, CleanUp
COM_Init()
pwhr :=   COM_CreateObject("WinHttp.WinHttpRequest.5.1")
psink:=   ConnectIWinHttpRequestEvents(pwhr)
COM_Invoke(pwhr, "Open", "GET", sURL, "True")
COM_Invoke(pwhr, "Send")
Return
CleanUp:
COM_Unadvise(NumGet(psink+8), NumGet(psink+12))
COM_Release(NumGet(psink+8))
COM_Release(pwhr)
COM_Term()
ExitApp
ResponseText:
MsgBox, % _ResponseText_
Return

IWinHttpRequestEvents(this, nStatus = "", pType = "")
{
   Critical
   If   A_EventInfo = 0
      NumPut(this,pType+0)
;   Else If   A_EventInfo = 1
;   Else If   A_EventInfo = 2
   Else If   A_EventInfo = 3
      OutputDebug, % "[START]`t" . nStatus . ": " . COM_Ansi4Unicode(pType) . "`n`n"
;   Else If   A_EventInfo = 4
   Else If   A_EventInfo = 5
   {
      Global   _ResponseText_ := COM_Invoke(NumGet(this+4), "ResponseText")
      SetTimer, ResponseText, -1
   }
   Else If   A_EventInfo = 6
      OutputDebug, % "[ERROR]`t" . nStatus . ": " . COM_Ansi4Unicode(pType) . "`n`n"
   Return   0
}

ConnectIWinHttpRequestEvents(pwhr)
{
   Static   IWinHttpRequestEvents
   If Not   VarSetCapacity(IWinHttpRequestEvents)
   {
      VarSetCapacity(IWinHttpRequestEvents,28,0), nParams=3113213
      Loop,   Parse,   nParams
      NumPut(RegisterCallback("IWinHttpRequestEvents","",A_LoopField,A_Index-1),IWinHttpRequestEvents,4*(A_Index-1))
   }
   pconn:=COM_FindConnectionPoint(pwhr,IID_IWinHttpRequestEvents:="{F97F4E15-B787-4212-80D1-D380CBBF982E}")
   psink:=COM_CoTaskMemAlloc(16), NumPut(pwhr,NumPut(&IWinHttpRequestEvents,psink+0))
   NumPut(COM_Advise(pconn,psink),NumPut(pconn,psink+8))
   Return   psink
}
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 2492
Location: Australia, Qld

PostPosted: Mon Dec 17, 2007 12:24 pm    Post subject: Reply with quote

Guest wrote:
Could i get the url behind a link with this?

I recently wrote a demonstration of how to do the opposite - get the text of a link, given a partial URL. Below is a slightly updated version (the original used embedded C# rather than WinHttpRequest):
Code:
#NoEnv
COM_Init()
; Init now since it can be reused for multiple requests.
req := COM_CreateObject("WinHttp.WinHttpRequest.5.1")

LoginPage = http://www.autohotkey.com/forum/login.php
Redirect = privmsg.php?folder=inbox
Username =
Password =

Gui, Add, Edit, vLoginPage W300 R1, %LoginPage%
Gui, Add, Text,, Username:
Gui, Add, Edit, vUsername XM+100 YP-2 W200, %Username%
Gui, Add, Text, XM, Password:
Gui, Add, Edit, vPassword XM+100 YP-2 W200 Password, %Password%
Gui, Add, Button, gCheckPrivmsg Default, Check Private Messages

Gui, Show,, privmsg test
return


CheckPrivmsg:

Gui, Submit, NoHide
postData := "username=" uriEncode(UserName)
         . "&password=" uriEncode(Password)
         . "&autologin=0&redirect=" uriEncode(Redirect)
         . "&login=Log+in"

; POST form data to the login page.
COM_Invoke(req, "Open", "POST", LoginPage)
COM_Invoke(req, "SetRequestHeader", "Content-Type", "application/x-www-form-urlencoded")
COM_Invoke(req, "Send", postData)
; Retrieve HTML of the response page (%Redirect%).
html := COM_Invoke(req, "ResponseText")

; Create a HTMLDocument to parse the HTML.
doc := COM_CreateObject("{25336920-03F9-11CF-8FD0-00AA00686F13}")
COM_Invoke(doc, "write", html)
COM_Invoke(doc, "close")

; "Retrieves a collection of all 'a' objects that specify the HREF property and
;  all 'area' objects in the document."
doc_links := COM_Invoke(doc, "links")

Loop, % COM_Invoke(doc_links, "length")
{
    link := COM_Invoke(doc_links, "item", A_Index-1)

    if InStr(COM_Invoke(link, "href"), "privmsg.php?folder=inbox")
    {
        text := COM_Invoke(link, "innerText")
        COM_Release(link)
        break
    }

    COM_Release(link)
}

COM_Release(doc_links)
COM_Release(doc)

; <DEBUG>
; FileDelete, check privmsg debug.html
; FileAppend, %html%, check privmsg debug.html
; Run, check privmsg debug.html
; </DEBUG>

MsgBox %text%

return


GuiClose:
ExitApp


; Authors: Titan, Ageless
;   http://www.autohotkey.com/forum/topic18876.html
uriEncode(str) {
   f = %A_FormatInteger%
   SetFormat, Integer, Hex
   If RegExMatch(str, "^\w+:/{0,2}", pr)
      StringTrimLeft, str, str, StrLen(pr)
   StringReplace, str, str, `%, `%25, All
   Loop
      If RegExMatch(str, "i)[^\w\.~%/:]", char)
         StringReplace, str, str, %char%, % "%" . SubStr(Asc(char),3), All
      Else Break
   SetFormat, Integer, %f%
   Return, pr . str
}
Given the username and password of a registered autohotkey.com/forum user, it (simultaneously) logs into the forum and retrieves your PM Inbox (page). It then parses the HTML (via COM + MSHTML) to find the text of the PM link - i.e. "You have N new messages."

If you haven't already, you'll have to register before you can test the script. Wink Alternatively, you could try it on some other phpBB forum.

I used synchronous code (for brevity Wink), so be patient if the GUI appears to stop responding.
Back to top
View user's profile Send private message
Guest






PostPosted: Sat Apr 05, 2008 12:47 pm    Post subject: Reply with quote

to Lexikos: is there a way to use the WinHttpRequest method like curl and make it click on a button ?

Eg:
Code:
curl -Ld "BUTTON_INPUT=Restart%20Cable%20Modem" http://192.168.100.1/configdata.html
Back to top
Lexikos



Joined: 17 Oct 2006
Posts: 2492
Location: Australia, Qld

PostPosted: Sun Apr 06, 2008 12:36 am    Post subject: Reply with quote

You can't "click on a button" without a web browser and button to click on. You can submit a form, exactly how my previous post demonstrates... It would be slightly different if the form's method is "GET", in which case I think the form data would become part of the URL.
Back to top
View user's profile Send private message
RobOtter



Joined: 30 Jan 2005
Posts: 125
Location: Darmstadt, Germany

PostPosted: Tue Apr 15, 2008 9:51 am    Post subject: Reply with quote

I´m not quite happy with this ugly kind of workaround to get URL contents into a variable...
Although I´m a programmer myself, I´m not familiar with C++ but I suspect it couldn´t be too hard to implement a function "UrlDownloadToVar" in a very similar way to "UrlDownloadToFile". Imho copying the URL content into a variable instead of a file should be a very easy task for Chris (or some other C++ guru who can maintain the source code) - et voila, we´d have a very quick and tidy solution.

Another workaround (which I haven´t tested yet) would be to use a IE control which loads the URL and then get the contents from that control. I guess this would avoid messing around with certain DLLs and so, keeping cross-platform compatibility (x-pf in the sense of different Windows OS, of course Smile )
Has anyone done that yet and can confirm its feasibility?

Regards,
Rob
Back to top
View user's profile Send private message
Yure
Guest





PostPosted: Sun Apr 20, 2008 4:12 pm    Post subject: Reply with quote

I guess this line should not be here, I can't see why should we destroy the last two characters. Once I had removed this line the function worked just great for me.

Code:
StringTrimRight, res, res, 2
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2, 3
Page 3 of 3

 
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