first commit
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type YahooChartResponse struct {
|
||||
Chart struct {
|
||||
Result []struct {
|
||||
Timestamp []int64 `json:"timestamp"`
|
||||
Indicators struct {
|
||||
Quote []struct {
|
||||
Close []float64 `json:"close"`
|
||||
} `json:"quote"`
|
||||
} `json:"indicators"`
|
||||
} `json:"result"`
|
||||
} `json:"chart"`
|
||||
}
|
||||
|
||||
func StockSearch(query string) (string, error) {
|
||||
url := fmt.Sprintf("https://query2.finance.yahoo.com/v1/finance/search?q=%s", query)
|
||||
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Fehler beim Erstellen der Anfrage: %v", err)
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.1 Safari/605.1.15")
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Fehler beim Abrufen der URL: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("Fehler: HTTP Status %s", resp.Status)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Fehler beim Lesen der Antwort: %v", err)
|
||||
}
|
||||
|
||||
return string(body), nil
|
||||
}
|
||||
|
||||
func FetchYahooFinanceData(stock string) (string, error) {
|
||||
url := fmt.Sprintf(
|
||||
"https://query2.finance.yahoo.com/v8/finance/chart/%s?range=1y&interval=1d&events=history",
|
||||
stock,
|
||||
)
|
||||
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Fehler beim Erstellen der Anfrage: %v", err)
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.1 Safari/605.1.15")
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Fehler beim Abrufen der URL: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("Fehler: HTTP Status %s", resp.Status)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Fehler beim Lesen der Antwort: %v", err)
|
||||
}
|
||||
|
||||
return string(body), nil
|
||||
}
|
||||
|
||||
func FetchYahooFinanceDataMax(stock string) (string, error) {
|
||||
url := fmt.Sprintf(
|
||||
"https://query2.finance.yahoo.com/v8/finance/chart/%s?range=max&interval=1mo&events=div",
|
||||
stock,
|
||||
)
|
||||
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Fehler beim Erstellen der Anfrage: %v", err)
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.1 Safari/605.1.15")
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Fehler beim Abrufen der URL: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("Fehler: HTTP Status %s", resp.Status)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Fehler beim Lesen der Antwort: %v", err)
|
||||
}
|
||||
|
||||
return string(body), nil
|
||||
}
|
||||
|
||||
// Neu: Daten für das Chart extrahieren
|
||||
func ExtractChartData(jsonStr string) ([]string, []float64, error) {
|
||||
var data YahooChartResponse
|
||||
err := json.Unmarshal([]byte(jsonStr), &data)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("Fehler beim Parsen des JSON: %v", err)
|
||||
}
|
||||
if len(data.Chart.Result) == 0 {
|
||||
return nil, nil, fmt.Errorf("Keine Daten gefunden")
|
||||
}
|
||||
timestamps := data.Chart.Result[0].Timestamp
|
||||
closes := data.Chart.Result[0].Indicators.Quote[0].Close
|
||||
|
||||
labels := make([]string, len(timestamps))
|
||||
for i, ts := range timestamps {
|
||||
labels[i] = time.Unix(ts, 0).Format("2006-01-02")
|
||||
}
|
||||
return labels, closes, nil
|
||||
}
|
||||
Reference in New Issue
Block a user