Add files via upload

This commit is contained in:
Tomáš Dvořák
2025-05-23 09:00:43 +02:00
committed by GitHub
parent f8432881e5
commit 61fec5aa97
+54 -55
View File
@@ -12,6 +12,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/fsnotify/fsnotify"
"github.com/xuri/excelize/v2" "github.com/xuri/excelize/v2"
) )
@@ -20,7 +21,7 @@ type Contact struct {
Position string `json:"position"` Position string `json:"position"`
Phone string `json:"phone,omitempty"` Phone string `json:"phone,omitempty"`
ServicePhone string `json:"service_phone,omitempty"` ServicePhone string `json:"service_phone,omitempty"`
Table int `json:"table"` // 1 for first table, 2 for second table Table int `json:"table"`
} }
type ContactData struct { type ContactData struct {
@@ -32,21 +33,42 @@ type ContactData struct {
var ( var (
currentData *ContactData currentData *ContactData
dataFile = "data/contacts.json" dataFile = "data/contacts.json"
xlsxFile = "contacts.xlsx" xlsxFile = "TelefonniSeznamWeb.xlsx"
) )
func startAutoReload() { func startAutoReload() {
ticker := time.NewTicker(3 * 24 * time.Hour) // Create new watcher
quit := make(chan struct{}) watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Printf("Error creating file watcher: %v", err)
return
}
defer watcher.Close()
// Add the xlsx file to watcher
err = watcher.Add(xlsxFile)
if err != nil {
log.Printf("Error watching file: %v", err)
return
}
// Start watching for changes
go func() { go func() {
for { for {
select { select {
case <-ticker.C: case event, ok := <-watcher.Events:
log.Println("Auto-reloading contact data...") if !ok {
loadData() return
case <-quit: }
ticker.Stop() if event.Has(fsnotify.Write) {
return log.Println("Detected file change, reloading data...")
loadData()
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Printf("Watcher error: %v", err)
} }
} }
}() }()
@@ -193,20 +215,15 @@ func parseExcelFile(filename string) ([]Contact, error) {
} }
sheetName := sheets[0] sheetName := sheets[0]
var contacts []Contact
// Parse first table (A-D columns) // Parse single table (A-E columns)
contacts = append(contacts, parseTable(f, sheetName, "A", "D", 1)...) contacts := parseTable(f, sheetName, "A", "E", 1)
// Parse second table (F-H columns)
contacts = append(contacts, parseTable(f, sheetName, "F", "H", 2)...)
return contacts, nil return contacts, nil
} }
func parseTable(f *excelize.File, sheetName, startCol, endCol string, tableNum int) []Contact { func parseTable(f *excelize.File, sheetName, startCol, endCol string, tableNum int) []Contact {
var contacts []Contact var contacts []Contact
var currentContact *Contact
// Get all rows in the sheet // Get all rows in the sheet
rows, err := f.GetRows(sheetName) rows, err := f.GetRows(sheetName)
@@ -215,19 +232,14 @@ func parseTable(f *excelize.File, sheetName, startCol, endCol string, tableNum i
return contacts return contacts
} }
// Skip header rows (first 3 rows based on your description) // Skip header rows (first row is header)
startRow := 3 startRow := 1
if len(rows) <= startRow { if len(rows) <= startRow {
return contacts return contacts
} }
// Column indices // Column indices (A-E: name, position, phone, service_phone, extension)
var nameCol, positionCol, phoneCol, servicePhoneCol int nameCol, positionCol, phoneCol, servicePhoneCol, extensionCol := 0, 1, 2, 3, 4
if tableNum == 1 {
nameCol, positionCol, phoneCol, servicePhoneCol = 0, 1, 2, 3 // A, B, C, D
} else {
nameCol, positionCol, phoneCol = 5, 6, 7 // F, G, H
}
for i := startRow; i < len(rows); i++ { for i := startRow; i < len(rows); i++ {
row := rows[i] row := rows[i]
@@ -237,20 +249,11 @@ func parseTable(f *excelize.File, sheetName, startCol, endCol string, tableNum i
continue continue
} }
// Check for "Aktualizace" - end of data
if len(row) > nameCol && strings.Contains(strings.ToLower(row[nameCol]), "aktualizace") {
break
}
// Check for special formatting rows (like "*02(xx)")
if len(row) > positionCol && strings.Contains(row[positionCol], "*") {
continue
}
name := strings.TrimSpace(row[nameCol]) name := strings.TrimSpace(row[nameCol])
position := "" position := ""
phone := "" phone := ""
servicePhone := "" servicePhone := ""
extension := ""
if len(row) > positionCol { if len(row) > positionCol {
position = strings.TrimSpace(row[positionCol]) position = strings.TrimSpace(row[positionCol])
@@ -258,37 +261,33 @@ func parseTable(f *excelize.File, sheetName, startCol, endCol string, tableNum i
if len(row) > phoneCol { if len(row) > phoneCol {
phone = strings.TrimSpace(row[phoneCol]) phone = strings.TrimSpace(row[phoneCol])
} }
if tableNum == 1 && len(row) > servicePhoneCol { if len(row) > servicePhoneCol {
servicePhone = strings.TrimSpace(row[servicePhoneCol]) servicePhone = strings.TrimSpace(row[servicePhoneCol])
} }
if len(row) > extensionCol {
extension = strings.TrimSpace(row[extensionCol])
}
// Clean phone numbers // Clean phone numbers
phone = cleanPhoneNumber(phone) phone = cleanPhoneNumber(phone)
servicePhone = cleanPhoneNumber(servicePhone) servicePhone = cleanPhoneNumber(servicePhone)
// If we have a name, start a new contact // Combine extension with service phone if both exist
if name != "" && !strings.Contains(name, "(") { if servicePhone != "" && extension != "" {
currentContact = &Contact{ servicePhone = fmt.Sprintf("%s (%s)", servicePhone, extension)
} else if extension != "" {
servicePhone = extension
}
// If we have a name, create new contact
if name != "" {
contacts = append(contacts, Contact{
Name: name, Name: name,
Position: position, Position: position,
Phone: phone, Phone: phone,
ServicePhone: servicePhone, ServicePhone: servicePhone,
Table: tableNum, Table: tableNum,
} })
contacts = append(contacts, *currentContact)
} else if currentContact != nil {
// This is additional data for the current contact
newContact := *currentContact
if position != "" {
newContact.Position = position
}
if phone != "" {
newContact.Phone = phone
}
if servicePhone != "" {
newContact.ServicePhone = servicePhone
}
contacts = append(contacts, newContact)
} }
} }