mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-12 13:46:51 +00:00
improve error handling
This commit is contained in:
parent
171a5e6515
commit
677e623edf
1 changed files with 67 additions and 30 deletions
|
|
@ -14,78 +14,115 @@ import (
|
||||||
func (bs Service) ParseFromApi(rawContent string) (*Booking, error) {
|
func (bs Service) ParseFromApi(rawContent string) (*Booking, error) {
|
||||||
content := strings.ReplaceAll(strings.TrimSpace(rawContent), "\u00a0", " ")
|
content := strings.ReplaceAll(strings.TrimSpace(rawContent), "\u00a0", " ")
|
||||||
|
|
||||||
arrivalDate := extractDate(`Date d'arrivée `, content)
|
arrivalDate, err := extractDate(`Date d'arrivée `, content)
|
||||||
departureDate := extractDate(`Date de départ `, content)
|
if err != nil {
|
||||||
stayLength := extractInt(`Durée de séjour : (\d+) nuits`, content)
|
return nil, err
|
||||||
customerName := extractString(`Nom du client : \n\s+([\w\s]+)`, content)
|
}
|
||||||
customerName = strings.SplitN(customerName, "\n", 2)[0]
|
|
||||||
customerEmail := extractString(`[\w\.\-]+@[\w\.\-]+\.\w+`, content)
|
departureDate, err := extractDate(`Date de départ `, content)
|
||||||
customerNumber := extractInt(`Nombre de personnes : \s*\n\s*(\d+)`, content)
|
if err != nil {
|
||||||
commissionAmount := extractFloat(`Commission : € (\d+,\d+)`, content)
|
return nil, err
|
||||||
itemName := extractString(`Maison . Chambre. \((T2|T3) -`, content)
|
}
|
||||||
externalId := extractString(`Numéro de réservation : \n\s+(\d+)`, content)
|
|
||||||
standardRate := extractFloat(`Standard Rate\n\s+€ (\d+)`, content)
|
stayLength, err := extractInt(`Durée de séjour : (\d+) nuits`, content)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
customerName, err := extractString(`Nom du client : \n\s+([\w\s]+)`, content)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
customerName = strings.SplitN(customerName, "\n", 2)[0]
|
||||||
|
customerEmail, err := extractString(`[\w\.\-]+@[\w\.\-]+\.\w+`, content)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
customerNumber, err := extractInt(`Nombre de personnes : \s*\n\s*(\d+)`, content)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
commissionAmount, err := extractFloat(`Commission : € (\d+,\d+)`, content)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
itemName, err := extractString(`Maison . Chambre. \((T2|T3) -`, content)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
externalId, err := extractString(`Numéro de réservation : \n\s+(\d+)`, content)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
standardRate, err := extractFloat(`Standard Rate\n\s+€ (\d+)`, content)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
log.Println(arrivalDate, departureDate, stayLength, customerName, customerEmail, customerNumber, commissionAmount, itemName, externalId, standardRate)
|
|
||||||
b := bs.Create(*formatDate(arrivalDate), *formatDate(departureDate), customerName, "", customerEmail, "Booking", customerNumber, commissionAmount, &externalId)
|
b := bs.Create(*formatDate(arrivalDate), *formatDate(departureDate), customerName, "", customerEmail, "Booking", customerNumber, commissionAmount, &externalId)
|
||||||
if item, ok := config.NewHost().Items[itemName]; ok {
|
if item, ok := config.NewHost().Items[itemName]; ok {
|
||||||
bs.CreateItem(b.Id, item, stayLength, standardRate, "Card", customerNumber, b.Platform)
|
bs.CreateItem(b.Id, item, stayLength, standardRate, "Card", customerNumber, b.Platform)
|
||||||
}
|
}
|
||||||
log.Println(b)
|
|
||||||
|
|
||||||
return b, nil
|
return b, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractDate(pattern, content string) string {
|
func extractDate(pattern, content string) (string, error) {
|
||||||
re := regexp.MustCompile(pattern + `(lun|mar|mer|jeu|ven|sam|dim)\. \d{1,2} (janv|févr|mars|avr|mai|juin|juil|août|sept|oct|nov|déc)\.? \d{4}`)
|
re := regexp.MustCompile(pattern + `(lun|mar|mer|jeu|ven|sam|dim)\. \d{1,2} (janv|févr|mars|avr|mai|juin|juil|août|sept|oct|nov|déc)\.? \d{4}`)
|
||||||
dateMatch := re.FindString(content)
|
dateMatch := re.FindString(content)
|
||||||
|
|
||||||
if dateMatch == "" {
|
if dateMatch == "" {
|
||||||
log.Println("date not found")
|
return "", fmt.Errorf("date not found")
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Regular expression to remove the prefix
|
// Regular expression to remove the prefix
|
||||||
rePrefix := regexp.MustCompile(pattern + `\w+\.\s*`)
|
rePrefix := regexp.MustCompile(pattern + `\w+\.\s*`)
|
||||||
dateString := rePrefix.ReplaceAllString(dateMatch, "")
|
dateString := rePrefix.ReplaceAllString(dateMatch, "")
|
||||||
return dateString
|
return dateString, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractInt(pattern, content string) int {
|
func extractInt(pattern, content string) (int, error) {
|
||||||
re := regexp.MustCompile(pattern)
|
re := regexp.MustCompile(pattern)
|
||||||
match := re.FindStringSubmatch(content)
|
match := re.FindStringSubmatch(content)
|
||||||
if len(match) > 1 {
|
if len(match) > 1 {
|
||||||
val, err := strconv.Atoi(match[1])
|
val, err := strconv.Atoi(match[1])
|
||||||
if err == nil {
|
if err != nil {
|
||||||
return val
|
return 0, err
|
||||||
}
|
}
|
||||||
|
return val, nil
|
||||||
}
|
}
|
||||||
return 0
|
return 0.0, fmt.Errorf("no match for %s", pattern)
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractFloat(pattern, content string) float64 {
|
func extractFloat(pattern, content string) (float64, error) {
|
||||||
re := regexp.MustCompile(pattern)
|
re := regexp.MustCompile(pattern)
|
||||||
match := re.FindStringSubmatch(content)
|
match := re.FindStringSubmatch(content)
|
||||||
if len(match) > 1 {
|
if len(match) > 1 {
|
||||||
val, err := strconv.ParseFloat(strings.ReplaceAll(match[1], ",", "."), 64)
|
val, err := strconv.ParseFloat(strings.ReplaceAll(match[1], ",", "."), 64)
|
||||||
if err == nil {
|
if err != nil {
|
||||||
return val
|
return 0, err
|
||||||
}
|
}
|
||||||
|
return val, nil
|
||||||
}
|
}
|
||||||
return 0.0
|
return 0.0, fmt.Errorf("no match for %s", pattern)
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractString(pattern, content string) string {
|
func extractString(pattern, content string) (string, error) {
|
||||||
re := regexp.MustCompile(pattern)
|
re := regexp.MustCompile(pattern)
|
||||||
match := re.FindStringSubmatch(content)
|
match := re.FindStringSubmatch(content)
|
||||||
log.Println(match)
|
log.Println(match)
|
||||||
if len(match) > 1 {
|
if len(match) > 1 {
|
||||||
return strings.TrimSpace(match[1])
|
return strings.TrimSpace(match[1]), nil
|
||||||
} else if len(match) > 0 {
|
} else if len(match) > 0 {
|
||||||
return strings.TrimSpace(match[0])
|
return strings.TrimSpace(match[0]), nil
|
||||||
}
|
}
|
||||||
log.Println("pattern not found")
|
return "", fmt.Errorf("pattern %s not found", pattern)
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatDate(date string) *time.Time {
|
func formatDate(date string) *time.Time {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue