2 Commits

Author SHA1 Message Date
25cdebd1b2 Fix Yahoo Finance API: switch from v7 quote to v8 chart endpoint
The v7 /quote endpoint returns 401 Unauthorized as it now requires
cookie-based authentication. The v8 /chart endpoint works without auth.

- Switch to v8/finance/chart/{symbol} endpoint
- Fetch each symbol individually (chart API limitation)
- Update response parsing for chart JSON structure
- Calculate price change from regularMarketPrice - chartPreviousClose

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-10 20:54:25 +00:00
9528b0b57e Add Yahoo Finance stock data integration
- Add StockDataSource to fetch quotes from Yahoo Finance API
- Add StockSettingsStore for persisting user's stock symbols
- Add StockSettingsView with UI to manage symbols (max 5)
- Add STOCK feed item type and ranker weight (0.3)
- Integrate stock fetch into ContextOrchestrator pipeline
- Stock cards appear in FYI bucket and sync to Glass via BLE

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-10 20:30:18 +00:00
7 changed files with 406 additions and 1 deletions

View File

@@ -9,6 +9,7 @@ import SwiftUI
struct ContentView: View { struct ContentView: View {
@EnvironmentObject private var orchestrator: ContextOrchestrator @EnvironmentObject private var orchestrator: ContextOrchestrator
@StateObject private var stockSettings = StockSettingsStore()
var body: some View { var body: some View {
TabView { TabView {
@@ -16,6 +17,10 @@ struct ContentView: View {
.tabItem { Label("BLE", systemImage: "dot.radiowaves.left.and.right") } .tabItem { Label("BLE", systemImage: "dot.radiowaves.left.and.right") }
OrchestratorView() OrchestratorView()
.tabItem { Label("Orchestrator", systemImage: "bolt.horizontal.circle") } .tabItem { Label("Orchestrator", systemImage: "bolt.horizontal.circle") }
NavigationStack {
StockSettingsView(store: stockSettings)
}
.tabItem { Label("Stocks", systemImage: "chart.line.uptrend.xyaxis") }
} }
.onAppear { orchestrator.start() } .onAppear { orchestrator.start() }
} }

View File

@@ -0,0 +1,177 @@
//
// StockDataSource.swift
// iris
//
import Foundation
struct StockDataSourceConfig: Sendable {
var maxSymbols: Int = 5
var cacheValiditySec: Int = 300
var ttlSec: Int = 600
init() {}
}
final class StockDataSource {
struct StockQuote: Sendable, Equatable {
let symbol: String
let shortName: String
let price: Double
let change: Double
let changePercent: Double
let marketState: String
}
struct StockData: Sendable, Equatable {
let quotes: [StockQuote]
}
struct Snapshot: Sendable {
let data: StockData
let diagnostics: [String: String]
}
enum StockError: Error, LocalizedError, Sendable {
case noSymbolsConfigured
case networkFailed(message: String, diagnostics: [String: String])
case rateLimited(diagnostics: [String: String])
case invalidResponse(diagnostics: [String: String])
var errorDescription: String? {
switch self {
case .noSymbolsConfigured:
return "No symbols configured"
case .networkFailed(let message, _):
return message
case .rateLimited:
return "Rate limited by Yahoo Finance"
case .invalidResponse:
return "Invalid response from Yahoo Finance"
}
}
}
private let config: StockDataSourceConfig
private var cache: (timestamp: Int, data: StockData)?
init(config: StockDataSourceConfig = .init()) {
self.config = config
}
func dataWithDiagnostics(symbols: [String], now: Int) async throws -> Snapshot {
var diagnostics: [String: String] = [
"now": String(now),
"symbols_requested": symbols.joined(separator: ","),
"max_symbols": String(config.maxSymbols),
]
guard !symbols.isEmpty else {
diagnostics["result"] = "no_symbols"
return Snapshot(data: StockData(quotes: []), diagnostics: diagnostics)
}
let limitedSymbols = Array(symbols.prefix(config.maxSymbols))
diagnostics["symbols_queried"] = limitedSymbols.joined(separator: ",")
if let cache = cache, now - cache.timestamp < config.cacheValiditySec {
diagnostics["source"] = "cache"
diagnostics["cache_age_sec"] = String(now - cache.timestamp)
return Snapshot(data: cache.data, diagnostics: diagnostics)
}
// Fetch each symbol using v8 chart API (supports one symbol per request)
var quotes: [StockQuote] = []
var fetchErrors: [String] = []
for symbol in limitedSymbols {
do {
if let quote = try await fetchQuote(symbol: symbol) {
quotes.append(quote)
}
} catch {
fetchErrors.append("\(symbol): \(error.localizedDescription)")
}
}
diagnostics["source"] = "network"
diagnostics["quotes_returned"] = String(quotes.count)
if !fetchErrors.isEmpty {
diagnostics["fetch_errors"] = fetchErrors.joined(separator: "; ")
}
let stockData = StockData(quotes: quotes)
cache = (timestamp: now, data: stockData)
return Snapshot(data: stockData, diagnostics: diagnostics)
}
private func fetchQuote(symbol: String) async throws -> StockQuote? {
guard let encodedSymbol = symbol.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
let url = URL(string: "https://query1.finance.yahoo.com/v8/finance/chart/\(encodedSymbol)?interval=1d&range=1d") else {
return nil
}
var request = URLRequest(url: url)
request.setValue("Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X)", forHTTPHeaderField: "User-Agent")
request.setValue("application/json", forHTTPHeaderField: "Accept")
let (data, response) = try await URLSession.shared.data(for: request)
if let httpResponse = response as? HTTPURLResponse {
guard (200..<300).contains(httpResponse.statusCode) else {
throw StockError.networkFailed(message: "HTTP \(httpResponse.statusCode)", diagnostics: [:])
}
}
let chartResponse = try JSONDecoder().decode(YahooChartResponse.self, from: data)
guard let result = chartResponse.chart.result?.first,
let meta = result.meta,
let price = meta.regularMarketPrice else {
return nil
}
let previousClose = meta.chartPreviousClose ?? meta.previousClose ?? price
let change = price - previousClose
let changePercent = previousClose > 0 ? (change / previousClose) * 100 : 0
return StockQuote(
symbol: meta.symbol ?? symbol,
shortName: meta.shortName ?? meta.symbol ?? symbol,
price: price,
change: change,
changePercent: changePercent,
marketState: meta.marketState ?? "CLOSED"
)
}
}
// MARK: - Yahoo Finance v8 Chart API Response Models
private struct YahooChartResponse: Codable {
let chart: Chart
struct Chart: Codable {
let result: [ChartResult]?
let error: ChartError?
}
struct ChartResult: Codable {
let meta: ChartMeta?
}
struct ChartMeta: Codable {
let symbol: String?
let shortName: String?
let regularMarketPrice: Double?
let chartPreviousClose: Double?
let previousClose: Double?
let marketState: String?
}
struct ChartError: Codable {
let code: String?
let description: String?
}
}

View File

@@ -90,6 +90,7 @@ final class HeuristicRanker {
case .transit: return 0.75 case .transit: return 0.75
case .poiNearby: return 0.6 case .poiNearby: return 0.6
case .info: return 0.4 case .info: return 0.4
case .stock: return 0.3
case .nowPlaying: return 0.25 case .nowPlaying: return 0.25
case .currentWeather: return 0.0 case .currentWeather: return 0.0
case .allQuiet: return 0.0 case .allQuiet: return 0.0

View File

@@ -0,0 +1,48 @@
//
// StockSettingsStore.swift
// iris
//
import Foundation
import Combine
@MainActor
final class StockSettingsStore: ObservableObject {
nonisolated static let userDefaultsKey = "iris.stock.symbols"
@Published private(set) var symbols: [String] = []
private let maxSymbols = 5
init() {
loadSymbols()
}
private func loadSymbols() {
symbols = UserDefaults.standard.stringArray(forKey: Self.userDefaultsKey) ?? []
}
func saveSymbols(_ newSymbols: [String]) {
let cleaned = newSymbols
.map { $0.uppercased().trimmingCharacters(in: .whitespacesAndNewlines) }
.filter { !$0.isEmpty }
.prefix(maxSymbols)
symbols = Array(cleaned)
UserDefaults.standard.set(symbols, forKey: Self.userDefaultsKey)
}
@discardableResult
func addSymbol(_ symbol: String) -> Bool {
guard symbols.count < maxSymbols else { return false }
let cleaned = symbol.uppercased().trimmingCharacters(in: .whitespacesAndNewlines)
guard !cleaned.isEmpty, !symbols.contains(cleaned) else { return false }
symbols.append(cleaned)
UserDefaults.standard.set(symbols, forKey: Self.userDefaultsKey)
return true
}
func removeSymbol(_ symbol: String) {
symbols.removeAll { $0 == symbol.uppercased() }
UserDefaults.standard.set(symbols, forKey: Self.userDefaultsKey)
}
}

View File

@@ -16,5 +16,6 @@ enum FeedItemType: String, Codable, CaseIterable {
case nowPlaying = "NOW_PLAYING" case nowPlaying = "NOW_PLAYING"
case currentWeather = "CURRENT_WEATHER" case currentWeather = "CURRENT_WEATHER"
case calendarEvent = "CALENDAR_EVENT" case calendarEvent = "CALENDAR_EVENT"
case stock = "STOCK"
case allQuiet = "ALL_QUIET" case allQuiet = "ALL_QUIET"
} }

View File

@@ -33,6 +33,7 @@ final class ContextOrchestrator: NSObject, ObservableObject {
private let weatherDataSource = WeatherDataSource() private let weatherDataSource = WeatherDataSource()
private let calendarDataSource = CalendarDataSource() private let calendarDataSource = CalendarDataSource()
private let poiDataSource = POIDataSource() private let poiDataSource = POIDataSource()
private let stockDataSource = StockDataSource()
private let ranker: HeuristicRanker private let ranker: HeuristicRanker
private let store: FeedStore private let store: FeedStore
private let server: LocalServer private let server: LocalServer
@@ -204,10 +205,15 @@ final class ContextOrchestrator: NSObject, ObservableObject {
async let poiResult = withTimeoutResult(seconds: 6) { async let poiResult = withTimeoutResult(seconds: 6) {
try await self.poiDataSource.data(for: location, now: nowEpoch) try await self.poiDataSource.data(for: location, now: nowEpoch)
} }
let stockSymbols = UserDefaults.standard.stringArray(forKey: StockSettingsStore.userDefaultsKey) ?? []
async let stockResult = withTimeoutResult(seconds: 6) {
try await self.stockDataSource.dataWithDiagnostics(symbols: stockSymbols, now: nowEpoch)
}
let wxRes = await weatherResult let wxRes = await weatherResult
let calRes = await calendarResult let calRes = await calendarResult
let poiRes = await poiResult let poiRes = await poiResult
let stockRes = await stockResult
func calendarTTL(endAt: Int, now: Int) -> Int { func calendarTTL(endAt: Int, now: Int) -> Int {
let ttl = endAt - now let ttl = endAt - now
@@ -223,6 +229,7 @@ final class ContextOrchestrator: NSObject, ObservableObject {
var rightNowCandidates: [HeuristicRanker.Ranked] = [] var rightNowCandidates: [HeuristicRanker.Ranked] = []
var calendarItems: [FeedItem] = [] var calendarItems: [FeedItem] = []
var poiItems: [FeedItem] = [] var poiItems: [FeedItem] = []
var stockItems: [FeedItem] = []
var weatherNowItem: FeedItem? = nil var weatherNowItem: FeedItem? = nil
var fetchFailed = false var fetchFailed = false
var wxDiagnostics: [String: String] = [:] var wxDiagnostics: [String: String] = [:]
@@ -385,13 +392,26 @@ final class ContextOrchestrator: NSObject, ObservableObject {
} }
} }
switch stockRes {
case .success(let snapshot):
for quote in snapshot.data.quotes.prefix(3) {
let item = stockQuoteToFeedItem(quote: quote, now: nowEpoch)
stockItems.append(item)
}
if !snapshot.data.quotes.isEmpty {
logger.info("stock quotes fetched count=\(snapshot.data.quotes.count)")
}
case .failure(let error):
logger.warning("stock fetch failed: \(String(describing: error), privacy: .public)")
}
let elapsedMs = Int(Date().timeIntervalSince(start) * 1000) let elapsedMs = Int(Date().timeIntervalSince(start) * 1000)
lastPipelineElapsedMs = elapsedMs lastPipelineElapsedMs = elapsedMs
lastFetchFailed = fetchFailed lastFetchFailed = fetchFailed
lastWeatherDiagnostics = wxDiagnostics lastWeatherDiagnostics = wxDiagnostics
lastCalendarDiagnostics = calDiagnostics lastCalendarDiagnostics = calDiagnostics
logger.info("pipeline right_now_candidates=\(rightNowCandidates.count) calendar_items=\(calendarItems.count) poi_items=\(poiItems.count) fetchFailed=\(fetchFailed) elapsed_ms=\(elapsedMs)") logger.info("pipeline right_now_candidates=\(rightNowCandidates.count) calendar_items=\(calendarItems.count) poi_items=\(poiItems.count) stock_items=\(stockItems.count) fetchFailed=\(fetchFailed) elapsed_ms=\(elapsedMs)")
if fetchFailed, rightNowCandidates.isEmpty, calendarItems.isEmpty, weatherNowItem == nil { if fetchFailed, rightNowCandidates.isEmpty, calendarItems.isEmpty, weatherNowItem == nil {
let fallbackFeed = store.getFeed(now: nowEpoch) let fallbackFeed = store.getFeed(now: nowEpoch)
@@ -459,6 +479,27 @@ final class ContextOrchestrator: NSObject, ObservableObject {
fyi.append(weatherNowItem) fyi.append(weatherNowItem)
} }
let fyiStocks = stockItems
.filter { $0.id != winnerItem.id }
.filter { !store.isSuppressed(id: $0.id, type: $0.type, now: nowEpoch) }
.prefix(3)
fyi.append(contentsOf: fyiStocks.map { item in
FeedItem(
id: item.id,
type: item.type,
title: item.title.truncated(maxLength: TextConstraints.titleMax),
subtitle: item.subtitle.truncated(maxLength: TextConstraints.subtitleMax),
priority: min(max(item.priority, 0.0), 1.0),
ttlSec: max(1, item.ttlSec),
condition: item.condition,
startsAt: item.startsAt,
poiType: item.poiType,
bucket: .fyi,
actions: ["DISMISS"]
)
})
let items = [winnerItem] + fyi let items = [winnerItem] + fyi
let feedEnvelope = FeedEnvelope( let feedEnvelope = FeedEnvelope(
schema: 1, schema: 1,
@@ -532,6 +573,34 @@ final class ContextOrchestrator: NSObject, ObservableObject {
} }
return "\(Int(meters.rounded())) m" return "\(Int(meters.rounded())) m"
} }
private func stockQuoteToFeedItem(quote: StockDataSource.StockQuote, now: Int) -> FeedItem {
let direction = quote.change >= 0 ? "+" : ""
let priceStr: String
if quote.price >= 100000 {
priceStr = String(format: "%.1fK", quote.price / 1000)
} else if quote.price >= 1000 {
priceStr = String(format: "%.0f", quote.price)
} else {
priceStr = String(format: "%.2f", quote.price)
}
let title = "\(quote.symbol) $\(priceStr)"
let subtitle = "\(direction)\(String(format: "%.2f", quote.change)) (\(direction)\(String(format: "%.2f", quote.changePercent))%)"
return FeedItem(
id: "stock:\(quote.symbol):\(now / 300)",
type: .stock,
title: title.truncated(maxLength: TextConstraints.titleMax),
subtitle: subtitle.truncated(maxLength: TextConstraints.subtitleMax),
priority: 0.3,
ttlSec: 600,
condition: nil,
startsAt: nil,
poiType: nil,
bucket: .fyi,
actions: ["DISMISS"]
)
}
} }
extension ContextOrchestrator: CLLocationManagerDelegate { extension ContextOrchestrator: CLLocationManagerDelegate {

View File

@@ -0,0 +1,104 @@
//
// StockSettingsView.swift
// iris
//
import SwiftUI
struct StockSettingsView: View {
@ObservedObject var store: StockSettingsStore
@State private var newSymbol: String = ""
@State private var showError: Bool = false
@State private var errorMessage: String = ""
var body: some View {
Form {
Section {
ForEach(store.symbols, id: \.self) { symbol in
HStack {
Text(symbol)
.font(.body.monospaced())
Spacer()
Button(role: .destructive) {
store.removeSymbol(symbol)
} label: {
Image(systemName: "trash")
.foregroundStyle(.red)
}
.buttonStyle(.plain)
}
}
.onDelete { indexSet in
for index in indexSet {
store.removeSymbol(store.symbols[index])
}
}
if store.symbols.count < 5 {
HStack {
TextField("Symbol (e.g. AAPL)", text: $newSymbol)
.textInputAutocapitalization(.characters)
.autocorrectionDisabled()
.font(.body.monospaced())
Button("Add") {
addSymbol()
}
.disabled(newSymbol.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
}
}
} header: {
Text("Stock Symbols")
} footer: {
Text("Enter up to 5 stock symbols. Cards appear in the FYI section.")
}
Section {
VStack(alignment: .leading, spacing: 8) {
Text("Examples:")
.font(.subheadline.bold())
Text("AAPL - Apple Inc.")
Text("GOOGL - Alphabet Inc.")
Text("^GSPC - S&P 500 Index")
Text("^DJI - Dow Jones")
Text("^IXIC - NASDAQ Composite")
}
.font(.caption)
.foregroundStyle(.secondary)
}
}
.navigationTitle("Stocks")
.alert("Error", isPresented: $showError) {
Button("OK") {}
} message: {
Text(errorMessage)
}
}
private func addSymbol() {
let cleaned = newSymbol.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
guard !cleaned.isEmpty else { return }
if store.symbols.contains(cleaned) {
errorMessage = "Symbol '\(cleaned)' already exists."
showError = true
return
}
if store.symbols.count >= 5 {
errorMessage = "Maximum of 5 symbols allowed."
showError = true
return
}
store.addSymbol(cleaned)
newSymbol = ""
}
}
struct StockSettingsView_Previews: PreviewProvider {
static var previews: some View {
NavigationStack {
StockSettingsView(store: StockSettingsStore())
}
}
}