[..] Echec téléchargement avec IE - pas avec Powershell

Aide et conseils concernant AutoIt et ses outils.
Règles du forum
.
Répondre
Avatar du membre
franco
Niveau 7
Niveau 7
Messages : 342
Enregistré le : lun. 03 oct. 2011 22:37
Localisation : Liévin
Status : Hors ligne

[..] Echec téléchargement avec IE - pas avec Powershell

#1

Message par franco »

Bonjour.


Je vous explique en détail mon souci, avant de vous mettre le script...

Je télécharge souvent des logiciels, avec les fonctions d'IE (AutoIt), afin de les télécharger quand j'en ai besoin...


Un seul logiciel me pose problème : la version "alpha" de captvty.
site : http://v3.captvty.fr/

D'habitude :
- J'utilise _IECreate et _IELinkGetCollection pour lister les liens de cette page web.
- Puis une condition afin d'avoir uniquement le lien qui se finit en .zip, le lien du fichier par conséquent...
- et j'enchaine par un InetGet pour télécharger le dit fichier...

Sauf que là, malgré mes tests, même si l'extension se finit en .zip, si je l'ouvre avec un éditeur de texte : en fait, c'est un fichier HTML...


Puis, je me suis dit - par curiosité, avant de laisser tomber - et si je faisais cette méthode, via en ligne de commande...
Et vu que je voulais commencer Powershell, c'est le moment d'essayer...

Et donc :
- j'ai listé les liens avec powershell
- je récupère le lien
- je commence le téléchargement.

Et.... ça fonctionne...

Alors que j'ai fait pratiqué la même 'procédure'.



Voici le script :
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <IE.au3>

Global $oIE

$dossiertmp = @DesktopDir & '\captvtytmp'
DirRemove($dossiertmp, 1)
DirCreate($dossiertmp)

_captvty_alpha_powershell()
_captvty_alpha_IE()
ShellExecute($dossiertmp)


Func _captvty_alpha_powershell()

   $acceuil = 'http://v3.captvty.fr/'

   $captvty_a_ps_zip = $dossiertmp & '\captvty_alpha_pshell.zip'

   Local $sText, $lien_zip

   $cmd = '(Invoke-WebRequest -Uri "' & $acceuil & '"https://v3.captvty.fr/").Links | sort-object href -Unique | Format-List innerText, href'
   $commande = 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command ' & $cmd
   $pid = Run($commande, @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD)
   StdinWrite($pid)

   $sSTDOUT = ""
   While 1
      $sOutput = StdoutRead($pid)
      If @error Then ExitLoop
      If $sOutput <> "" Then
         $sText &= $sOutput & @CRLF
      EndIf
   WEnd

   If $sText <> '' Then
      For $a = 1 To StringSplit($sText, @LF)[0]
         $ligne = StringSplit($sText, @LF)[$a]
         If StringInStr($ligne, '.zip') Then $lien_zip = 'https:' & StringStripWS(StringReplace(StringReplace($ligne, ':', ''), 'href', ''), 8)
      Next
   EndIf

   $cmd_2 = 'Invoke-WebRequest -Uri "' & $lien_zip & '" -OutFile "' & $captvty_a_ps_zip
   $commande_2 = 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command ' & $cmd_2
   RunWait($commande_2, @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD)

EndFunc   ;==>_captvty_alpha_powershell


Func _captvty_alpha_IE()

   $acceuil = 'http://v3.captvty.fr/'

   $captvty_a_ie_zip = $dossiertmp & '\captvty_alpha_IE.zip'

   Local $sText, $lien_zip

   Local $oIE = _IECreate($acceuil)
   Local $oLinks = _IELinkGetCollection($oIE)
   Local $iNumLinks = @extended


   For $oLink In $oLinks
      If StringRight($oLink.href, 4) = '.zip' Then $lien_zip = $oLink.href
   Next

   $myDL = InetGet(StringStripWS($lien_zip, 8), $captvty_a_ie_zip, 1, 1)
   Do
      Sleep(250)
   Until InetGetInfo($myDL, 2) ; Check if the download is complete.
   Local $nBytes = InetGetInfo($myDL, 0)
   InetClose($myDL) ; Close the handle to release resources.

;~    $cmd_2 = 'Invoke-WebRequest -Uri "' & $lien_zip & '" -OutFile "' & $captvty_a_ie_zip
;~    $commande_2 = 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command ' & $cmd_2
;~    RunWait($commande_2, @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD)


EndFunc   ;==>_captvty_alpha_IE


