-
Notifications
You must be signed in to change notification settings - Fork 9
/
gdocs.go
46 lines (36 loc) · 1.03 KB
/
gdocs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package gdocs
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)
// Download Simply download the doc from the given URL and run a bunch of sanity checks
// before dumping it into the filesystem.
func Download(url string, filename string, timeout int64) error {
log.Println("Downloading", url, "...")
client := http.Client{
Timeout: time.Duration(timeout * int64(time.Second)),
}
resp, err := client.Get(url)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return fmt.Errorf("Response from the Google Docs URL was %d, expecting 200", resp.StatusCode)
}
if resp.Header["Content-Type"][0] != "text/tab-separated-values" {
return fmt.Errorf("The file we downloaded has content type '%s', while we expected 'text/tab-separated-values'. Are you sure you entered the right URL?", resp.Header["Content-Type"])
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = ioutil.WriteFile(filename, b, 0644)
if err != nil {
return err
}
log.Printf("Doc downloaded in %s", filename)
return nil
}