User Tools

Site Tools


mgenay:technique:script_flashcode_tcl
$wcl = New-Object Net.Webclient
#$wcl.Credentials = new-object Net.NetworkCredential("user", "pwd", "COMMUN")
#$wcl.Proxy.Credentials = new-object Net.NetworkCredential("user", "pwd", "COMMUN"); 




Function ParseFlashCode($code)
{
	#$code=1105
	$uri = new-object System.Uri "http://m.tcl.fr/flashcode/flashcode.do?code=$code"

	$result =  $wcl.DownloadString($uri)
	#$webrequestobject=[System.Net.WebRequest]::Create($uri)
	#$response=$webrequestobject.GetResponse()
	#$reqstream = $response.GetResponseStream()
	#$sr = new-object System.IO.StreamReader $reqstream

	#$result = $sr.ReadToEnd()
	#write-host $result

	filter Get-Matches($Pattern) {
	$_ | Select-String -AllMatches $pattern | 
	  Select-Object -ExpandProperty Matches | 
	  Select-Object -ExpandProperty Value
	}

	#write-host 'full page is in $result'
	#write-host $result
	$Stop=if (($result | Get-Matches 'Horaires - <span>[^<]+') -match '<span>(.*)') { $matches[1] }
	#write-host "l'arrêt"'est $Stop'"= $Stop"

	#$LinesDest = $result | Get-Matches '<img src=[^>]*toutes-les-lignes[^>]*/>' | foreach { if ($_ -match 'title="(.*)"') { $matches[1] } }
	$LinesDest2 = $result | Select-String -AllMatches '(<img src=[^>]*toutes-les-lignes[^>]*/>)|(</html)'  | 
		Select-Object -ExpandProperty Matches | 
		foreach {
			if ($_.Value -match 'title="(.*)"') {
				$h1 = New-Object System.Object
				$h1 | Add-Member -type NoteProperty -name Pattern -value $_.Value
				$h1 | Add-Member -type NoteProperty -name Index -value $_.Index
				$h1 | Add-Member -type NoteProperty -name Line -value $matches[1]
				$h1 | Add-Member -type NoteProperty -name Stop -value $Stop
				#$h1 | Add-Member -type NoteProperty -name Destination -value $matches[2]
				$h1
			} elseif ($_.Value -match '</html') {
				$h1 = New-Object System.Object
				$h1 | Add-Member -type NoteProperty -name Pattern -value "END"
				$h1 | Add-Member -type NoteProperty -name Index -value $_.Index
				$h1 | Add-Member -type NoteProperty -name Line -value ""
				$h1 | Add-Member -type NoteProperty -name Stop -value $Stop
				$h1 | Add-Member -type NoteProperty -name Destination ""
				$h1
			}
	}
	#write-host 'le detail des lignes trouvées est dans $LinesDest2'
	#write-host $LinesDest2

	if ($LinesDest2.Count -ge 2) {
		$nextStarts = (0..($LinesDest2.Count-2)) | foreach {
			$i=$_
			$data=$result.substring($LinesDest2[$i].Index,$LinesDest2[$i+1].Index-$LinesDest2[$i].Index);
			$ns0=New-Object System.Object
			$ns0 | Add-Member -type NoteProperty -name Code  -value ("{0:D5}" -f $Code)
			$ns0 | Add-Member -type NoteProperty -name Line -value $LinesDest2[$i].Line
			$ns0 | Add-Member -type NoteProperty -name Stop -value $LinesDest2[$i].Stop

			if ($data -match 'Vers\s<strong>([^<]+)<') {
				$ns0 | Add-Member -type NoteProperty -name Destination -value $matches[1]
			}

			if (-not $data -match '(\<!-- )<span class="DEPART-imminent">') {
				#depart imminent => 1 minutes
				$ns1=$ns0.PsObject.Copy()
				
				$delay=0
				$dd=(get-date).AddMinutes($delay)
				$ns1 | Add-Member -type NoteProperty -name MinutesToDeparture -value ("{0:D2}" -f $delay)
				$ns1 | Add-Member -type NoteProperty -name Departure -value (get-date $dd -format "HH:mm")
				$ns1 | Add-Member -type NoteProperty -name DepartureFull -value $dd
				$ns1
			}

			if ($data -match '<span class="HEURE-depart">\s*Proche') {
				#1er prochain départ
				$ns1=$ns0.PsObject.Copy()
				
				$delay=0
				$dd=(get-date).AddMinutes($delay)
				$ns1 | Add-Member -type NoteProperty -name MinutesToDeparture -value ("{0:D2}" -f $delay)
				$ns1 | Add-Member -type NoteProperty -name Departure -value (get-date $dd -format "HH:mm")
				$ns1 | Add-Member -type NoteProperty -name DepartureFull -value $dd
				$ns1
			}

			if ($data -match '<span class="HEURE-depart">\s*([0-9]+) min') {
				#1er prochain départ
				$ns1=$ns0.PsObject.Copy()
				
				$delay=[System.Convert]::ToInt32($matches[1])
				$dd=(get-date).AddMinutes($delay)
				$ns1 | Add-Member -type NoteProperty -name MinutesToDeparture -value ("{0:D2}" -f $delay)
				$ns1 | Add-Member -type NoteProperty -name Departure -value (get-date $dd -format "HH:mm")
				$ns1 | Add-Member -type NoteProperty -name DepartureFull -value $dd
				$ns1
			}

			if ($data -match '<span class="HEURE-arrivee">\s*([0-9]+) min') {
				#2ème prochain depart
				$ns1=$ns0.PsObject.Copy()
				
				$delay=[System.Convert]::ToInt32($matches[1])
				$dd=(get-date).AddMinutes($delay)
				$ns1 | Add-Member -type NoteProperty -name MinutesToDeparture -value ("{0:D2}" -f $delay)
				$ns1 | Add-Member -type NoteProperty -name Departure -value (get-date $dd -format "HH:mm")
				$ns1 | Add-Member -type NoteProperty -name DepartureFull -value $dd
				$ns1
			}
		}
		$nextStarts # | ft -AutoSize
	}
}

