Jason Moiron avatar Jason Moiron committed b971fec

add a log, accessible with --log; ukiyo is now pretty safe to run headless in cron a couple times a day -ud

Comments (0)

Files changed (4)

 	"os"
 	"path/filepath"
 	"runtime"
+	"time"
 )
 
 type Xdg struct {
 	Numberf float64
 }
 
+type Log struct {
+	Timestamp int64
+	Message   string
+}
+
 var xdg Xdg
 var config Config
 
 	Exec(c.db, "update sites set priority=? where name=?", priority, name)
 }
 
+func (c *Config) Log(message string) error {
+	now := time.Now().Unix()
+	_, err := c.db.Exec("insert into log (timestamp, message) values (?, ?)",
+		now, message)
+	return err
+}
+
+func (c *Config) GetLog(numlines int) []*Log {
+	messages := make([]*Log, 0)
+	rows, err := c.db.Query("SELECT timestamp, message FROM log ORDER BY timestamp ASC LIMIT ?", numlines)
+	if err != nil {
+		fmt.Printf("Error occured fetching log: %s\n", err)
+		return messages
+	}
+	for rows.Next() {
+		m := new(Log)
+		err = rows.Scan(&m.Timestamp, &m.Message)
+		if err != nil {
+			fmt.Printf("Error occured scanning message: %s\n", err)
+		} else {
+			messages = append(messages, m)
+		}
+	}
+	return messages
+}
+
 // Convenient interface for fetching a list of series objects from the db
 func QuerySeries(db *sql.DB, query string, args ...interface{}) []*Series {
 	series := make([]*Series, 0)
 	"strings"
 )
 
+var EnableHttpCache = false
+
 var httpClient = &http.Client{
 	// keep user-agent:
 	// https://groups.google.com/forum/?fromgroups#!topic/golang-nuts/OwGvopYXpwE%5B1-25%5D
 
 	var body []byte
 
-	if len(cachefile) > 0 && exists(cachefile) {
+	if len(cachefile) > 0 && exists(cachefile) && EnableHttpCache {
 		if opts.Verbose {
 			fmt.Printf("Reading in cached body for %s (in %s)\n", url, cachefile)
 		}
 	Sync                 bool
 	Search               bool
 	Show                 bool
+	Log                  bool
 	Filter               *Filter
 }
 
 		return
 	}
 
+	if opts.Log {
+		if len(optarg.Remainder) > 1 {
+			fmt.Printf("--log takes one optional argument (number of lines)\n")
+			return
+		}
+		ShowLog(optarg.Remainder...)
+		return
+	}
+
 	if opts.List {
 		ShowWatchlist()
 		return
 
 	if opts.Update {
 		Update()
-		return
 	}
 
 	if opts.Search {
 	w.Update(opts.Force)
 }
 
+func ShowLog(num ...string) {
+	nlines := 20
+	if len(num) == 1 {
+		nlines, _ = strconv.Atoi(num[0])
+	}
+	messages := config.GetLog(nlines)
+	for _, m := range messages {
+		fmt.Printf("%s: %s\n", toDate(m.Timestamp), m.Message)
+	}
+}
+
 func DownloadNewChapters() {
 	w := new(Watchlist)
 	updates := w.FindNew()
 		fmt.Sprintf("Change destination for sync and downloads. (Current: %s)", config.DownloadPath), "")
 	optarg.Add("", "toggle-use-underscores",
 		fmt.Sprintf("Use undescores instead of spaces for downloads. (Current: %v)", config.UseUnderscores), false)
+	optarg.Add("", "log", "Show the download log", false)
 
 	optarg.Header("Sites")
 	optarg.Add("", "sites", "List sites.", false)
 			opts.SetDownloadPath = opt.String()
 		case "toggle-use-underscores":
 			opts.ToggleUseUnderscores = opt.Bool()
+		case "log":
+			opts.Log = opt.Bool()
 		// sites
 		case "sites":
 			opts.ListSites = opt.Bool()
 	ZipDir(destpath, destzip)
 	os.RemoveAll(destpath)
 
+	config.Log(fmt.Sprintf("Downloaded %s %s from %s", chapter.Series, chapter.Number, site))
+
 	return nil
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.