Example #1
0
func getDetails(api *stocks.API, userID stocks.UserID) []stocks.StockDetail {
	details, err := api.GetStockDetailsForUser(userID)
	if err != nil {
		panic(err)
	}
	return details
}
Example #2
0
func fetchLatest(api *stocks.API, symbols ...string) {
	// Run through each actively tracked stock and calculate stopping prices, notify next of kin, what have you...
	log.Printf("%d stocks tracked.\n", len(symbols))

	for _, symbol := range symbols {
		// Record trading history:
		log.Printf("%s: recording historical data and calculating statistics...\n", symbol)
		api.RecordHistory(symbol)
	}

	// Fetch current prices from Yahoo into the database:
	log.Printf("Fetching current prices...\n")
	api.GetCurrentHourlyPrices(true, symbols...)
}
Example #3
0
func attemptEmailUser(api *stocks.API, user *stocks.User, sd *stocks.StockDetail, lastDeliveryTime *stocks.NullDateTime, templateName string) bool {
	// Determine next available delivery time:
	nextDeliveryTime := time.Now()
	if (*lastDeliveryTime).Valid {
		nextDeliveryTime = (*lastDeliveryTime).Value.Add(user.NotificationTimeout)
	}

	// Can we deliver?
	if (*lastDeliveryTime).Valid && !time.Now().After(nextDeliveryTime) {
		log.Printf("  Not delivering notification email due to anti-spam timeout; next delivery after %s\n", nextDeliveryTime.Format(time.RFC3339))
		return false
	}

	log.Printf("  Delivering notification email to %s <%s>...\n", user.Name, user.PrimaryEmail())

	// Format mail addresses:
	from := mail.Address{"stock-watcher-" + sd.Stock.Symbol, "stock.watcher." + sd.Stock.Symbol + "@bittwiddlers.org"}
	to := mail.Address{user.Name, user.PrimaryEmail()}

	// Execute email template to get subject and body:
	subject := textTemplateString(emailTemplate, templateName+"/subject", sd)
	body := textTemplateString(emailTemplate, templateName+"/body", sd)

	// Deliver email:
	if err := mailutil.SendHtmlMessage(from, to, subject, body); err != nil {
		log.Println(err)
		log.Printf("  Failed delivering notification email.\n")
		return false
	} else {
		log.Printf("  Delivered notification email.\n")

		// Successfully delivered email as far as we know; record last delivery date/time:
		*lastDeliveryTime = stocks.NullDateTime{Value: time.Now(), Valid: true}
		api.UpdateNotifyTimes(&sd.Stock)
		return true
	}
}