#$(ParseFlashCode(1104); ParseFlashCode(1105)) | ft -AutoSize

$csvfile="C:\Documents and Settings\PMGY07781\Bureau\tcl-codes.csv"
$wikifile="C:\Documents and Settings\PMGY07781\Bureau\tcl-codes.txt"
$collectedData=@()
if (test-path $csvfile) { $collectedData=Import-CSV -Path $csvfile }

$collectedCodes=$collectedData | group-object Code | foreach { $_.Group | select -first 1 } | foreach { [System.Convert]::ToInt32($_.Code) }

$d1=get-date
$newData=@()
(1..5000) | foreach {
   if ($newData.Count -le 1000) { #overload protection
	   if ($collectedCodes -notcontains $_) { $newData+=ParseFlashCode($_)  ; write-host $_ }
	   #else { write-host "skipping $_" }
   } }
$d2=get-date

#$newData | ft -AutoSize

($d2-$d1).TotalSeconds
#ParseFlashCode(1080)
#ParseFlashCode(1075)
if ($newData.Count -gt 0) {
    $collectedData+=$newData | select-object "Code","Line","Stop","Destination",@{Name="Link";Expression={"http://m.tcl.fr/flashcode/flashcode.do?code="+$_.Code}}
    $collectedData | Export-CSV -Path $csvfile -NoTypeInformation -Encoding UTF8
}

$horodate=get-date -Format "yyyy-MM-dd HH:mm:ss"
$wiki=$(
	Write-Output "{| class=""wikitable alternance centre""","|+ Liens QRCode/FlashCode de toutes les lignes TCL, réalisé le $horodate via le [[Script FlashCode TCL|script]]",
				"|-","! scope=""col"" | Ligne", "! scope=""col"" | Arrêt", "! scope=""col"" | Destination", "! scope=""col"" | Lien"

$collectedData | foreach { Write-Output "|-","| $($_.Line)","| $($_.Stop)","| $($_.Destination)","| $($_.Link)" }

Write-Output "|}"
)

$wiki | Out-File -FilePath $wikifile
mgenay/technique/script_flashcode_tcl.txt · Last modified: by 127.0.0.1