Created
June 17, 2020 14:48
-
-
Save schtritoff/547841c2a629ce93dd8a06df7d751f84 to your computer and use it in GitHub Desktop.
Download all invoices in JSON and PDF from web app SOLO.com.hr https://solo.com.hr/api-dokumentacija
This file contains 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
<# | |
.SYNOPSIS | |
Download all invoices from SOLO.COM.HR API | |
#> | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory=$True)] | |
[string]$Token, | |
[switch]$RefreshInvoicesList, | |
[switch]$RedownloadInvoices | |
) | |
$Url = "https://api.solo.com.hr/racun?token=$Token" | |
$InvoicesFile = Join-Path $PSScriptRoot ".\invoices.json" | |
$InvoicesRoot = Join-Path $PSScriptRoot ".\invoices" | |
# get all invoices | |
if (!(Test-Path $InvoicesFile) -or ($RefreshInvoicesList.IsPresent)) { | |
$invoices = Invoke-WebRequest -UseBasicParsing $url | |
$invoices.Content | Out-File $InvoicesFile | |
} | |
# create folder | |
If (!(Test-Path $InvoicesRoot)) { | |
New-Item -Path $InvoicesRoot -ItemType Directory | Out-Null | |
} | |
# get individual invoices | |
$invoices = Get-Content $InvoicesFile | ConvertFrom-Json | |
$invoices.racuni | foreach { | |
# https://docs.microsoft.com/en-us/dotnet/api/system.datetime.parseexact?redirectedfrom=MSDN&view=netcore-3.1#System_DateTime_ParseExact_System_String_System_String___System_IFormatProvider_System_Globalization_DateTimeStyles_ | |
# https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings?view=netcore-3.1 | |
$InvDate = [datetime]::ParseExact($_.datum_racuna,"d.M.yyyy. H:m:s",[Globalization.CultureInfo]::CreateSpecificCulture('hr-HR')) | |
# create year folder | |
$Year = $(Join-Path $InvoicesRoot $InvDate.Year) | |
if (!(Test-Path $year)) { | |
New-Item -Path $year -ItemType Directory | |
} | |
# create month folder | |
$Month = Join-Path $Year $InvDate.Month | |
if (!(Test-Path $Month)) { | |
New-Item -Path $Month -ItemType Directory | |
} | |
# get single invoice by id | |
$File = Join-Path $Month "$($_.id).json" | |
if (!(Test-Path $File) -or ($RedownloadInvoices.IsPresent)) { | |
$invoice = Invoke-WebRequest -UseBasicParsing $("$url&id="+$_.id) | |
# write to disk only if status = 0 (https://solo.com.hr/api-dokumentacija/prikaz-racuna) | |
if (($invoice.Content | ConvertFrom-Json).status -eq "0") { | |
$invoice.Content | Out-File $File | |
$invoice.Content | |
# sleep because of API rate limit | |
Start-Sleep -Seconds 11 | |
} | |
} | |
# get pdf file for invoice | |
if (Test-Path $File) { | |
# read json invoice from disk | |
$InvoiceJson = Get-Content $File | ConvertFrom-Json | |
# download PDF if does not exist yet | |
$FilePdf = Join-Path $Month "$($InvoiceJson.racun.id)_$($InvoiceJson.racun.broj_racuna).pdf" | |
if (!(Test-Path $FilePdf)) { | |
Write-Host "Downloading $($InvoiceJson.racun.pdf) to $FilePdf" | |
Invoke-WebRequest -UseBasicParsing -OutFile $FilePdf -Uri $($InvoiceJson.racun.pdf) | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment