hbcurl y googledrive

hbcurl y googledrive

Postby Baxajaun » Sun Aug 17, 2014 9:30 am

Buenas,

he estado probando hbcurl para descarga de ficheros desde googledrive, este siempre me contestaba diciéndome que el fichero se había movido temporalmento o se había redirigido:

Code: Select all  Expand view
<HTML>
<HEAD>
<TITLE>Moved Temporarily</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Moved Temporarily</H1>
The document has moved <A HREF="https://sites.google.com/site/ejemplo/home/fichero.zip?attredirects=0">here</A>.
</BODY>
</HTML>
<HTML>
<HEAD>
<TITLE>Moved Temporarily</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Moved Temporarily</H1>
The document has moved <A HREF="https://sites.google.com/site/ejemplo/home/fichero.zip?attredirects=0">here</A>.
</BODY>
</HTML>
 


lo he solucionado añadiendo la siguiente sentencia al código

Code: Select all  Expand view
curl_easy_setopt( curl, HB_CURLOPT_FOLLOWLOCATION )


Desde estas líneas dar las gracias a nuestro compañero Carlos Mora por su ayuda. Carlos, muchísimas gracias.

Saludos
User avatar
Baxajaun
 
Posts: 968
Joined: Wed Oct 19, 2005 2:17 pm
Location: Gatika. Bizkaia

Re: hbcurl y googledrive

Postby postinelli » Wed Aug 20, 2014 11:34 am

podrias compartir la rutina para bajar archivos?

tienes forma de subir archivos a googledrive ?

desde ya gracias
postinelli
 
Posts: 149
Joined: Tue Jul 15, 2008 7:12 pm
Location: Argentina

Re: hbcurl y googledrive

Postby Baxajaun » Wed Aug 20, 2014 12:55 pm

Hola Postinelli,

aquí tienes la función que uso:

Code: Select all  Expand view
FUNCTION Descarga(sURL)

LOCAL cFolderDestino := StrCapFirst( cFilePath( GetModuleFileName( GetInstance() ) ) + "Descargas" )
LOCAL curl, lOK

curl_global_init()

IF ! Empty( curl := curl_easy_init() )
            curl_easy_setopt( curl, HB_CURLOPT_DOWNLOAD )
            curl_easy_setopt( curl, HB_CURLOPT_URL, sURL )
             // Si usa https, estas lineas ayudan
            curl_easy_setopt( curl, HB_CURLOPT_SSL_VERIFYPEER, FALSE )
            curl_easy_setopt( curl, HB_CURLOPT_SSL_VERIFYHOST, FALSE )
            curl_easy_setopt( curl, HB_CURLOPT_FOLLOWLOCATION )        // Necesario para aquellos sitios que nos redirigen a otros
            curl_easy_setopt( curl, HB_CURLOPT_FILETIME, 1)
            curl_easy_setopt( curl, HB_CURLOPT_DL_FILE_SETUP,cFolderDestino + "\fichero.zip")
            curl_easy_setopt( curl, HB_CURLOPT_NOPROGRESS, FALSE )
            curl_easy_setopt( curl, HB_CURLOPT_VERBOSE, FALSE )
            curl_easy_perform( curl )
                                   
            if curl_easy_perform( curl ) == 0
               lOK := TRUE
            Else
              lOK := FALSE
           endif
            curl_easy_reset( curl )
endif
curl_global_cleanup()

RETURN lOK


Saludos,
User avatar
Baxajaun
 
Posts: 968
Joined: Wed Oct 19, 2005 2:17 pm
Location: Gatika. Bizkaia

Re: hbcurl y googledrive

Postby Baxajaun » Wed Aug 20, 2014 1:37 pm

Postinelli,

puedes mejorar la rutina añadiendo:

Code: Select all  Expand view

// Variables para tomar el nombre del fichero de la URL
LOCAL fichero := ""
LOCAL nCaracteres := LEN(sURL)
LOCAL nPosicion := RAT("/",sURL)

fichero := SubStr(sURL,nPosicion + 1, nCaracteres - nPosicion)

