-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags_seq.go
62 lines (52 loc) · 1.34 KB
/
flags_seq.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Download flags of top 20 countries by population
//
// Sequential version with no error checking.
//
// Sample run:
//
// $ go run flags_seq.go
// BD BR CD CN DE EG ET FR ID IN IR JP MX NG PH PK RU TR US VN
// 20 flags downloaded in 3.86s
package main
import (
"fmt"
"io/ioutil"
"net/http"
"sort"
"strings"
"time"
)
// country codes of the 20 most populous countries in 2014
const POP20_CC = "CN IN US ID BR PK NG BD RU JP MX PH VN ET EG DE IR TR CD FR"
const BASE_URL = "http://flupy.org/data/flags"
const DEST_DIR = "downloads/"
func saveFlag(img []byte, filename string) {
path := fmt.Sprintf("%v%v", DEST_DIR, filename)
ioutil.WriteFile(path, img, 0660)
}
func getFlag(cc string) []byte {
url := fmt.Sprintf("%[1]v/%[2]v/%[2]v.gif", BASE_URL, strings.ToLower(cc))
resp, _ := http.Get(url)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
return body
}
func downloadOne(cc string) {
image := getFlag(cc)
saveFlag(image, fmt.Sprintf("%v.gif", strings.ToLower(cc)))
}
func DownloadMany(cc_list []string) int {
sort.Strings(cc_list)
for _, cc := range cc_list {
downloadOne(cc)
fmt.Print(cc, " ")
}
return len(cc_list)
}
func main() {
t0 := time.Now()
count := DownloadMany(strings.Split(POP20_CC, " "))
elapsed := time.Since(t0)
msg := "\n%d flags downloaded in %.2fs\n"
fmt.Printf(msg, count, elapsed.Seconds())
}