function Initialize([string]$DestinationFolder) {
if ((Test-Path -Path $DestinationFolder) -eq $false) {
Write-Host "Thư mục không tồn tại. Vui lòng thử lại sau!"
return
}
[string]$Url = "wss://io.dexscreener.com/dex/screener/pairs/h24/1?rankBy[key]=trendingScoreH6&rankBy[order]=desc&filters[chainIds][0]=ethereum"
[System.Threading.CancellationToken]$CT = New-Object System.Threading.CancellationToken
[System.Net.WebSockets.ClientWebSocket]$Client = [System.Net.WebSockets.ClientWebSocket]::new()
$Client.Options.SetRequestHeader("Origin", "https://dexscreener.com")
$Client.Options.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0")
try {
$Client.ConnectAsync($Url, $CT).GetAwaiter().GetResult()
if ($Client.State -eq 'Open') {
[bool]$ConnectionClosed = $false
while ($ConnectionClosed -eq $false) {
[byte[]]$Buffer = [byte[]] @(, 0) * 8192
[System.ArraySegment[byte]]$BufferAS = New-Object System.ArraySegment[byte] -ArgumentList @(, $Buffer)
[System.Net.WebSockets.WebSocketReceiveResult]$Result = $null
[System.IO.MemoryStream]$MS = New-Object System.IO.MemoryStream
do {
$Result = $Client.ReceiveAsync($BufferAS, $CT).GetAwaiter().GetResult()
if ($Result.MessageType -eq 'Close') {
$ConnectionClosed = $true
$Client.CloseOutputAsync([System.Net.WebSockets.WebSocketCloseStatus]::NormalClosure,[string]::Empty, $CT).GetAwaiter().GetResult()
}
else {
if ($Result.Count -gt 0) {
if ($BufferAS.Array) {
$MS.Write($BufferAS.Array, $BufferAS.Offset, $Result.Count)
}
}
}
}
while ($Result.EndOfMessage -eq $false)
$MS.Seek(0, [System.IO.SeekOrigin]::Begin)
[System.IO.StreamReader]$SR = [System.IO.StreamReader]::new($MS, [System.Text.ASCIIEncoding]::UTF8)
[string]$Response = $SR.ReadToEnd()
$SR.Close()
$MS.Close()
Write-Host $Response `r `f
ProcessResponse -Response $Response -Client $Client -CT $CT -DestinationFolder $DestinationFolder
}
}
}
catch {
Write-Host "Đã có lỗi xảy ra. $($_.Exception.Message)"
}
finally {
if ($Client.State -eq 'Open') {
$Client.CloseOutputAsync([System.Net.WebSockets.WebSocketCloseStatus]::NormalClosure,[string]::Empty, $CT).GetAwaiter().GetResult()
}
}
}
function ProcessResponse(
[string]$Response,
[System.Net.WebSockets.ClientWebSocket]$Client,
[System.Threading.CancellationToken]$CT,
[string]$DestinationFolder
) {
if ([string]::IsNullOrEmpty($Response) -eq $false) {
if ($Response.Contains("ping")) {
try {
[byte[]]$Buffer = [System.Text.ASCIIEncoding]::UTF8.GetBytes("pong")
[System.ArraySegment[byte]]$BufferAS = New-Object System.ArraySegment[byte] -ArgumentList @(, $Buffer)
$Client.SendAsync($BufferAS, [System.Net.WebSockets.WebSocketMessageType]::Text, $true, $CT).GetAwaiter().GetResult()
}
catch {
Write-Host "Đã có lỗi xảy ra. $($_.Exception.Message)"
}
}
else {
[PSCustomObject]$Json = ConvertFrom-Json -InputObject $Response
if (Get-Member -InputObject $Json -MemberType "Properties" -Name "type") {
if ($Json.type -eq "pairs") {
$Result = @()
$Json.pairs | ForEach-Object {
$Result += New-Object psobject -Property @{
"chainId" = $_.chainId
"dexId" = $_.dexId
"baseTokenAddress" = $_.baseToken.address
"baseTokenName" = $_.baseToken.name
"baseTokenSymbol" = $_.baseToken.symbol
"quoteTokenAddress" = $_.quoteToken.address
"quoteTokenName" = $_.quoteToken.name
"quoteTokenSymbol" = $_.quoteToken.symbol
"price" = $_.price
"priceUsd" = $_.priceUsd
"txns_m5_buy" = $_.txns.m5.buy
"txns_m5_sell" = $_.txns.m5.sell
"txns_h1_buy" = $_.txns.h1.buy
"txns_h1_sell" = $_.txns.h1.sell
"txns_h6_buy" = $_.txns.h6.buy
"txns_h6_sell" = $_.txns.h6.sell
"txns_h24_buy" = $_.txns.h24.buy
"txns_h24_sell" = $_.txns.h24.sell
"buyers_m5" = $_.buyers.m5
"buyers_h1" = $_.buyers.h1
"buyers_h6" = $_.buyers.h6
"buyers_h24" = $_.buyers.h24
"sellers_m5" = $_.sellers.m5
"sellers_h1" = $_.sellers.h1
"sellers_h6" = $_.sellers.h6
"sellers_h24" = $_.sellers.h24
"makers_m5" = $_.makers.m5
"makers_h1" = $_.makers.h1
"makers_h6" = $_.makers.h6
"makers_h24" = $_.makers.h24
"volume_m5" = $_.volume.m5
"volume_h1" = $_.volume.h1
"volume_h6" = $_.volume.h6
"volume_h24" = $_.volume.h24
"volumeBuy_m5" = $_.volumeBuy.m5
"volumeBuy_h1" = $_.volumeBuy.h1
"volumeBuy_h6" = $_.volumeBuy.h6
"volumeBuy_h24" = $_.volumeBuy.h24
"volumeSell_m5" = $_.volumeSell.m5
"volumeSell_h1" = $_.volumeSell.h1
"volumeSell_h6" = $_.volumeSell.h6
"volumeSell_h24" = $_.volumeSell.h24
"priceChange_m5" = $_.priceChange.m5
"priceChange_h1" = $_.priceChange.h1
"priceChange_h6" = $_.priceChange.h6
"priceChange_h24" = $_.priceChange.h24
"liquidity_usd" = $_.liquidity.usd
"liquidity_base" = $_.liquidity.base
"liquidity_quote" = $_.liquidity.quote
"marketCap" = $_.marketCap
"pairCreatedAt" = $_.pairCreatedAt
"eti" = $_.eti
"c" = $_.c
"a" = $_.a
}
}
$Result | Export-Csv -Path ($DestinationFolder + "\" + (Get-Date -Format "dd-MM-yyyy hh-mm-ss") + ".csv") -NoTypeInformation -Append
}
}
}
}
}
[string]$DestinationFolder = Read-Host -Prompt "Nhập đường dẫn thư mục chứa dữ liệu sẽ lưu lại"
if ([string]::IsNullOrEmpty($DestinationFolder) -eq $false) {
Initialize -DestinationFolder $DestinationFolder
} else {
Write-Host "Đường dẫn thư mục không thể để trống. Vui lòng thử lại sau!"
}