Save and restore arrays

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

RPC
Posts: 283
Joined: Fri Feb 10, 2017 4:12 am
DBs Used: DBF

Re: Save and restore arrays

Post by RPC »

edk wrote: Sat Feb 22, 2020 10:22 pm Forgive me, but I can't help you with these sources.
Hi Edward
I am totally floored by your humility. You don't have to apologize. In fact I feel bad you have devoted so much of your time in solving my problem.
I think it is quirky programming nseindia people must have done as a result of which we are able to download data only for some days.
I really want to thank you from core of my heart for helping me.

Just one last request, in case, in future, you come across some solution for downloading the file from the link produced by
https://www1.nseindia.com/ArchieveSearc ... section=EQ
please let me know

With best wishes
Rajeev
edk
Posts: 911
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland

Re: Save and restore arrays

Post by edk »

RPC wrote: Sun Feb 23, 2020 6:48 am Just one last request, in case, in future, you come across some solution for downloading the file from the link produced by
https://www1.nseindia.com/ArchieveSearc ... section=EQ
please let me know

With best wishes
Rajeev
Hi Rajeev.

Try this, it finally works for me ;) :

Code: Select all

#include "HMG.CH"

FUNCTION main()

SET DATE BRIT
SET CENTURY ON

DEFINE WINDOW testunzip MAIN AT 0,0 WIDTH 200 HEIGHT 200
       DEFINE DATEPICKER datepicker_1
	           ROW 20
			   COL 20
			   WIDTH 100
			   HEIGHT 20
			   ONLOSTFOCUS getDay()
			   VALUE CTOD('17/12/2015')
	   END DATEPICKER

       DEFINE BUTTON Button_1
          ROW 100
          COL 20
          WIDTH 100
          HEIGHT 30
          CAPTION "Download" 
		  ACTION Download()
      END BUTTON	

      DEFINE LABEL Lbl_1
         ROW 45
         COL 20
         WIDTH 150
         HEIGHT 20
		 VALUE ""
      END LABEL		 
	  
	  
END WINDOW
testunzip.center
testunzip.activate
RETURN nil	  


FUNCTION Download()
LOCAL cBhavFile, cFileNm, cUrl, cFile, dDate, cHost
      dDate := testunzip.datepicker_1.value
      cBhavFile := 'cm' + ntoc( DAY( dDate ), 10, 2, '0' ) + UPPER( LEFT( CMONTH( dDate ), 3 ) ) + ntoc( YEAR( dDate ), 10, 4, '0' ) + 'bhav.csv'
      cFileNm := cBhavFile + '.zip'
      cHost:= "https://www1.nseindia.com"
      cUrl := "/ArchieveSearch?h_filetype=eqbhav&date=" + hb_DtoC( dDate , "dd-mm-YYYY" ) + "&section=EQ"

      cFile := getcurrentfolder() + '\' + cFileNm

      lGotFile := downloadfile( cHost, cUrl, cFile, dDate, "NSE" )
      IF !lGotFile
         msginfo( {"NSE Bhavcopy file not found for ", dDate })
         
      ELSE
         IF FILE( cFile )
         
		 UNCOMPRESS cFile
		 msgdebug("File uncompressed")
         
		 ENDIF
     ENDIF
RETURN nil

FUNCTION GetDay()
testUnZip.Lbl_1.value := cdow(testUnZip.datepicker_1.value)
RETURN nil

****************************************************************
FUNCTION DownloadFile( cHost, cUrl, cFile, dDate, cExchange )

