Dim gsLoginURL, gsLoginPostData, gsLoginGetPost, gsLoginReferer, gsLoginCookies, gsLoginContentType Dim gsDestinationURL, gsDestPostData, gsDestGetPost, gsDestReferer, gsDestContentType Dim giAffID, gsProgIDs, gsCatIDs, gsKWDs Dim gsUserAgent Dim sLoginURL, sLoginPostData, sCookieData, sDebug Dim sDestinationURL, sDestPostData, sOutDir, sOutFile, sOutFull Dim oCmdArguments Dim sUserN, sPassW, sAffID, sProgIDs, sCatIDs, sKWDs Dim bError, sErrMsg, sOWrite Dim ofso, gbDebug, giTimeOutTime Const READYSTATE_UNINITIALIZED = 0, _ READYSTATE_INITIALIZED = 1, _ READYSTATE_LOADED = 2, _ READYSTATE_INTERACTIVE = 3, _ READYSTATE_COMPLETE = 4 Const WinHttpRequestOption_UserAgentString = 0, _ WinHttpRequestOption_EnableRedirects = 6 '----------------------------------------------------------------------- 'AUTHENTICATION '----------------------------------------------------------------------- 'Use Place Holders: %USER% for Username and %PASSW% for Password either in Login URL or PostData 'Use Place Holders: %AFFID% for Affiliate ID, %PROGIDS% for Program IDs ' %CATIDS% for Categories and %KWDS% for Keywords gsLoginURL = "http://www.pepperjamnetwork.com/login.php" gsLoginPostData = "email=%USER%&passwd=%PASSW%" gsLoginGetPost = "POST" '"POST or "GET" gsLoginReferer = "http://www.pepperjamnetwork.com/" gsLoginCookies = "" gsLoginContentType = "application/x-www-form-urlencoded" gsDestinationURL = "http://feeds.pepperjamnetwork.com/product-catalog/download/?affiliate_id=%AFFID%&program_ids=%PROGIDS%&categories=%CATIDS%&keywords=%KWDS%" gsDestPostData = "" gsDestGetPost = "GET" gsDestReferer = "http://www.pepperjamnetwork.com/affiliate/store_front_beta.php" gsDestContentType = "text/html; charset=utf-8" gsUserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1;)" 'gsUserAgent = "Mozilla/5.001 (windows; U; NT4.0; en-us) Gecko/25250101" gsOutputFormat = "B" 'B = Binary, A = ASCII gbDebug = true 'Debugging on/off giTimeOutTime = 300 Call Main() '--------------------------------------------------------------------------- ' M A I N P R O G R A M '--------------------------------------------------------------------------- Public Sub Main() Dim iResults bError = false sErrMsg = "" sCookieData = "" sDebug = "" Set oCmdArguments = WScript.Arguments Set ofso = CreateObject("Scripting.FileSystemObject") iResults = InputValidation() if iResults <> 0 then call ShowHelpMessage() end if Call PrepareParams() Call LoginHTTPRequest() if bError = true then Call ShowErrorMessage() end if Call DestHTTPRequest(sDestinationURL, sOutFull) if bError = true then Call ShowErrorMessage() end if Set ofso = Nothing End Sub ' --------------------------------------------------------------------- '* @info Generate a folder tree from the path '* '* @param (String) Path '* @return (Boolean) Folder Exists: Recursion continues (Y/N) ' --------------------------------------------------------------------- Public Function GeneratePath(pFolderPath) Set objGenPathFSO = CreateObject("Scripting.FileSystemObject") GeneratePath = False If Not objGenPathFSO.FolderExists(pFolderPath) Then If GeneratePath(objGenPathFSO.GetParentFolderName(pFolderPath)) Then GeneratePath = True Call objGenPathFSO.CreateFolder(pFolderPath) End If Else GeneratePath = True End If Set objGenPathFSO = Nothing End Function '------------------------------------------------------------------------- ' Validation of Input Parameters '------------------------------------------------------------------------- Public function InputValidation() Dim bFailed, b bFailed = 0 if NOT oCmdArguments.Named.Exists("u") or _ NOT oCmdArguments.Named.Exists("p") or _ NOT oCmdArguments.Named.Exists("a") or _ NOT oCmdArguments.Named.Exists("out") then bFailed = 1 sErrMsg = sErrMsg & "One or more of the Required Parameters were not provided!" & vbcrlf end if if bFailed = 0 then sOutFile=oCmdArguments.Named.Item("out") sOutDir=oCmdArguments.Named.Item("outdir") if instr(sOutFile,"\") > 0 or instr(sOutFile,"/") > 0 then bFailed = 1 sErrMsg = sErrMsg & "Output File Name is not allowed to contain Path!" & vbcrlf end if end if if bFailed = 0 then sUserN=oCmdArguments.Named.Item("u") sPassW=oCmdArguments.Named.Item("p") sAffID=oCmdArguments.Named.Item("a") sProgIDs=oCmdArguments.Named.Item("progs") sCatIDs=oCmdArguments.Named.Item("cats") sKWDs=oCmdArguments.Named.Item("kw") if oCmdArguments.Named.Exists("timeout") and isNumeric(oCmdArguments.Named.Item("timeout")) then giTimeOutTime = abs(int(oCmdArguments.Named.Item("timeout"))) end if sOWrite=Ucase(oCmdArguments.Named.Item("over")) if sOutDir = "" then sOutFull = ofso.GetAbsolutePathName(sOutFile) sOutDir = left(sOutFull,len(sOutFull)-Len(sOutFile)-1) else if right(sOutDir,1) = "\" then sOutDir = left(sOutDir,len(sOutDir)-1) end if sOutFull = sOutDir & "\" & sOutFile end if b = GeneratePath(sOutDir) if sOWrite = "N" then if ofso.FileExists(sOutFull) then sErrMsg = sErrMsg & "Operation Aborted!" & vbcrlf sErrMsg = sErrMsg & "Automatic Output File Overwrite disabled with /over:N option." & vbcrlf sErrMsg = sErrMsg & "Target Output File " & sOutFull & " already exists" & vbcrlf WScript.Echo sErrMsg WScript.Quit 1 end if else sOWrite = "Y" end if end if 'oCmdArguments.Unnamed.Count 'oCmdArguments.Named.Item("u") InputValidation = bFailed End Function '-------------------------------------------------------------------------- ' Show Help Screen '-------------------------------------------------------------------------- Public Sub ShowHelpMessage() sErrMsg = sErrMsg & "PepperJam Affiliate Network - Automatic Product Data Feed Download Script, Version 0.9b" & vbcrlf sErrMsg = sErrMsg & "written by Carsten Cumbrowski (2009), http://www.cumbrowski.com/" & vbcrlf sErrMsg = sErrMsg & "" & vbcrlf sErrMsg = sErrMsg & "usage: " & Wscript.ScriptName & " /u:EMAIL /p:PW /a:AFFID /out:OUTFILE.CSV [/progs:PRGIDS /cats:CATIDS /kw:KWDS /outdir:DRIVE:\PATH /over:N /:timeout:NNN]" & vbcrlf sErrMsg = sErrMsg & "" & vbcrlf sErrMsg = sErrMsg & "Required Command Line Parameters" & vbcrlf sErrMsg = sErrMsg & "/u: Your PPJN Username/Email" & vbcrlf sErrMsg = sErrMsg & "/p: Your PPJN Account Password" & vbcrlf sErrMsg = sErrMsg & "/a: Your Numeric Affiliate ID" & vbcrlf sErrMsg = sErrMsg & "/out: Output File Name (only file name, not Path)" & vbcrlf sErrMsg = sErrMsg & "" & vbcrlf sErrMsg = sErrMsg & "Optional Parameters" & vbcrlf sErrMsg = sErrMsg & "/progs: Program IDs, separate multiple programs with '-', default = all active programs" & vbcrlf sErrMsg = sErrMsg & "/cats: Category IDs, separate multiple categories with '-' " & vbcrlf sErrMsg = sErrMsg & "/kw: Keywords, separate multiple keywords with '+'" & vbcrlf sErrMsg = sErrMsg & "/outdir: Output Directory (Default = current directory)" & vbcrlf sErrMsg = sErrMsg & "/over:N Do Not Overwrite Existing Output File (Default = overwrite)" & vbcrlf sErrMsg = sErrMsg & "/timeout:NNN Timeout in seconds for Download Request (default = 300 (5 min))" & vbcrlf sErrMsg = sErrMsg & "" & vbcrlf sErrMsg = sErrMsg & "Example:" & vbcrlf sErrMsg = sErrMsg & Wscript.ScriptName & " /u:john.doe@hotmail.com /p:mypassword /a:987 /out:ppjnfeed.csv" & vbcrlf sErrMsg = sErrMsg & "" & vbcrlf WScript.Echo sErrMsg WScript.Quit 1 End Sub '-------------------------------------------------------------------------- ' Show Error Message '-------------------------------------------------------------------------- Public Sub ShowErrorMessage() WScript.Echo sErrMsg WScript.Quit 1 End Sub '-------------------------------------------------------------------------- ' Prepare Input Parameters '-------------------------------------------------------------------------- Public Sub PrepareParams() sLoginURL = gsLoginURL sLoginPostData = gsLoginPostData sDestinationURL = gsDestinationURL sDestPostData = gsDestPostData sLoginURL = replace(sLoginURL, "%USER%", sUserN, 1, -1 ,1) sLoginURL = replace(sLoginURL, "%PASSW%", sPassW, 1, -1 ,1) sLoginPostData = replace(sLoginPostData, "%USER%", sUserN, 1, -1 ,1) sLoginPostData = replace(sLoginPostData, "%PASSW%", sPassW, 1, -1 ,1) sDestinationURL = replace(sDestinationURL, "%AFFID%", sAffID, 1, -1 ,1) if sProgIDs <> "" then sDestinationURL = replace(sDestinationURL, "%PROGIDS%", sProgIDs, 1, -1 ,1) else sDestinationURL = replace(sDestinationURL, "%PROGIDS%", "all", 1, -1 ,1) end if sDestinationURL = replace(sDestinationURL, "%CATIDS%", sCatIDs, 1, -1 ,1) sDestinationURL = replace(sDestinationURL, "%KWDS%", sKWDs, 1, -1 ,1) End Sub '-------------------------------------------------------------------------- ' Login Request '-------------------------------------------------------------------------- Public Sub LoginHTTPRequest() Dim objXMLHTTP, iResponseStatus Dim strHeaders, hArr, kk, theCookie ' Set objXMLHTTP = CreateObject("Microsoft.XMLHTTP") ' Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP") ' Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP.2.6") ' Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP.3.0") ' Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP") ' Set objXMLHTTP = CreateObject("Msxml2.ServerXMLHTTP.3.0") ' Set objXMLHTTP = CreateObject("WinHttp.WinHttpRequest") Set objXMLHTTP = CreateObject("WinHttp.WinHttpRequest.5.1") On Error Resume Next objXMLHTTP.Option(WinHttpRequestOption_EnableRedirects) = False objXMLHTTP.Option(WinHttpRequestOption_UserAgentString) = gsUserAgent ' objXMLHTTP.setRequestHeader "User-Agent", gsUserAgent objXMLHTTP.Open gsLoginGetPost, sLoginURL, false objXMLHTTP.setRequestHeader "Content-Type", gsLoginContentType if gsLoginCookies <> "" then objXMLHTTP.setRequestHeader "Cookie", gsLoginCookies end if if gsLoginReferer <> "" then objXMLHTTP.setRequestHeader "Referer", gsLoginReferer end if if Err.Number <> 0 then if gbDebug = true then sDebug = sDebug & "Login HTTP Call!" & vbcrlf sDebug = sDebug & "Err: " & Err.Number & vbcrlf sDebug = sDebug & "Source: " & Err.Source & vbcrlf sDebug = sDebug & "Desc: " & Err.Description & vbcrlf Call DebugOutput() end if end if On Error Goto 0 On Error Resume Next if sLoginPostData <> "" then objXMLHTTP.send sLoginPostData else objXMLHTTP.send end if iResponseStatus = objXMLHTTP.status On Error Goto 0 if err.Number <> 0 or (iResponseStatus <> 200 and iResponseStatus <> 302 and iResponseStatus <> 301) then bError = true sErrMsg = sErrMsg & "Error (Login Step)! Technical Debugging Details:" & vbcrlf if err.Number <> 0 then sErrMsg = sErrMsg & "Error Number: " & err.number & vbcrlf sErrMsg = sErrMsg & "Error Number: " & err.Description & vbcrlf end if if iResponseStatus <> 200 then if iResponseStatus = 404 then sErrMsg = sErrMsg & "Page does not exist (" & iResponseStatus & ")" & vbcrlf elseif iResponseStatus >= 401 and iResponseStatus < 402 then sErrMsg = sErrMsg & "Access denied (" & iResponseStatus & ")" & vbcrlf elseif iResponseStatus >= 500 and iResponseStatus <= 600 then sErrMsg = sErrMsg & "Internal Server Error on remote site (" & iResponseStatus & ")" & vbcrlf else sErrMsg = sErrMsg & "Response Code: " & iResponseStatus & vbcrlf End if sErrMsg = sErrMsg & "ParseError.URL: " & objXMLHTTP.parseError.URL & vbcrlf sErrMsg = sErrMsg & "ParseError.Reason: " &objXMLHTTP.parseError.Reason & vbcrlf end if else strHeaders = objXMLHTTP.getAllResponseHeaders() if instr(1, strHeaders, "Set-Cookie: ",1) > 0 then hArr = split(strHeaders,"Set-Cookie: ") for kk = 1 to ubound(hArr) if instr(1,hArr(kk),"path=/",1)> 0 then theCookie = left(hArr(kk),instr(1, hArr(kk),"path=/",1)-2) sCookieData = sCookieData & " " & theCookie else sDebug = sDebug & hArr(kk) & vbcrlf end if next end if if sCookieData = "" then bError = true sErrMsg = sErrMsg & "Error (Login Step)! No Cookie Data Set by Remote Server" & vbcrlf sErrMsg = sErrMsg & "Header Data:" & vbcrlf & vbcrlf sErrMsg = sErrMsg & strHeaders & vbcrlf & vbcrlf sErrMsg = sErrMsg & "Body Data:" & vbcrlf & vbcrlf sErrMsg = sErrMsg & objXMLHTTP.ResponseText & vbcrlf & vbcrlf end if end if Set objXMLHTTP = Nothing End Sub '-------------------------------------------------------------------------- ' Destination Request '-------------------------------------------------------------------------- Public Sub DestHTTPRequest(sDURL, sOFull) dim iResponseStatus2, objXMLHTTP2, objStream, sHTTPResults Dim bEnde , iStartTime ' Set objXMLHTTP2 = CreateObject("MSXML2.XMLHTTP.3.0") ' Set objXMLHTTP = CreateObject("Microsoft.XMLHTTP") ' Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP") ' Set objXMLHTTP = CreateObject("WinHttp.WinHttpRequest.5.1") ' Set objXMLHTTP = CreateObject("WinHttp.WinHttpRequest") Set objXMLHTTP2 = WScript.CreateObject("Microsoft.XMLHTTP") objXMLHTTP2.Open gsDestGetPost, sDURL, False objXMLHTTP2.setRequestHeader "User-Agent", gsUserAgent objXMLHTTP2.setRequestHeader "Content-Type", gsDestContentType if sCookieData <> "" then objXMLHTTP2.setRequestHeader "Cookie", sCookieData end if if gsDestReferer <> "" then objXMLHTTP2.setRequestHeader "Referer", gsDestReferer end if On Error Resume Next if gsDestPostData <> "" then objXMLHTTP2.send gsDestPostData else objXMLHTTP2.send end if 'wait for response bEnde = false iStartTime = now() While objXMLHTTP2.readyState <> READYSTATE_COMPLETE and bEnde = false objXMLHTTP2.waitForResponse 1000 if datediff("s",iStartTime,now()) > giTimeOutTime then bEnde = true bError = true sErrMsg = sErrMsg & "Destination Download Request!" & vbcrlf sErrMsg = sErrMsg & "Timeout after " & giTimeOutTime & " seconds." & vbcrlf sErrMsg = sErrMsg & "The default timeout time is 300 seconds. You can change that with the /timeout: parameter." & vbcrlf sErrMsg = sErrMsg & "The set timeout limit might not be enough for larger product feeds." & vbcrlf & vbcrlf sErrMsg = sErrMsg & "Readystate: " & objXMLHTTP2.readyState & vbcrlf & vbcrlf end if Wend iResponseStatus2 = objXMLHTTP2.status On Error Goto 0 if err.Number <> 0 or iResponseStatus2 <> 200 then bError = true sErrMsg = sErrMsg & "Error (Destination Step)! Technical Debugging Details:" & vbcrlf if err.Number <> 0 then sErrMsg = sErrMsg & "Error Number: " & err.number & vbcrlf sErrMsg = sErrMsg & "Error Number: " & err.Description & vbcrlf end if if iResponseStatus2 <> 200 then if iResponseStatus2 = 404 then sErrMsg = sErrMsg & "Page does not exist (" & iResponseStatus2 & ")" & vbcrlf elseif iResponseStatus2 >= 401 and iResponseStatus2 < 402 then sErrMsg = sErrMsg & "Access denied (" & iResponseStatus2 & ")" & vbcrlf elseif iResponseStatus2 >= 500 and iResponseStatus2 <= 600 then sErrMsg = sErrMsg & "Internal Server Error on remote site (" & iResponseStatus2 & ")" & vbcrlf else sErrMsg = sErrMsg & "Response Code: " & iResponseStatus2 & vbcrlf End if sErrMsg = sErrMsg & "ParseError.URL: " & objXMLHTTP2.parseError.URL & vbcrlf sErrMsg = sErrMsg & "ParseError.Reason: " &objXMLHTTP2.parseError.Reason & vbcrlf end if end if if bError = false then 'Create the stream Set objStream = CreateObject("ADODB.Stream") 'Initialize the stream objStream.Open objStream.Position = 0 'Reset the position and indicate the character encoding Select Case gsOutputFormat Case "A" objStream.Type = 2 ' adTypeText objStream.Charset ="ascii" sHTTPResults = objXMLHTTP2.ResponseText objStream.WriteText sHTTPResults Case "B" objStream.Type = 1 ' adTypeBinary sHTTPResults = objXMLHTTP2.ResponseBody 'sHTTPResults = objXMLHTTP.ResponseStream objStream.Write sHTTPResults Case else objStream.Type = 2 ' adTypeText objStream.Charset ="ascii" 'objStream.LineSeparator 'LineSeparatorsEnum , Constant Value Description 'adCRLF -1 Default. Carriage return line feed 'adLF 10 Line feed only 'adCR 13 Carriage return only sHTTPResults = objXMLHTTP2.ResponseText objStream.WriteText sHTTPResults End Select Set objXMLHTTP2 = Nothing if objStream.size <= 0 then bError = true sErrMsg = sErrMsg & "Response Stream is 0 bytes." & vbcrlf Set objStream = Nothing end if end if if bError = false then 'Save the stream to a file if ofso.FileExists(sOFull) then if sOWrite = "N" then sErrMsg = sErrMsg & "Operation Aborted!" & vbcrlf sErrMsg = sErrMsg & "Automatic Output File Overwrite disabled with /over:N option." & vbcrlf sErrMsg = sErrMsg & "Target Output File " & sOFull & " already exists" & vbcrlf WScript.echo sErrMsg WScript.Quit 1 else ofso.Deletefile sOFull end if end if objStream.SaveToFile sOFull,2 If NOT ofso.FileExists(sOFull) Then bError = true sErrMsg = sErrMsg & "Destination Download Error!" & vbcrlf sErrMsg = sErrMsg & "Output File " & sOFull & " could not be created." & vbcrlf end if Set objStream = Nothing End If End Sub Public Sub DebugOutput() sDebug = sDebug & "gsLoginURL: " & gsLoginURL & vbcrlf sDebug = sDebug & "gsLoginPostData: " & gsLoginPostData & vbcrlf sDebug = sDebug & "gsLoginGetPost: " & gsLoginGetPost & vbcrlf sDebug = sDebug & "gsLoginReferer: " & gsLoginReferer & vbcrlf sDebug = sDebug & "gsLoginCookies: " & gsLoginCookies & vbcrlf sDebug = sDebug & "gsLoginContentType: " & gsLoginContentType & vbcrlf sDebug = sDebug & "gsDestinationURL: " & gsDestinationURL & vbcrlf sDebug = sDebug & "gsDestPostData: " & gsDestPostData & vbcrlf sDebug = sDebug & "gsDestGetPost: " & gsDestGetPost & vbcrlf sDebug = sDebug & "gsDestReferer: " & gsDestReferer & vbcrlf sDebug = sDebug & "gsDestContentType: " & gsDestContentType & vbcrlf sDebug = sDebug & "------------------------------------------------" & vbcrlf sDebug = sDebug & "giAffID: " & giAffID & vbcrlf sDebug = sDebug & "gsProgIDs: " & gsProgIDs & vbcrlf sDebug = sDebug & "gsCatIDs: " & gsCatIDs & vbcrlf sDebug = sDebug & "gsKWDs: " & gsKWDs & vbcrlf sDebug = sDebug & "gsUserAgent: " & gsUserAgent & vbcrlf sDebug = sDebug & "------------------------------------------------" & vbcrlf sDebug = sDebug & "sLoginURL: " & sLoginURL & vbcrlf sDebug = sDebug & "sLoginPostData: " & sLoginPostData & vbcrlf sDebug = sDebug & "sCookieData: " & sCookieData & vbcrlf sDebug = sDebug & "sDestinationURL: " & sDestinationURL & vbcrlf sDebug = sDebug & "sDestPostData: " & sDestPostData & vbcrlf sDebug = sDebug & "sOutDir: " & sOutDir & vbcrlf sDebug = sDebug & "sOutFile: " & sOutFile & vbcrlf sDebug = sDebug & "sOutFull: " & sOutFull & vbcrlf sDebug = sDebug & "sUserN: " & sUserN & vbcrlf sDebug = sDebug & "sPassW: " & sPassW & vbcrlf sDebug = sDebug & "sAffID: " & sAffID & vbcrlf sDebug = sDebug & "sProgIDs: " & sProgIDs & vbcrlf sDebug = sDebug & "sCatIDs: " & sCatIDs & vbcrlf sDebug = sDebug & "sKWDs: " & sKWDs & vbcrlf sDebug = sDebug & "sOWrite: " & sOWrite & vbcrlf WScript.echo sDebug WScript.quit 1 End Sub