Files
tm_back/ted/calendar.go
T
n.nakhostin 7b09291271 Improve Error Handling in GetOJS Function
- Enhanced the GetOJS function to include error handling for reading the response body and parsing CSV records, ensuring that errors are properly returned instead of being ignored.
- Added a check to ensure that the CSV records contain data, returning a meaningful error if the calendar is empty, which improves robustness and clarity in error reporting.
2025-10-20 11:42:08 +03:30

58 lines
996 B
Go

package ted
import (
"bytes"
"encoding/csv"
"fmt"
"io"
"net/http"
"strings"
)
const (
calendarUrl = "%s/en/release-calendar/-/download/file/CSV/%d"
)
// Get OJS Number
func GetOJS(baseURL string, year int, date string) (string, error) {
var (
ojs string
)
url := fmt.Sprintf(calendarUrl, baseURL, year)
resp, err := http.Get(url)
if err != nil {
return ojs, err
}
defer resp.Body.Close()
// read CSV
body, err := io.ReadAll(resp.Body)
if err != nil {
return ojs, err
}
reader := csv.NewReader(bytes.NewReader(body))
records, err := reader.ReadAll()
if err != nil {
return ojs, err
}
if len(records) < 2 {
return ojs, fmt.Errorf("ojs calendar is empty")
}
for _, row := range records[1:] {
rowDate := strings.TrimSpace(row[1])
if rowDate == strings.TrimSpace(date) {
ojs = fmt.Sprintf("%v00%v", year, strings.TrimSpace(fmt.Sprint(row[0])))
break
}
}
if ojs == "" {
return ojs, fmt.Errorf("can not find ojs number")
}
return ojs, nil
}