-
Notifications
You must be signed in to change notification settings - Fork 0
/
location.go
118 lines (106 loc) · 2.25 KB
/
location.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package serendipity
import (
"fmt"
"log"
)
func (r *Serendipity) Street() string {
return fmt.Sprintf("%s %s", r.FamilyName(), r.StreetSuffix())
}
func (r *Serendipity) StreetSuffix() string {
obj, err := r.loadStrings("/street_suffix.txt")
if err != nil {
log.Println(err)
return ""
}
count := len(*obj)
if count == 0 {
return ""
}
i := r.N(0, count-1)
return (*obj)[i]
}
func (r *Serendipity) Locality() string {
obj, err := r.loadStrings("/locality-GB.txt")
if err != nil {
log.Println(err)
return ""
}
count := len(*obj)
if count == 0 {
return ""
}
i := r.N(0, count-1)
return (*obj)[i]
}
func (r *Serendipity) Region() string {
obj, err := r.loadStrings("/region-GB.txt")
if err != nil {
log.Println(err)
return ""
}
count := len(*obj)
if count == 0 {
return ""
}
i := r.N(0, count-1)
return (*obj)[i]
}
func (r *Serendipity) PostalCode() string {
from := "ABCDEFGHJKLMNPRSTUVWXYZ"
text := fmt.Sprintf("%s%s%d %d%s%s", r.Char(from), r.Char(from), r.N(1, 59), r.N(1, 9), r.Char(from), r.Char(from))
return text
}
func (r *Serendipity) Country() *Data {
obj, err := r.loadData("/country.json")
if err != nil {
log.Println(err)
return nil
}
count := len(*obj)
if count == 0 {
return nil
}
i := r.N(0, count-1)
return &(*obj)[i]
}
func (r *Serendipity) Latitude() float64 {
min, max := float64(-90), float64(90)
return min + r.Float64()*(max-min)
}
func (r *Serendipity) Longitude() float64 {
min, max := float64(-180), float64(180)
return min + r.Float64()*(max-min)
}
func (r *Serendipity) Timezone() string {
obj, err := r.loadStrings("/timezone.txt")
if err != nil {
log.Println(err)
return ""
}
count := len(*obj)
if count == 0 {
return ""
}
i := r.N(0, count-1)
return (*obj)[i]
}
type AddressInfo struct {
Street string `json:"street_address"`
Locality string `json:"locality"`
Region string `json:"region"`
PostalCode string `json:"postal_code"`
Country string `json:"country"`
}
func (r *Serendipity) Address() *AddressInfo {
ctry := r.Country()
addr := AddressInfo{
Street: fmt.Sprintf("%d %s", r.N(1, 2999), r.Street()),
Locality: r.Locality(),
Region: r.Region(),
PostalCode: r.PostalCode(),
}
if ctry != nil {
addr.Country = ctry.Name
}
return &addr
}