summaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
Diffstat (limited to 'pkg')
-rw-r--r--pkg/bash/bash.go27
-rw-r--r--pkg/geo/geo.go69
-rw-r--r--pkg/weather/weather.go30
3 files changed, 126 insertions, 0 deletions
diff --git a/pkg/bash/bash.go b/pkg/bash/bash.go
new file mode 100644
index 0000000..7a377ef
--- /dev/null
+++ b/pkg/bash/bash.go
@@ -0,0 +1,27 @@
+package bash
+
+import (
+ "encoding/json"
+ "math/rand"
+ "os"
+ "time"
+)
+
+func Get() (*QuoteElem, error) {
+ f, err := os.ReadFile("db/quotes.json")
+ if err != nil {
+ return nil, err
+ }
+ quotes := []QuoteElem{}
+ if err := json.Unmarshal(f, &quotes); err != nil {
+ return nil, err
+ }
+ rand.Seed(time.Now().UnixMicro())
+ return &quotes[rand.Intn(len(quotes))], nil
+}
+
+type QuoteElem struct {
+ Num int `json:"num"`
+ Body string `json:"body"`
+ Date string `json:"date"`
+}
diff --git a/pkg/geo/geo.go b/pkg/geo/geo.go
new file mode 100644
index 0000000..0c2e9ee
--- /dev/null
+++ b/pkg/geo/geo.go
@@ -0,0 +1,69 @@
+package geo
+
+import (
+ "strconv"
+ "strings"
+
+ "github.com/serjvanilla/go-overpass"
+)
+
+var queries = map[string]string{
+ // "[out:json];relation(1673881);>>;out body;"
+ "shops": `[out:json][timeout:25];
+ (
+ nwr["shop"="mall"](around:1000, {{lat}},{{lon}});
+ nwr["shop"="supermarket"](around:1000, {{lat}},{{lon}});
+ nwr["shop"="convenience"](around:1000, {{lat}},{{lon}});
+ );
+ out body;
+ >;
+ out skel qt;`,
+ "farma": `[out:json][timeout:25];
+ (
+ nwr["amenity"="pharmacy"](around:1000, {{lat}},{{lon}});
+ );
+ out body;
+ >;
+ out skel qt;`,
+}
+
+func Search(lat, lng float64, search string) ([]Point, error) {
+ client := overpass.New()
+ query := queries[search]
+ query = strings.ReplaceAll(query, "{{lat}}", strconv.FormatFloat(lat, 'g', 5, 64))
+ query = strings.ReplaceAll(query, "{{lon}}", strconv.FormatFloat(lng, 'g', 5, 64))
+
+ result, err := client.Query(query)
+ if err != nil {
+ return nil, err
+ }
+
+ ret := make([]Point, 0, len(result.Nodes)+len(result.Ways))
+ for _, n := range result.Nodes {
+ if n.Tags["name"] == "" {
+ continue
+ }
+ ret = append(ret, Point{
+ Lat: n.Lat,
+ Lon: n.Lon,
+ Name: n.Tags["name"],
+ })
+ }
+ for _, n := range result.Ways {
+ if n.Tags["name"] == "" {
+ continue
+ }
+ ret = append(ret, Point{
+ Lat: n.Nodes[0].Lat,
+ Lon: n.Nodes[0].Lon,
+ Name: n.Tags["name"],
+ })
+ }
+ return ret, nil
+}
+
+type Point struct {
+ Lat float64
+ Lon float64
+ Name string
+}
diff --git a/pkg/weather/weather.go b/pkg/weather/weather.go
new file mode 100644
index 0000000..4674ee1
--- /dev/null
+++ b/pkg/weather/weather.go
@@ -0,0 +1,30 @@
+package weather
+
+import (
+ "fmt"
+ "io"
+ "net/http"
+ "time"
+)
+
+func Get(city string) (string, error) {
+ client := http.Client{
+ Timeout: 30 * time.Second,
+ }
+ u := fmt.Sprintf(`https://wttr.in/%s?format=%%l:+%%t+(ощущается+как+%%f)+%%c+%%C`, city)
+ req, _ := http.NewRequest(http.MethodGet, u, nil)
+ req.Header.Set("Accept-Language", "ru")
+ resp, err := client.Do(req)
+ if err != nil {
+ return "", err
+ }
+ defer resp.Body.Close()
+ if resp.StatusCode != http.StatusOK {
+ return "", fmt.Errorf("Не смог получить погоду")
+ }
+ result, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return "", err
+ }
+ return string(result), nil
+}