You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.6 KiB
Go

package diyanet
import (
"context"
"fmt"
"regexp"
"time"
"github.com/PuerkitoBio/goquery"
"prayertimes/pkg/prayer"
)
type Fetcher interface {
FetchParsed(ctx context.Context, url string) (*goquery.Document, error)
}
type Diyanet struct {
FetcherFunc func(ctx context.Context, url string) (*goquery.Document, error)
}
var reNumeric = regexp.MustCompile(`\d+`)
func validateLocation(location string) error {
if !reNumeric.MatchString(location) {
return fmt.Errorf("invalid location id")
}
return nil
}
func (d Diyanet) Get(ctx context.Context, location string) ([]prayer.Times, error) {
if err := validateLocation(location); err != nil {
return nil, fmt.Errorf("%w: %v", prayer.ErrInvalidLocation, err)
}
u := fmt.Sprintf("https://namazvakitleri.diyanet.gov.tr/en-US/%s", location)
doc, err := d.FetcherFunc(ctx, u)
if err != nil {
return nil, fmt.Errorf("failed to fetch location %q: %w", location, err)
}
var times []prayer.Times
doc.Find("#tab-1 .vakit-table tbody tr").Each(func(_ int, el *goquery.Selection) {
date := el.Find("td:first-of-type").Text()
parsedDate, err := time.Parse("02.01.2006", date)
if err != nil {
return
}
row := prayer.Times{
Date: parsedDate,
Fajr: el.Find("td:nth-of-type(2)").Text(),
Sunrise: el.Find("td:nth-of-type(3)").Text(),
Dhuhr: el.Find("td:nth-of-type(4)").Text(),
Asr: el.Find("td:nth-of-type(5)").Text(),
Maghrib: el.Find("td:nth-of-type(6)").Text(),
Isha: el.Find("td:nth-of-type(7)").Text(),
}
times = append(times, row)
})
return times, err
}
func (d Diyanet) Name() string {
return "diyanet"
}