Iczer

cURL command line - unicode and options combination

5 posts in this topic

#1 ·  Posted (edited)

trying to make some download tool, but have 2 issues with curl.exe:

1. If I set user custom agent header, curl do not process "' --remote-name --remote-header-name '" settings. Otherwise downloaded file get proper name (from server).

2. If I try download file, which have unicode letters in FileName, download goes OK, but file on disk would have broken unicode letters in FileName ()

#include <WinAPI.au3>

$sDownloadUrl = "https://www.autoitscript.com/forum/applications/core/interface/file/attachment.php?id=51443"; eng filename
;$sDownloadUrl = "https://www.autoitscript.com/forum/applications/core/interface/file/attachment.php?id=51442"; unicode filename

$sPath = @TempDir
$sFileName = ""

cURL_CLI_Download($sDownloadUrl, $sPath, $sFileName)


Func cURL_CLI_Download($sDownloadUrl = "", $sPath = "", $sFileName = "")

    $sDownloadUrl = _WinAPI_WideCharToMultiByte($sDownloadUrl, 65001, True )
    $sFileName = _WinAPI_WideCharToMultiByte($sFileName, 65001, True )

    Local $aURI = StringRegExp($sDownloadUrl,'^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?',3), $code = ""

    $code &= ' --header "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0" '
    $code &= ' --header "Referer: ' & $aURI[0] & $aURI[2] & '" '
    $code &= ' --header "Accept-Charset: utf-8" '

    If $sFileName == "" Then
        $code &= ' --remote-name --remote-header-name '
    Else
        $code &= ' --output "' & $sFileName & '" '
    EndIf

    $code &= '--location '
    $code &= '--connect-timeout "88" '

    $code &= '"'&$sDownloadUrl&'" '

    ;$code = 'curl --help'

    If Not ($sPath == "") Then
        If Not FileExists($sPath) Then DirCreate($sPath)
        ;FileChangeDir($sPath)
    EndIf

    ConsoleWrite($code & @CRLF)
    ConsoleWrite(@ScriptDir & "\curl.exe" & @CRLF)

    $iPID = Run(@ScriptDir & "\curl.exe --trace-ascii - " & $code, $sPath, @SW_HIDE,$STDERR_MERGED)

    ProcessWaitClose($iPID)
    $sOutput = StdoutRead($iPID)
    StdioClose($iPID)

    ConsoleWrite("$sOutput = "&$sOutput & @CRLF)
EndFunc

Is there a something wrong with my approach to curl.exe?

いくつかのファイル名.txt

Some File Name.txt

Edited by Iczer

Share this post


Link to post
Share on other sites

You can't pass UTF8 strings to console programs unless the default charset for console is 65001.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

is there a fast way to change default charset for console back and forth? if not, punicode is only way?

 

Share this post


Link to post
Share on other sites

Look here :

 


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

thanks!

interesting, if i set user-agent like:

$code &= ' --user-agent "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/"'
$code &= ' --user-agent "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/dlfjldfkjdf"'
$code &= ' --user-agent "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/ 40.0"'

downloaded file is written to disk, but

$code &= ' --user-agent "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/*any digit*"'

then file is not created

is there a some way to escape characters (digits or slashes) in curl so firefox user agent can be used?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now