LOCAL ohttp, lOk
LOCAL nFileSize
Local cHeader, cHeaders, cLine, nPos, cLink, recv
Local cUrlFile:=""
Local cCookie:=""

   IF FILE ( cFile ) //old file exist
   	DELETEFILE ( cFile )
   ENDIF

   lOk := .F.
   nFileSize := 0
   BEGIN SEQUENCE WITH { | e | BREAK( e ) }
   	oHttp := Win_OleCreateObject( "WinHttp.WinHttpRequest.5.1" )
   	
   	// Connect to the HTTPs server
	// Set Headers
	ohttp:Open( "GET", cHost + cUrl, .T. )
	ohttp:setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
	ohttp:setRequestHeader("Accept", "*/*")
	ohttp:setRequestHeader("X-Requested-With", "XMLHttpRequest")
	ohttp:setRequestHeader("User-Agent", "Mozilla/5.0 (Linux; Android 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Mobile Safari/537.36")
	ohttp:setRequestHeader("DNT", "1")
	ohttp:setRequestHeader("Referer", "https://www.nseindia.com/products/content/equities/indices/historical_index_data.htm")
	ohttp:Send()
	ohttp:WaitForResponse()

	// download the response
	recv := ohttp:responseBody()
	cHeaders:=ohttp:getAllResponseHeaders()
	IF ohttp:Status <> 200
         BREAK "HTTP Server " + cHost + cUrl + " status is " + hb_NToS( ohttp:Status ) + " " + ohttp:statusText
     ENDIF
     
     //get cookies
	cCookie:=""
	FOR EACH cHeader IN hb_ATokens(cHeaders, .T.)	
		IF Left(cHeader,12) == "Set-Cookie: "
			cCookie += IF (!EMPTY( cCookie ), '; ', '') + hb_ATokens(SubStr( cHeader, 13 ), ';') [1]
		ENDIF
	NEXT
	
*	msgdebug (cCookie)
     
	//check links

	FOR EACH cLine IN hb_ATokens( recv, .T.)
		nPos:=AT("<A HREF=", Upper(cLine))
		IF nPos > 0 
			cLink:= SubsTR(cLine, nPos + Len("<A HREF=") )
			nPos:=AT(" ", cLink)
			IF nPos > 0
				cLink:=Left(cLink, nPos - 1)
			ENDIF
			IF hb_FNameNameExt (cLink) == hb_FNameNameExt (cFile)
				cUrlFile:= cHost + cLink
			ENDIF
		ENDIF
	NEXT 
	
	IF EMPTY ( cUrlFile )
		BREAK 'No link to the "' +  hb_FNameNameExt (cFile) + '" file'
	ENDIF
*	msgdebug (cUrlFile)
	
	//get file from link
	ohttp:Open( "GET", cUrlFile, .T. )
	ohttp:setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
	ohttp:setRequestHeader("Accept", "*/*")
	ohttp:setRequestHeader("X-Requested-With", "XMLHttpRequest")
	ohttp:setRequestHeader("User-Agent", "Mozilla/5.0 (Linux; Android 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Mobile Safari/537.36")
	IF !Empty( cCookie )
		ohttp:setRequestHeader("Cookie", cCookie)	//required if exist
	ENDIF
	ohttp:setRequestHeader("DNT", "1")
	ohttp:setRequestHeader("Referer", "https://www.nseindia.com/products/content/equities/indices/historical_index_data.htm")
	ohttp:Send()
	ohttp:WaitForResponse()
	
	// download the response
	recv := ohttp:responseBody()
	*cHeaders:=ohttp:getAllResponseHeaders()
	IF ohttp:Status <> 200
         BREAK "HTTP Server " + cUrlFile + " status is " + hb_NToS( ohttp:Status ) + " " + ohttp:statusText
     ENDIF
     
     ohttp:=Nil
     
     IF EMPTY( recv ) .OR. '!DOCTYPE' $ recv
    	     BREAK "Invalid response "
    	ENDIF
    	
    	StrFile ( recv, cFile )
     

   RECOVER USING oError
      MsgStop( IF( VALTYPE( oError ) = 'O', oError:Description, oError ) )

   END SEQUENCE

   ohttp := Nil

   lOk := FILE( cFile )

RETURN lOk
It seems to me that the problem is the use of the object "MSXML2.ServerXMLHTTP", which probably sends cookies incorrectly.

PS. The earliest file I've ever downloaded is from November 7, 1994

Best regards, Edward.
RPC
Posts: 283
Joined: Fri Feb 10, 2017 4:12 am
DBs Used: DBF

Re: Save and restore arrays

Post by RPC »

Hi Edward
I just downloaded your program and tried it. It works flawlessly , and downloads from 3 Nov 1994, the first date for which data is available.
I was dejected and had actually given up and was not checking the forum, but from another HMGian(Mr Bharat Dave) I got message you have uploaded a new version which works. I was overjoyed.
You are a gem of a person. You have persevered in helping me. I will never forget.
I will incorporate this program in my program and download files to my hearts content. :))
Wishing you the best.
Rajeev
Post Reply