feat: Add Diyanet API provider
feat: Add UI
This commit is contained in:
1 parent
d4ff42387f
commit
93af84cef8
17 files changed
+642
-43
No files matched your search
@@ -0,0 +1,100 @@
|
||||
package diyanetapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/imroc/req/v3"
|
||||
|
||||
"prayertimes/pkg/prayer"
|
||||
)
|
||||
|
||||
type Provider struct {
|
||||
http *req.Client
|
||||
}
|
||||
|
||||
func New(c *req.Client) Provider {
|
||||
return Provider{
|
||||
http: c.Clone().SetCommonBasicAuth("diyanet", "Q6Y3vYt5F3x2txPaaMF3uPgbK99EJhpM"),
|
||||
}
|
||||
}
|
||||
|
||||
func (d Provider) GetByCoords(ctx context.Context, coords prayer.Coordinates) ([]prayer.Times, error) {
|
||||
res, err := d.http.NewRequest().
|
||||
SetContext(ctx).
|
||||
SetQueryParams(map[string]string{
|
||||
"latitude": fmt.Sprintf("%f", coords.Latitude),
|
||||
"longitude": fmt.Sprintf("%f", coords.Longitude),
|
||||
}).
|
||||
Get("https://namazvakti.diyanet.gov.tr/api/NamazVakti/Aylik")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get prayer times by coords: %w", err)
|
||||
}
|
||||
|
||||
return d.parseResponse(res)
|
||||
}
|
||||
|
||||
func (d Provider) Get(ctx context.Context, locationID string) ([]prayer.Times, error) {
|
||||
res, err := d.http.NewRequest().
|
||||
SetContext(ctx).
|
||||
SetQueryParam("ilceId", locationID).
|
||||
Get("https://namazvakti.diyanet.gov.tr/api/NamazVakti/Aylik")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get prayer times by location id: %w", err)
|
||||
}
|
||||
|
||||
return d.parseResponse(res)
|
||||
}
|
||||
|
||||
func (d Provider) parseResponse(res *req.Response) ([]prayer.Times, error) {
|
||||
var response struct {
|
||||
Success bool `json:"success"`
|
||||
ResultObject struct {
|
||||
Location struct {
|
||||
ID int `json:"konum_Id"`
|
||||
Timezone string `json:"timezone"`
|
||||
} `json:"konum"`
|
||||
PrayerTimes []struct {
|
||||
Date time.Time `json:"miladi_tarih_uzun_Iso8601"`
|
||||
Fajr time.Time `json:"imsak"`
|
||||
Sunrise time.Time `json:"gunes"`
|
||||
Dhuhr time.Time `json:"ogle"`
|
||||
Asr time.Time `json:"ikindi"`
|
||||
Maghrib time.Time `json:"aksam"`
|
||||
Isha time.Time `json:"yatsi"`
|
||||
} `json:"namazVakti"`
|
||||
} `json:"resultObject"`
|
||||
}
|
||||
if err := res.Unmarshal(&response); err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal as json: %w", err)
|
||||
}
|
||||
if !response.Success {
|
||||
return nil, fmt.Errorf("received error: %s", res.String())
|
||||
}
|
||||
|
||||
var times []prayer.Times
|
||||
const format = "15:04"
|
||||
now := time.Now()
|
||||
for _, pt := range response.ResultObject.PrayerTimes {
|
||||
then := pt.Date.UTC().Truncate(time.Hour * 24)
|
||||
if then.Before(now) {
|
||||
continue
|
||||
}
|
||||
|
||||
times = append(times, prayer.Times{
|
||||
Date: then,
|
||||
Fajr: pt.Fajr.Format(format),
|
||||
Sunrise: pt.Sunrise.Format(format),
|
||||
Dhuhr: pt.Dhuhr.Format(format),
|
||||
Asr: pt.Asr.Format(format),
|
||||
Maghrib: pt.Maghrib.Format(format),
|
||||
Isha: pt.Isha.Format(format),
|
||||
})
|
||||
}
|
||||
return times, nil
|
||||
}
|
||||
|
||||
func (d Provider) Name() string {
|
||||
return "diyanet"
|
||||
}
|
||||
Reference in new issue
Block a user