//sustituye la línea anterior
//curl_easy_setopt( curl, HB_CURLOPT_DL_FILE_SETUP,cFolderDestino + "\fichero.zip)
//por
curl_easy_setopt( curl, HB_CURLOPT_DL_FILE_SETUP,cFolderDestino + "\" + fichero)


Incluso podías hacerlo así:
Code: Select all  Expand view

LOCAL fichero := SubStr( sURL,  RAT("/",sURL) + 1, LEN(sURL) -  RAT("/",sURL) )
 


Saludos
User avatar
Baxajaun
 
Posts: 968
Joined: Wed Oct 19, 2005 2:17 pm
Location: Gatika. Bizkaia

Re: hbcurl y googledrive

Postby Baxajaun » Wed Aug 20, 2014 1:47 pm

Postinelli,

adjunto el código del ejemplo ftp_uldl.prg de hbcurl, podrás ver los parámetros que se utilizan para poder subir ficheros.

Code: Select all  Expand view

/*
 * $Id: ftp_uldl.prg 17552 2012-06-04 22:21:59Z vszakats $
 */


/* NOTE: Redirect STDERR to a file to see the verbose output. */

#include "hbcurl.ch"

#include "fileio.ch"

#define UPLOAD_FILE_AS      "test_ul.bin"
#define RENAME_FILE_TO      "test_ul_renamed.bin"
#define REMOTE_URL          "ftp://harbour:power@localhost/" + UPLOAD_FILE_AS
#define REMOTE_URL_DEL      "ftp://harbour:power@localhost/" + RENAME_FILE_TO
#define REMOTE_URL_MEM      "ftp://harbour:power@localhost/from_mem.txt"

PROCEDURE Main( cDL, cUL )
   LOCAL curl
   LOCAL info
   LOCAL tmp
   LOCAL tmp1
   LOCAL f
   LOCAL a

   LOCAL lVerbose := .F.

   ? curl_version()
   ? curl_getdate( "Sun, 1 Jun 2008 02:10:58 +0200" )

   info := curl_version_info()

   FOR tmp := 1 TO Len( info )
      IF tmp == 8
         ? tmp, ""
         FOR tmp1 := 1 TO Len( info[ 8 ] )
            ?? info[ 8 ][ tmp1 ] + " "
         NEXT
      ELSE
         ? tmp, info[ tmp ]
      ENDIF
   NEXT

   ? "Press key..."
   Inkey( 0 )

   ? "INIT:", curl_global_init()

   IF ! Empty( curl := curl_easy_init() )

      ? "ESCAPE:", tmp := curl_easy_escape( curl, "http://domain.com/my dir with space&more/" )
      ? "UNESCAPE:", curl_easy_unescape( curl, tmp )

      ? "Press key..."
      Inkey( 0 )

      hb_default( @cUL, "ftp_uldl.prg" )

      ? curl_easy_setopt( curl, HB_CURLOPT_UPLOAD )
      ? curl_easy_setopt( curl, HB_CURLOPT_URL, REMOTE_URL )
      ? curl_easy_setopt( curl, HB_CURLOPT_UL_FILE_SETUP, cUL )
      ? curl_easy_setopt( curl, HB_CURLOPT_INFILESIZE, hb_FSize( cUL ) ), hb_FSize( cUL )
//    ? curl_easy_setopt( curl, HB_CURLOPT_USERPWD, "harbour:power" ) /* May use this instead of embedding in URL */
      ? curl_easy_setopt( curl, HB_CURLOPT_PROGRESSBLOCK, {| nPos, nLen | a := CurGet(), DispOutAt( 10, 10, Str( ( nPos / nLen ) * 100, 6, 2 ) + "%" ), CurSet( a ) } )
      ? curl_easy_setopt( curl, HB_CURLOPT_NOPROGRESS, .F. )
      ? curl_easy_setopt( curl, HB_CURLOPT_POSTQUOTE, { "RNFR " + UPLOAD_FILE_AS, "RNTO " + RENAME_FILE_TO } )
      ? curl_easy_setopt( curl, HB_CURLOPT_VERBOSE, lVerbose )

      ? "UPLOAD FILE:", curl_easy_perform( curl )

      ? curl_easy_getinfo( curl, HB_CURLINFO_EFFECTIVE_URL )
      ? curl_easy_getinfo( curl, HB_CURLINFO_TOTAL_TIME )

      info := curl_easy_getinfo( curl, HB_CURLINFO_SSL_ENGINES, @tmp )
      ? "SSL ENGINES: ", tmp, Len( info )
      FOR tmp := 1 TO Len( info )
         ?? info[ tmp ] + " "
      NEXT

      curl_easy_reset( curl )

      ? "Press key..."
      Inkey( 0 )

      /* Delete file */

      ? curl_easy_setopt( curl, HB_CURLOPT_UPLOAD )
      ? curl_easy_setopt( curl, HB_CURLOPT_UL_NULL_SETUP )
      ? curl_easy_setopt( curl, HB_CURLOPT_URL, REMOTE_URL_DEL )
//    ? curl_easy_setopt( curl, HB_CURLOPT_USERPWD, "harbour:power" ) /* May use this instead of embedding in URL */
      ? curl_easy_setopt( curl, HB_CURLOPT_NOPROGRESS )
      ? curl_easy_setopt( curl, HB_CURLOPT_POSTQUOTE, { "DELE " + RENAME_FILE_TO } )
      ? curl_easy_setopt( curl, HB_CURLOPT_VERBOSE, lVerbose )

      ? "DELETE FILE:", curl_easy_perform( curl )

      curl_easy_reset( curl )

      ? "Press key..."
      Inkey( 0 )

      /* Upload file from memory */

      tmp := "This will be the content of the file"

      ? curl_easy_setopt( curl, HB_CURLOPT_UPLOAD )
      ? curl_easy_setopt( curl, HB_CURLOPT_URL, REMOTE_URL_MEM )
      ? curl_easy_setopt( curl, HB_CURLOPT_UL_BUFF_SETUP, tmp )
      ? curl_easy_setopt( curl, HB_CURLOPT_INFILESIZE, Len( tmp ) ), Len( tmp )
//    ? curl_easy_setopt( curl, HB_CURLOPT_USERPWD, "harbour:power" ) /* May use this instead of embedding in URL */
      ? curl_easy_setopt( curl, HB_CURLOPT_PROGRESSBLOCK, {| nPos, nLen | a := CurGet(), DispOutAt( 10, 10, Str( ( nPos / nLen ) * 100, 6, 2 ) + "%" ), CurSet( a ) } )
      ? curl_easy_setopt( curl, HB_CURLOPT_NOPROGRESS, .F. )
      ? curl_easy_setopt( curl, HB_CURLOPT_VERBOSE, lVerbose )

      ? "UPLOAD FILE FROM MEMORY:", curl_easy_perform( curl )

      ? curl_easy_getinfo( curl, HB_CURLINFO_EFFECTIVE_URL )
      ? curl_easy_getinfo( curl, HB_CURLINFO_TOTAL_TIME )

      curl_easy_reset( curl )

      ? "Press key..."
      Inkey( 0 )

      hb_default( @cDL, "ftp://ftp.cisco.com/README" )

      /* Now let's download to a file */

      ? curl_easy_setopt( curl, HB_CURLOPT_DOWNLOAD )
      ? curl_easy_setopt( curl, HB_CURLOPT_URL, cDL )
      ? curl_easy_setopt( curl, HB_CURLOPT_SSL_VERIFYPEER, .F. )
      ? curl_easy_setopt( curl, HB_CURLOPT_SSL_VERIFYHOST, .F. )
      ? curl_easy_setopt( curl, HB_CURLOPT_DL_FILE_SETUP, "test_dl.bin" )
      ? curl_easy_setopt( curl, HB_CURLOPT_PROGRESSBLOCK, {| nPos, nLen | a := CurGet(), DispOutAt( 11, 10, Str( ( nPos / nLen ) * 100, 6, 2 ) + "%" ), CurSet( a ) } )
      ? curl_easy_setopt( curl, HB_CURLOPT_NOPROGRESS, .F. )
      ? curl_easy_setopt( curl, HB_CURLOPT_VERBOSE, lVerbose )

      ? "DOWNLOAD FILE:", curl_easy_perform( curl )

      curl_easy_reset( curl )

      ? "Press key..."
      Inkey( 0 )

      /* Now let's download to memory */

      ? curl_easy_setopt( curl, HB_CURLOPT_DOWNLOAD )
      ? curl_easy_setopt( curl, HB_CURLOPT_URL, "http://kent.dl.sourceforge.net/sourceforge/harbour-project/harbour-static-0.99.2-0mdk20070.i586.rpm" )
      ? curl_easy_setopt( curl, HB_CURLOPT_SSL_VERIFYPEER, .F. )
      ? curl_easy_setopt( curl, HB_CURLOPT_SSL_VERIFYHOST, .F. )
      ? curl_easy_setopt( curl, HB_CURLOPT_DL_BUFF_SETUP )
      ? curl_easy_setopt( curl, HB_CURLOPT_PROGRESSBLOCK, {| nPos, nLen | a := CurGet(), DispOutAt( 11, 10, Str( ( nPos / nLen ) * 100, 6, 2 ) + "%" ), CurSet( a ) } )
      ? curl_easy_setopt( curl, HB_CURLOPT_NOPROGRESS, .F. )
      ? curl_easy_setopt( curl, HB_CURLOPT_VERBOSE, lVerbose )

      ? "DOWNLOAD FILE TO MEM:", curl_easy_perform( curl )

      tmp := "test_dlm.bin"
      ? "WRITING TO FILE: ", tmp
      f := FCreate( tmp, FC_NORMAL )
      IF f != F_ERROR
         FWrite( f, curl_easy_dl_buff_get( curl ) )
         FClose( f )
      ENDIF

      curl_easy_reset( curl )

      ? "Press key..."
      Inkey( 0 )

      hb_default( @cDL, "ftp://ftp.cisco.com/" )

      /* Now let's download a dirlist to memory */

      ? curl_easy_setopt( curl, HB_CURLOPT_DOWNLOAD )
      ? curl_easy_setopt( curl, HB_CURLOPT_DIRLISTONLY )
      ? curl_easy_setopt( curl, HB_CURLOPT_URL, cDL )
      ? curl_easy_setopt( curl, HB_CURLOPT_SSL_VERIFYPEER, .F. )
      ? curl_easy_setopt( curl, HB_CURLOPT_SSL_VERIFYHOST, .F. )
      ? curl_easy_setopt( curl, HB_CURLOPT_DL_BUFF_SETUP )
      ? curl_easy_setopt( curl, HB_CURLOPT_PROGRESSBLOCK, {| nPos, nLen | a := CurGet(), DispOutAt( 11, 10, Str( ( nPos / nLen ) * 100, 6, 2 ) + "%" ), CurSet( a ) } )
      ? curl_easy_setopt( curl, HB_CURLOPT_NOPROGRESS, .F. )
      ? curl_easy_setopt( curl, HB_CURLOPT_VERBOSE, lVerbose )

      ? "DOWNLOAD DIRLIST TO STRING:", curl_easy_perform( curl )

      ? "RESULT 1: " + curl_easy_dl_buff_get( curl )
      ? curl_easy_setopt( curl, HB_CURLOPT_DL_BUFF_GET, @tmp )
      ? "RESULT 2: " + tmp

      /* Cleanup session */

      curl_easy_cleanup( curl )

   ENDIF

   curl_global_cleanup()

   RETURN

STATIC FUNCTION CurGet()
   RETURN { Row(), Col() }

STATIC PROCEDURE CurSet( a )
   SetPos( a[ 1 ], a[ 2 ] )
   RETURN

 


Saludos,
User avatar
Baxajaun
 
Posts: 968
Joined: Wed Oct 19, 2005 2:17 pm
Location: Gatika. Bizkaia

Re: hbcurl y googledrive

Postby postinelli » Wed Aug 20, 2014 3:39 pm

muy amable, haré pruebas y comento
postinelli
 
Posts: 149
Joined: Tue Jul 15, 2008 7:12 pm
Location: Argentina

Re: hbcurl y googledrive

Postby Carles » Fri Sep 05, 2014 5:39 am

Baxajuan,

Podrias mostrar un ejemplo de la llamada al drive para subir/bajar un fichero ?

Gracias !
Salutacions, saludos, regards

"...programar es fácil, hacer programas es difícil..."

UT Page -> https://carles9000.github.io/
Forum UT -> https://discord.gg/bq8a9yGMWh
Skype -> https://join.skype.com/cnzQg3Kr1dnk
User avatar
Carles
 
Posts: 1134
Joined: Fri Feb 10, 2006 2:34 pm
Location: Barcelona


Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 50 guests