Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # run as
- # powershell -ExecutionPolicy Bypass -File "currency_data.ps1"
- # Get current date
- $currentDate = Get-Date
- $endDay = $currentDate.ToString("dd")
- $endMonth = $currentDate.ToString("MM")
- $endYear = $currentDate.ToString("yyyy")
- # Calculate date 10 days ago for beginning date
- $beginDate = $currentDate.AddDays(-40)
- $begDay = $beginDate.ToString("dd")
- $begMonth = $beginDate.ToString("MM")
- $begYear = $beginDate.ToString("yyyy")
- # Form the GET strings
- $url1 = "https://nbkr.kg/index1.jsp?item=1562&lang=RUS&valuta_id=20&beg_day=$begDay&beg_month=$begMonth&beg_year=$begYear&end_day=$endDay&end_month=$endMonth&end_year=$endYear"
- $url2 = "https://nbkr.kg/index1.jsp?item=1562&lang=RUS&valuta_id=44&beg_day=$begDay&beg_month=$begMonth&beg_year=$begYear&end_day=$endDay&end_month=$endMonth&end_year=$endYear"
- Write-Host "Fetching data from first URL: $url1"
- try {
- # Fetch the first HTML content
- $response1 = Invoke-WebRequest -Uri $url1 -UseBasicParsing
- $htmlContent1 = $response1.Content
- # Find the lineChartData variable using regex
- $pattern = 'var lineChartData = \{[^}]+labels\s*:\s*\[([^\]]+)\][^}]+data\s*:\s*\[([^\]]+)\]'
- if ($htmlContent1 -match $pattern) {
- $labelsString = $matches[1]
- $dataOneString = $matches[2]
- # Clean up and parse labels (remove quotes and whitespace)
- $labels = $labelsString -split ',' | ForEach-Object { $_.Trim().Trim('"') }
- # Clean up and parse first data (remove whitespace)
- $dataOne = $dataOneString -split ',' | ForEach-Object { $_.Trim() }
- Write-Host "Fetching data from second URL: $url2"
- # Fetch the second HTML content
- $response2 = Invoke-WebRequest -Uri $url2 -UseBasicParsing
- $htmlContent2 = $response2.Content
- if ($htmlContent2 -match $pattern) {
- $dataTwoString = $matches[2]
- # Clean up and parse second data (remove whitespace)
- $dataTwo = $dataTwoString -split ',' | ForEach-Object { $_.Trim() }
- Write-Host "`nExtracted Data:"
- Write-Host "==============="
- # Output label-data pairs
- for ($i = 0; $i -lt $labels.Length -and $i -lt $dataOne.Length -and $i -lt $dataTwo.Length; $i++) {
- Write-Host "$($labels[$i]) - $($dataOne[$i]) - $($dataTwo[$i])"
- }
- # Save to file
- $outputFile = "currency_data.txt"
- $output = @()
- for ($i = 0; $i -lt $labels.Length -and $i -lt $dataOne.Length -and $i -lt $dataTwo.Length; $i++) {
- $output += "$($labels[$i]) - $($dataOne[$i]) - $($dataTwo[$i])"
- }
- $output | Out-File -FilePath $outputFile -Encoding UTF8
- Write-Host "`nData saved to: $outputFile"
- # Open the file in notepad
- Start-Process "notepad.exe" -ArgumentList $outputFile
- } else {
- Write-Host "Could not find lineChartData in the second HTML response"
- }
- } else {
- Write-Host "Could not find lineChartData in the first HTML response"
- Write-Host "Response preview:"
- Write-Host $htmlContent1.Substring(0, [Math]::Min(500, $htmlContent1.Length))
- }
- } catch {
- Write-Host "Error fetching data: $($_.Exception.Message)"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement