Skip to content

Commit 2669c0d

Browse files
authored
Merge pull request #26 from ccil-kbw/csv-support
feat: remove api dependency
2 parents 6f8295f + 3bc4dbf commit 2669c0d

File tree

10 files changed

+664
-55
lines changed

10 files changed

+664
-55
lines changed

cmd/monolith/main.go

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@ package main
33
import (
44
"fmt"
55
"github.com/ccil-kbw/robot/iqama"
6-
"io"
7-
"net/http"
86
"os"
97
"os/signal"
108
"strings"
119
"time"
1210

1311
"github.com/ccil-kbw/robot/discord"
14-
v1 "github.com/ccil-kbw/robot/iqama/v1"
1512
"github.com/ccil-kbw/robot/rec"
1613
)
1714

@@ -21,7 +18,6 @@ var (
2118
// Double check on the env side if we need the feature at run time (e.g openbroadcaster is configured, proxy is required, etc)
2219
config = Config{
2320
Features: Features{
24-
Proxy: true,
2521
DiscordBot: true,
2622
Record: true,
2723
},
@@ -49,27 +45,23 @@ func main() {
4945
notifyChan := make(chan string)
5046

5147
signal.Notify(stop, os.Interrupt)
52-
53-
var prayersData *iqama.PrayersData
54-
{
55-
prayersData = iqama.StartIqamaServer()
56-
}
57-
58-
go func() {
59-
go iqama.StartRecordingScheduleServer()
60-
61-
for {
62-
in := 15 * time.Minute
63-
notifyFunc(notifyChan, prayersData, in)
64-
notifyFunc(notifyChan, prayersData, 0)
65-
time.Sleep(55 * time.Second)
48+
/*
49+
var prayersData *iqama.PrayersData
50+
{
51+
prayersData = iqama.StartIqamaServer()
6652
}
53+
go func() {
54+
go iqama.StartRecordingScheduleServer()
6755
68-
}()
56+
for {
57+
in := 15 * time.Minute
58+
notifyFunc(notifyChan, prayersData, in)
59+
notifyFunc(notifyChan, prayersData, 0)
60+
time.Sleep(55 * time.Second)
61+
}
6962
70-
if config.Features.Proxy {
71-
go proxy()
72-
}
63+
}()
64+
*/
7365

7466
var obs *rec.Recorder
7567

@@ -136,13 +128,7 @@ out:
136128

137129
// proxy, move to apis, maybe pkg/apis/proxyserver/proxyserver.go
138130
func proxy() {
139-
http.HandleFunc("/today", func(w http.ResponseWriter, r *http.Request) {
140-
fmt.Printf("request: %s, %s\n", r.Method, r.URL)
141-
_, _ = io.WriteString(w, string(v1.GetRAW()))
142-
})
143-
144-
fmt.Println("Running iqama-proxy Go server on port :3333")
145-
_ = http.ListenAndServe(":3333", nil)
131+
fmt.Println("implement me")
146132
}
147133

148134
func bot(obs *rec.Recorder, notifyChan chan string) {

iqama/v1/iqama.go

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,15 @@
11
package v1
22

33
import (
4-
"io/ioutil"
5-
"log"
6-
"net/http"
4+
"errors"
5+
"fmt"
76
)
87

98
func Get() (*Resp, error) {
10-
11-
body := GetRAW()
12-
13-
iqamaResp, err := UnmarshalResp(body)
14-
if err != nil {
15-
return nil, err
16-
}
17-
return &iqamaResp, err
9+
fmt.Println("implement me")
10+
return nil, errors.New("implement me")
1811
}
1912

2013
func GetRAW() []byte {
21-
resp, err := http.Get("https://iqama.ccil-kbw.com/iqamatimes.php")
22-
if err != nil {
23-
log.Fatalln(err)
24-
}
25-
defer resp.Body.Close()
26-
27-
body, err := ioutil.ReadAll(resp.Body)
28-
if err != nil {
29-
log.Fatalln(err)
30-
}
31-
return body
14+
panic("implement me")
3215
}

iqama/v2/csv.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package v2
2+
3+
import (
4+
"encoding/csv"
5+
"log"
6+
"os"
7+
"time"
8+
)
9+
10+
type IqamaCSV struct {
11+
filePath string
12+
iqamaTimes map[time.Time]IqamaDailyTimes
13+
}
14+
15+
func NewIqamaCSV(filePath string) Iqama {
16+
i := &IqamaCSV{filePath: filePath}
17+
if err := i.readCSV(); err != nil {
18+
log.Fatalf("Unable to read CSV file %s", filePath)
19+
}
20+
return i
21+
}
22+
23+
func (i *IqamaCSV) GetTodayTimes() (*IqamaDailyTimes, error) {
24+
// Get today's date
25+
today := time.Now().Truncate(24 * time.Hour)
26+
// Get today's iqama times
27+
times, ok := i.iqamaTimes[today]
28+
if !ok {
29+
return nil, nil
30+
}
31+
return &times, nil
32+
}
33+
34+
func (i *IqamaCSV) GetTomorrowTimes() (*IqamaDailyTimes, error) {
35+
// Get tomorrow's date
36+
tomorrow := time.Now().Add(24 * time.Hour).Truncate(24 * time.Hour)
37+
// Get tomorrow's iqama times
38+
times, ok := i.iqamaTimes[tomorrow]
39+
if !ok {
40+
return nil, nil
41+
}
42+
return &times, nil
43+
}
44+
45+
func (i *IqamaCSV) GetDiscordPrettified() string {
46+
t := toTable(i.iqamaTimes)
47+
return "```markdown\n" + t.Render() + "\n```"
48+
}
49+
50+
func (i *IqamaCSV) GetShellPrettified() string {
51+
//TODO implement me
52+
panic("implement me")
53+
}
54+
55+
func (i *IqamaCSV) readCSV() error {
56+
file, err := os.Open(i.filePath)
57+
if err != nil {
58+
log.Fatalf("Unable to open file %s", i.filePath)
59+
return err
60+
}
61+
defer file.Close()
62+
63+
reader := csv.NewReader(file)
64+
records, err := reader.ReadAll()
65+
if err != nil {
66+
log.Fatalf("Unable to parse file as CSV for %s", i.filePath)
67+
return err
68+
}
69+
70+
iqamaTimes := make(map[time.Time]IqamaDailyTimes)
71+
for _, record := range records {
72+
// Convert each record to IqamaDailyTimes and add to iqamaTimes
73+
// You need to replace the following lines with your conversion logic
74+
date, _ := time.Parse("2006-01-02", record[0]) // assuming the date is the first field in the record
75+
iqamaTimes[date] = IqamaDailyTimes{}
76+
}
77+
78+
i.iqamaTimes = iqamaTimes
79+
80+
return nil
81+
}

iqama/v2/csv_test.go

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package v2
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
"time"
7+
)
8+
9+
func TestNewIqamaCSV(t *testing.T) {
10+
type args struct {
11+
filePath string
12+
}
13+
tests := []struct {
14+
name string
15+
args args
16+
}{
17+
{
18+
name: "TestNewIqamaCSV",
19+
args: args{
20+
filePath: "test_assets/iqama_2024.csv",
21+
},
22+
},
23+
}
24+
25+
for _, tt := range tests {
26+
t.Run(tt.name, func(t *testing.T) {
27+
NewIqamaCSV(tt.args.filePath)
28+
})
29+
}
30+
}
31+
32+
func TestIqamaCSV_GetTodayTimes(t *testing.T) {
33+
type fields struct {
34+
filePath string
35+
iqamaTimes map[time.Time]IqamaDailyTimes
36+
}
37+
tests := []struct {
38+
name string
39+
fields fields
40+
want *IqamaDailyTimes
41+
wantErr bool
42+
}{
43+
{
44+
name: "TestIqamaCSV_GetTodayTimes",
45+
fields: fields{
46+
filePath: "test_assets/iqama_2024.csv",
47+
iqamaTimes: nil,
48+
},
49+
want: nil,
50+
wantErr: false,
51+
},
52+
}
53+
54+
for _, tt := range tests {
55+
t.Run(tt.name, func(t *testing.T) {
56+
i := &IqamaCSV{
57+
filePath: tt.fields.filePath,
58+
iqamaTimes: tt.fields.iqamaTimes,
59+
}
60+
got, err := i.GetTodayTimes()
61+
if (err != nil) != tt.wantErr {
62+
t.Errorf("IqamaCSV.GetTodayTimes() error = %v, wantErr %v", err, tt.wantErr)
63+
return
64+
}
65+
if !reflect.DeepEqual(got, tt.want) {
66+
t.Errorf("IqamaCSV.GetTodayTimes() = %v, want %v", got, tt.want)
67+
}
68+
})
69+
}
70+
}
71+
72+
func TestIqamaCSV_GetTomorrowTimes(t *testing.T) {
73+
type fields struct {
74+
filePath string
75+
iqamaTimes map[time.Time]IqamaDailyTimes
76+
}
77+
tests := []struct {
78+
name string
79+
fields fields
80+
want *IqamaDailyTimes
81+
wantErr bool
82+
}{
83+
{
84+
name: "TestIqamaCSV_GetTomorrowTimes",
85+
fields: fields{
86+
filePath: "test_assets/iqama_2024.csv",
87+
iqamaTimes: nil,
88+
},
89+
want: nil,
90+
wantErr: false,
91+
},
92+
}
93+
94+
for _, tt := range tests {
95+
t.Run(tt.name, func(t *testing.T) {
96+
i := &IqamaCSV{
97+
filePath: tt.fields.filePath,
98+
iqamaTimes: tt.fields.iqamaTimes,
99+
}
100+
got, err := i.GetTomorrowTimes()
101+
if (err != nil) != tt.wantErr {
102+
t.Errorf("IqamaCSV.GetTomorrowTimes() error = %v, wantErr %v", err, tt.wantErr)
103+
return
104+
}
105+
if !reflect.DeepEqual(got, tt.want) {
106+
t.Errorf("IqamaCSV.GetTomorrowTimes() = %v, want %v", got, tt.want)
107+
}
108+
})
109+
}
110+
}
111+
112+
func TestIqamaCSV_GetDiscordPrettified(t *testing.T) {
113+
type fields struct {
114+
filePath string
115+
iqamaTimes map[time.Time]IqamaDailyTimes
116+
}
117+
tests := []struct {
118+
name string
119+
fields fields
120+
want string
121+
}{
122+
{
123+
name: "TestIqamaCSV_GetDiscordPrettified",
124+
fields: fields{
125+
filePath: "test_assets/iqama_2024.csv",
126+
iqamaTimes: nil,
127+
},
128+
want: "```markdown\n+------+------+--------+-----+---------+------+\n| DATE | FAJR | DHUHUR | ASR | MAGHRIB | ISHA |\n+------+------+--------+-----+---------+------+\n+------+------+--------+-----+---------+------+\n```",
129+
},
130+
}
131+
132+
for _, tt := range tests {
133+
t.Run(tt.name, func(t *testing.T) {
134+
i := &IqamaCSV{
135+
filePath: tt.fields.filePath,
136+
iqamaTimes: tt.fields.iqamaTimes,
137+
}
138+
if got := i.GetDiscordPrettified(); got != tt.want {
139+
t.Errorf("IqamaCSV.GetDiscordPrettified() = %v, want %v", got, tt.want)
140+
}
141+
})
142+
}
143+
}

iqama/v2/helpers.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package v2
2+
3+
import (
4+
"github.com/jedib0t/go-pretty/v6/table"
5+
"os"
6+
"time"
7+
)
8+
9+
func toTable(m map[time.Time]IqamaDailyTimes) table.Writer {
10+
t := table.NewWriter()
11+
t.SetOutputMirror(os.Stdout)
12+
t.AppendHeader(table.Row{"Date", "Fajr", "Dhuhur", "Asr", "Maghrib", "Isha"})
13+
for date, times := range m {
14+
t.AppendRows([]table.Row{
15+
{date.Format("2006-01-02"), times.Fajr, times.Dhuhr, times.Asr, times.Maghrib, times.Isha},
16+
})
17+
}
18+
return t
19+
}

iqama/v2/interface.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package v2
2+
3+
type Iqama interface {
4+
GetTodayTimes() (*IqamaDailyTimes, error)
5+
GetTomorrowTimes() (*IqamaDailyTimes, error)
6+
GetDiscordPrettified() string
7+
GetShellPrettified() string
8+
}

iqama/v2/models.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package v2
2+
3+
import "time"
4+
5+
type IqamaDailyTimes struct {
6+
Date time.Time
7+
Fajr Prayer
8+
Sunrise time.Time
9+
Dhuhr Prayer
10+
Asr Prayer
11+
Maghrib Prayer
12+
Isha Prayer
13+
Jumuaa Jumuaa
14+
}
15+
16+
type Prayer struct {
17+
Adhan time.Time
18+
Iqama time.Time
19+
}
20+
21+
type Jumuaa struct {
22+
Times []time.Time
23+
}

0 commit comments

Comments
 (0)