Sub DownloadString(URL As String) As ResumableSub
Log("Downloading from >>>" & URL & "<<<")
Dim S As String = ""
Dim j As HttpJob
j.Initialize("", Me)
j.Download(URL)
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
S = j.GetString
End If
j.Release
Log("Downloaded " & S.Length &" Chars = >>>" & S & "<<<")
Return S
End Sub
Sub Activity_Create(FirstTime As Boolean)
'get link from Google Sheets, File, Share, Share with others, Anyone with the link, Viewer, Copy
Dim URL As String = "https://docs.google.com/spreadsheets/d/1kwo_h0lH4ezu4ImPHdbkoxS4kz63n8qAlp__qlB93PA/edit?usp=sharing"
URL = URL.Replace("/edit?usp=sharing", "/export?format=tsv") 'change from edit to export tsv (NOT csv, which adds quotes we don't be needing)
wait for (DownloadString(URL)) complete (DownloadedCoupon As String)
DownloadedCoupon = DownloadedCoupon.Replace("""", "") 'just in case you decided to use csv anyway
'''Dim DownloadedCoupon As String = "{thisisa=demo, number=271828, expiry=1668653179748, centsperlitre=4}"
If DownloadedCoupon.Length <> 0 Then
Dim JSON As JSONParser
JSON.Initialize(DownloadedCoupon)
Dim CouponMap As Map = JSON.NextObject
Log("DownloadedCoupon = """ & DownloadedCoupon & """")
Log("CouponMap = " & CouponMap)
Log("Coupon number = " & CouponMap.Get("number"))
Log("Coupon expiry = " & DateTime.Date(CouponMap.Get("expiry")))
Dim DaysToGo As Int = (CouponMap.Get("expiry") - DateTime.Now) / DateTime.TicksPerDay
Log(DaysToGo & " days until coupon expires")
Else
Log("Could not download coupon... early bird catchs the worm ?")
End If
End Sub