;~ Func _FindBetweenString($Line, $char1, $char2)
;~    $part1 = StringRegExpReplace($Line, '^.*' & $char1, '')
;~    $Term = StringRegExpReplace($part1, '\' & $char2 & '.*', '')
;~    Return SetExtended(1, $Term)
;~ EndFunc   ;==>_FindBetweenString
 

Pouvez-vous me dire pourquoi, avec IE, ça coince ?



Merci.
++
L'entraide, c'est mon dada ;)
rgx
Niveau 4
Niveau 4
Messages : 61
Enregistré le : sam. 16 nov. 2019 17:53
Status : Hors ligne

Re: [..] Echec téléchargement avec IE - pas avec Powershell

#2

Message par rgx »

Bonjour,

Je ne vois pas avec IE (je n'utilise pas) mais pour ce style de téléchargement avec lien variable, je procède d'une manière simple:
  • Téléchargement de la page web (c'est à dire que je récupère le source)
  • Analyse du source pour trouver le texte entre les balises.
    Ici c'est facile puisqu'il est entre <a class="dl zip" href=" et .zip">Télécharger (archive ZIP)</a>
  • Téléchargement du fichier avec l'URL créée avec le texte ainsi isolé (il suffit juste de précéder par http://)
Le tout avec InetGet

Edit: Voilà une bafouille réalisée à partir de l'exemple de l'aide sur InetGet.
(Je donne au fichier binaire un nom temporaire, mais on peut mettre le vrai en le prenant dans l'URL du code source de la page)
#include <InetConstants.au3>

Local $FileName_Source = @TempDir & "\Source.html"
Local $FileName_Bin    = @TempDir & "\Captvty_Tmp.zip"
FileDelete($FileName_Source)
FileDelete($FileName_Bin)

ConsoleWrite("--- Informations ---")
ConsoleWrite(@CRLF & "Fichier HTML       : " & $FileName_Source)
ConsoleWrite(@CRLF & "Binaire Temporaire : " & $FileName_Bin)

; Charger la source
Local $hDownload = InetGet("http://v3.captvty.fr/", $FileName_Source, $INET_FORCERELOAD, $INET_DOWNLOADBACKGROUND)
Do
    Sleep(250)
Until InetGetInfo($hDownload, $INET_DOWNLOADCOMPLETE)
ConsoleWrite(@CRLF & "Octets téléchargés       : " & InetGetInfo($hDownload, $INET_DOWNLOADREAD))
ConsoleWrite(@CRLF & "Taille du Fichier source : " & FileGetSize($FileName_Source))

; Charger le fichier source en mémoire
Local $Full_Txt = FileRead($FileName_Source)
ConsoleWrite(@CRLF & "Octets en RAM            : " & StringLen($Full_Txt))

; Chercher les balises début et fin du fichier
Local $Div_A = "<a class=""dl zip"" href=""//"
Local $Div_B = ".zip"">Télécharger (archive ZIP)</a>"

Local $Pos_A = StringInStr($Full_Txt, $Div_A)
ConsoleWrite(@CRLF & "Début                    : " & $Pos_A)
Local $Pos_B = StringInStr($Full_Txt, $Div_B)
ConsoleWrite(@CRLF & "Fin                      : " & $Pos_B)

; Créer l'URL
Local $File_URL = "http://" & StringMid($Full_Txt, $Pos_A + 26, $Pos_B - $Pos_A -22)
ConsoleWrite(@CRLF & "URL du Fichier           : " & $File_URL)

; Charger le fichier binaire
$hDownload = InetGet($File_URL, $FileName_Bin, $INET_FORCERELOAD, $INET_DOWNLOADBACKGROUND)
Do
    Sleep(250)
Until InetGetInfo($hDownload, $INET_DOWNLOADCOMPLETE)
ConsoleWrite(@CRLF & "Octets téléchargés        : " & InetGetInfo($hDownload, $INET_DOWNLOADREAD))
ConsoleWrite(@CRLF & "Taille du Fichier binaire : " & FileGetSize($FileName_Bin))

InetClose($hDownload)
C'est moche, mais ça fonctionne :lol:

:wink:
Avatar du membre
mikell
Spammer !
Spammer !
Messages : 6292
Enregistré le : dim. 29 mai 2011 17:32
Localisation : Deep Cévennes
Status : Hors ligne

Re: [..] Echec téléchargement avec IE - pas avec Powershell

#3

Message par mikell »

InetRead suffit pour la page, et le site n'est pas en https donc c'est simple

$page = BinaryToString(InetRead("http://v3.captvty.fr/", 1), 4)
$link = "http://" & StringRegExpReplace($page, '(?s).*?(releases.*?zip).*', "$1")
InetGet($link, @scriptdir & "\" & StringRegExpReplace($link, '.*/', ""), 5)
Msgbox(0,"", "ok")
" L'échec est le fondement de la réussite. " (Lao-Tseu )
" Plus ça rate, plus on a de chances que ça marche " (les Shadoks )
Répondre