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.
73 lines
1.8 KiB
Go
73 lines
1.8 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 Provider struct {
|
|
FetcherFunc func(ctx context.Context, url string) (*goquery.Document, error)
|
|
}
|
|
|
|
func New(fetcherFunc func(ctx context.Context, url string) (*goquery.Document, error)) *Provider {
|
|
return &Provider{FetcherFunc: fetcherFunc}
|
|
}
|
|
|
|
var reNumeric = regexp.MustCompile(`\d+`)
|
|
|
|
func validateLocation(location string) error {
|
|
if !reNumeric.MatchString(location) {
|
|
return fmt.Errorf("invalid location id")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (d Provider) 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.Format(time.DateOnly),
|
|
Fajr: el.Find("td:nth-of-type(3)").Text(),
|
|
Sunrise: el.Find("td:nth-of-type(4)").Text(),
|
|
Dhuhr: el.Find("td:nth-of-type(5)").Text(),
|
|
Asr: el.Find("td:nth-of-type(6)").Text(),
|
|
Maghrib: el.Find("td:nth-of-type(7)").Text(),
|
|
Isha: el.Find("td:nth-of-type(8)").Text(),
|
|
}
|
|
times = append(times, row)
|
|
})
|
|
|
|
return times, err
|
|
}
|
|
|
|
func (d Provider) Name() string {
|
|
return "diyanetweb"
|
|
}
|