Last active
August 23, 2021 10:25
-
-
Save Sia200/432213c0c2a35488b03b895fe95a2a29 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Examples: | |
# ======== | |
# | |
# Download the top 100 python packages, and for each package download the last 5 releases | |
# PS> Get-PythonPackages -TopPackages 100 -Last 5 | |
# | |
# Download packages listed in a file, and for each package download the last 5 releases | |
# PS> Get-PythonPackagesFromProvidedList -Path .\packages.txt -Last 5 | |
# | |
# Notes: | |
# ===== | |
# | |
# It's optional to execute $ProgressPreference = 'SilentlyContinue' before | |
# executing any function to prevent the blue progress bar window. | |
function Get-TopPackages { | |
$response = Invoke-WebRequest -UseBasicParsing 'https://hugovk.github.io/top-pypi-packages/top-pypi-packages-30-days.json' | |
return ($response | ConvertFrom-Json).rows | % {$_.project} | |
} | |
function Get-PythonPackageDownloadUrls { | |
param ( | |
[Parameter(Mandatory)] | |
[String]$Name, | |
[Int32]$Last = 10 | |
) | |
$urls = (Invoke-WebRequest -UseBasicParsing "https://pypi.org/simple/$Name/") -split "\n" | Select-String -Pattern 'href="(.*?)"' | Select -Last $Last | % {$_.Matches.Groups[1].Value} | |
[array]::Reverse($urls) | |
return $urls | |
} | |
function Download-FromUrl { | |
param ( | |
[Parameter(Mandatory)] | |
[String]$Url | |
) | |
$filename = Split-Path -Path $Url -Leaf | Select-String -Pattern "(.*)#" | % {$_.Matches[0].Groups[1].Value} | |
Write-Host -NoNewline ('[] ------ {0} ' -f $filename) | |
Invoke-WebRequest -Uri $Url -OutFile $filename | |
Write-Host ('[{0} KB]' -f ([math]::Round((Get-Item $filename).Length / 1024, 2))) | |
} | |
function Get-PythonPackages { | |
param ( | |
[Parameter(Mandatory)] | |
[Int32]$TopPackages, | |
[Int32]$Last = 10 | |
) | |
$packages = Get-TopPackages | Select -First $TopPackages | |
foreach ($package in $packages) { | |
Write-Host '[*] Downloading {0}' -f $package | |
$urls = Get-PythonPackageDownloadUrls -Name $package -Last $Last | |
foreach ($url in $urls) { | |
Download-FromUrl -Url $url | |
} | |
} | |
} | |
function Get-PythonPackagesFromProvidedList { | |
param ( | |
[Parameter(Mandatory)] | |
[String]$Path, | |
[Int32]$Last = 10 | |
) | |
Get-Content $Path | ForEach-Object { | |
Write-Host ('[*] Downloading {0}' -f $_) | |
$urls = Get-PythonPackageDownloadUrls -Name $_ -Last $Last | |
foreach ($url in $urls) { | |
Download-FromUrl -Url $url | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment