github.com/omniscale/go-osm@v0.3.1/replication/diff/example_test.go (about) 1 package diff_test 2 3 import ( 4 "io/ioutil" 5 "log" 6 "os" 7 "time" 8 9 "github.com/omniscale/go-osm/replication/diff" 10 ) 11 12 func Example() { 13 // This example shows how to automatically download OSM diff files. 14 15 // We store all diffs in a temporary directory for this example. 16 diffDir, err := ioutil.TempDir("", "") 17 if err != nil { 18 log.Fatal("unable to create diff dir:", err) 19 } 20 defer os.RemoveAll(diffDir) 21 22 // Where do we fetch our diffs from? 23 url := "https://planet.openstreetmap.org/replication/minute/" 24 25 // Query the ID of the latest diff. 26 seqID, err := diff.CurrentSequence(url) 27 if err != nil { 28 log.Fatal("unable to fetch current sequence:", err) 29 } 30 31 // Start downloader to fetch diffs. Start with a previous sequence ID, so 32 // that we don't have to wait till the files are available. 33 dl := diff.NewDownloader(diffDir, url, seqID-5, time.Minute) 34 35 // Iterate all diffs as they are downloaded 36 downloaded := 0 37 for seq := range dl.Sequences() { 38 39 if seq.Error != nil { 40 // Error is set if an error occurred during download (network issues, etc.). 41 // Filename and Time is not set, but you can access the Sequence and Error. 42 log.Printf("error while downloading diff #%d: %s", seq.Sequence, seq.Error) 43 // The downloader automatically retries after a short delay, so we 44 // can just continue. 45 continue 46 } 47 48 downloaded++ 49 50 // seq contains the Filename of the downloaded diff file. You can use parser/diff to parse the content. 51 log.Printf("downloaded diff #%d with changes till %s to %s", seq.Sequence, seq.Time, seq.Filename) 52 53 if downloaded == 3 { 54 // Stop downloading after 3 diffs for this example. 55 // (Stop() closes the channel from dl.Sequences and for our loop will stop). 56 dl.Stop() 57 } 58 } 59 // Output: 60 }