77 "archive/zip"
88 "bufio"
99 "bytes"
10+ "errors"
1011 "fmt"
1112 "io"
1213 "io/ioutil"
@@ -15,36 +16,49 @@ import (
1516 "os"
1617 "path"
1718 "path/filepath"
19+ "runtime"
1820 "strings"
19- "time"
20-
21- "github.com/PuerkitoBio/goquery"
2221)
2322
2423func download (prefix string ) (url string , content []byte , err error ) {
25- year := time .Now ().Year ()
26-
27- site := "https://www.sqlite.org/download.html"
28- //fmt.Printf("scraping %v\n", site)
29- doc , err := goquery .NewDocument (site )
24+ resp , err := http .Get ("https://www.sqlite.org/download.html" )
3025 if err != nil {
31- log . Fatal ( err )
26+ return "" , nil , err
3227 }
28+ defer resp .Body .Close ()
3329
34- doc .Find ("a" ).Each (func (_ int , s * goquery.Selection ) {
35- if strings .HasPrefix (s .Text (), prefix ) {
36- url = fmt .Sprintf ("https://www.sqlite.org/%d/" , year ) + s .Text ()
30+ b , err := ioutil .ReadAll (resp .Body )
31+ if err != nil {
32+ return "" , nil , err
33+ }
34+ html := string (b )
35+ start := strings .Index (html , `<!-- Download product data for scripts to read` )
36+ if start == - 1 {
37+ return "" , nil , errors .New ("Unable to find download section on sqlite.org" )
38+ }
39+ html = html [start :]
40+ end := strings .Index (html , `-->` )
41+ if end == - 1 {
42+ return "" , nil , errors .New ("Unable to find download section on sqlite.org" )
43+ }
44+ html = html [:end ]
45+ for _ , line := range strings .Split (html , "\n " ) {
46+ if strings .Contains (line , prefix ) {
47+ if tok := strings .Split (line , "," ); len (tok ) >= 5 {
48+ url = fmt .Sprintf ("https://www.sqlite.org/%s" , tok [2 ])
49+ break
50+ }
3751 }
38- })
52+ }
3953
4054 if url == "" {
4155 return "" , nil , fmt .Errorf ("Unable to find prefix '%s' on sqlite.org" , prefix )
4256 }
4357
4458 fmt .Printf ("Downloading %v\n " , url )
45- resp , err : = http .Get (url )
59+ resp , err = http .Get (url )
4660 if err != nil {
47- log . Fatal ( err )
61+ return "" , nil , err
4862 }
4963
5064 // Ready Body Content
@@ -98,13 +112,13 @@ func mergeFile(src string, dst string) error {
98112func main () {
99113 fmt .Println ("Go-SQLite3 Upgrade Tool" )
100114
101- wd , err := os . Getwd ( )
102- if err != nil {
103- log .Fatal (err )
115+ _ , file , _ , ok := runtime . Caller ( 0 )
116+ if ! ok {
117+ log .Fatal ("could not get current file info" )
104118 }
105- if filepath .Base ( wd ) != "upgrade" {
106- log . Printf ( "Current directory is %q but should run in upgrade directory" , wd )
107- os . Exit ( 1 )
119+ err := os . Chdir ( filepath .Dir ( filepath . Dir ( file )))
120+ if err != nil {
121+ log . Fatalf ( "could not change directory: %s" , err )
108122 }
109123
110124 // Download Amalgamation
0 commit comments