Beispiel #1
0
// searchByImdbID searches on tmdb based on the imdb id
func (t *TmDB) searchByImdbID(m *polochon.Movie, log *logrus.Entry) error {
	// No imdb id, no search
	if m.ImdbID == "" {
		return ErrNoMovieImDBID
	}

	// ID already found
	if m.TmdbID != 0 {
		return nil
	}

	// Search on tmdb
	results, err := tmdbSearchByImdbID(t.client, m.ImdbID, "imdb_id", map[string]string{})
	if err != nil {
		return err
	}

	// Check if there is any results
	if len(results.MovieResults) == 0 {
		log.Debugf("Failed to find movie from imdb ID %q", m.ImdbID)
		return ErrNoMovieFound
	}

	m.TmdbID = results.MovieResults[0].ID

	log.Debugf("Found movie from imdb ID %q", m.ImdbID)

	return nil
}
Beispiel #2
0
// DefaultBefore is the default func assigned to *Middleware.Before
func DefaultBefore(entry *logrus.Entry, req *http.Request, remoteAddr string) *logrus.Entry {
	return entry.WithFields(logrus.Fields{
		"request": req.RequestURI,
		"method":  req.Method,
		"remote":  remoteAddr,
	})
}
Beispiel #3
0
/*
Judge run and judge other answers
*/
func Judge(dir, path string, answer []Answer, tests []os.FileInfo, timelimit float64, isConcurrent bool, logger *logrus.Entry) {
	logger.WithFields(logrus.Fields{
		"state": "check",
		"type":  "info",
		"info":  "check answers start"}).Info("Check answers start")

	chk, err := chkSetup(dir, path, logger)
	if err != nil {
		return
	}

	//file setup
	fileSetup(dir, tests, answer, isConcurrent, logger)

	//testing
	for k, s := range answer { //tests
		id := k + 1
		name := filepath.Base(s.Name)
		outdir := filepath.Join(dir, "temp", fmt.Sprintf("ans_%d", id))
		info := ProgInfo{Src: s.Name, Out: filepath.Join(outdir, fmt.Sprintf("ans_%d.out", id))}
		test(outdir, name, id, info, tests, timelimit, chk, isConcurrent, logger)
	}

	logger.WithFields(logrus.Fields{
		"state": "check",
		"type":  "info",
		"info":  "check answers end"}).Info("Check answers finished")
}
Beispiel #4
0
// RemoveSeason removes the season from the index
func (si *ShowIndex) RemoveSeason(show *polochon.Show, season int, log *logrus.Entry) error {
	log.Infof("Deleting whole season %d of %s from index", season, show.ImdbID)

	delete(si.shows[show.ImdbID].Seasons, season)

	return nil
}
func (s *fileSender) send(entry *logrus.Entry) (err error) {
	msg, err := entry.String()
	if err != nil {
		return
	}

	file, err := os.OpenFile(utils.GetLogPath(),
		os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666)
	if err != nil {
		err = &WriteError{
			errors.Wrap(err, "logger: Failed to open log file"),
		}
		return
	}
	defer file.Close()

	_, err = file.WriteString(msg)
	if err != nil {
		err = &WriteError{
			errors.Wrap(err, "logger: Failed to write to log file"),
		}
		return
	}

	return
}
Beispiel #6
0
// Stop stops the app
func (b *Base) Stop(log *logrus.Entry) {
	log.WithField("app", b.AppName).Debug("stopping app asynchronously")
	if b.AppStatus == Started {
		close(b.Done)
		b.AppStatus = Stopped
	}
}
Beispiel #7
0
func (h *StreamHook) Fire(entry *logrus.Entry) error {
	h.Lock()
	defer h.Unlock()

	var line string
	var err error

	if line, err = entry.String(); err != nil {
		if h.debug {
			fmt.Fprintf(os.Stderr, "Unable to read entry: %v\n", err)
		}
		return err
	}
	if h.terminator != "" {
		line += h.terminator
	}
	if _, err := h.writer.WriteString(line); err != nil {
		if h.debug {
			fmt.Fprintf(os.Stderr, "Unable to write the content: %v\n", err)
		}
		return err
	}
	if err := h.writer.Flush(); err != nil {
		if h.debug {
			fmt.Fprintf(os.Stderr, "Unable to flush the buffer: %v\n", err)
		}
		return err
	}

	return nil
}
Beispiel #8
0
func (fs *FsNotify) eventHandler(ctx polochon.FsNotifierCtx, log *logrus.Entry) {
	// Notify the waitgroup
	ctx.Wg.Add(1)
	defer ctx.Wg.Done()

	// Close the watcher when done
	defer fs.watcher.Close()

	for {
		select {
		case <-ctx.Done:
			log.Debug("fsnotify is done watching")
			return
		case ev := <-fs.watcher.Events:
			if ev.Op != fsnotify.Create && ev.Op != fsnotify.Chmod {
				continue
			}

			// Wait for the delay time before sending an event.
			// Transmission creates the folder and move the files afterwards.
			// We need to wait for the file to be moved in before sending the
			// event. Delay is the estimated time to wait.
			go func() {
				time.Sleep(DELAY)
				ctx.Event <- ev.Name
			}()
		case err := <-fs.watcher.Errors:
			log.Error(err)
		}
	}
}
Beispiel #9
0
// Fire fires the event to the ELK beat
func (elk *UDPHook) Fire(e *log.Entry) error {
	// Make a copy to safely modify
	entry := e.WithFields(nil)
	if frameNo := findFrame(); frameNo != -1 {
		t := newTrace(frameNo-1, nil)
		entry.Data[FileField] = t.String()
		entry.Data[FunctionField] = t.Func()
	}
	data, err := json.Marshal(Frame{
		Time:    elk.Clock.Now().UTC(),
		Type:    "trace",
		Entry:   entry.Data,
		Message: entry.Message,
		Level:   entry.Level.String(),
	})
	if err != nil {
		return Wrap(err)
	}

	c, err := net.ListenPacket("udp", ":0")
	if err != nil {
		return Wrap(err)
	}

	ra, err := net.ResolveUDPAddr("udp", "127.0.0.1:5000")
	if err != nil {
		return Wrap(err)
	}

	_, err = (c.(*net.UDPConn)).WriteToUDP(data, ra)
	return Wrap(err)

}
Beispiel #10
0
// GetLogrusLogger returns the logrus logger for the context. If one more keys
// are provided, they will be resolved on the context and included in the
// logger. Only use this function if specific logrus functionality is
// required.
func getLogrusLogger(ctx Context, keys ...interface{}) *logrus.Entry {
	var logger *logrus.Entry

	// Get a logger, if it is present.
	loggerInterface := ctx.Value("logger")
	if loggerInterface != nil {
		if lgr, ok := loggerInterface.(*logrus.Entry); ok {
			logger = lgr
		}
	}

	if logger == nil {
		// If no logger is found, just return the standard logger.
		logger = logrus.NewEntry(logrus.StandardLogger())
	}

	fields := logrus.Fields{}

	for _, key := range keys {
		v := ctx.Value(key)
		if v != nil {
			fields[fmt.Sprint(key)] = v
		}
	}

	return logger.WithFields(fields)
}
Beispiel #11
0
// Open the file, write to the file, close the file.
// Whichever user is running the function needs write permissions to the file or directory if the file does not yet exist.
func (hook *lfsHook) Fire(entry *logrus.Entry) error {
	var (
		fd   *os.File
		path string
		msg  string
		err  error
		ok   bool
	)
	if path, ok = hook.paths[entry.Level]; !ok {
		err = fmt.Errorf("no file provided for loglevel: %d", entry.Level)
		log.Println(err.Error())
		return err
	}
	fd, err = os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
	if err != nil {
		log.Println("failed to open logfile:", path, err)
		return err
	}
	defer fd.Close()
	msg, err = entry.String()
	if err != nil {
		log.Println("failed to generate string for entry:", err)
		return err
	}
	fd.WriteString(msg)
	return nil
}
Beispiel #12
0
// GetDetails helps getting infos for a movie
// If there is an error, it will be of type *errors.Collector
func (m *Movie) GetDetails(log *logrus.Entry) error {
	c := errors.NewCollector()

	if len(m.Detailers) == 0 {
		c.Push(errors.Wrap("No detailer available").Fatal())
		return c
	}

	var done bool
	for _, d := range m.Detailers {
		detailerLog := log.WithField("detailer", d.Name())
		err := d.GetDetails(m, detailerLog)
		if err == nil {
			done = true
			break
		}
		c.Push(errors.Wrap(err).Ctx("Detailer", d.Name()))
	}
	if !done {
		c.Push(errors.Wrap("All detailers failed").Fatal())
	}

	if c.HasErrors() {
		return c
	}

	return nil
}
Beispiel #13
0
func (k *Kickass) search(s Searcher, log *logrus.Entry) ([]polochon.Torrent, error) {
	users := s.users()
	result := []polochon.Torrent{}

	log = log.WithFields(logrus.Fields{
		"search_category": s.category(),
		"search_string":   s.searchStr(),
	})

	if err := s.validate(); err != nil {
		log.Error(err)
		return nil, err
	}

	for _, u := range users {
		torrents, err := k.searchUser(s, log, u)
		if err != nil {
			return nil, err
		}

		result = append(result, torrents...)
	}

	return polochon.FilterTorrents(result), nil
}
Beispiel #14
0
// GetSubtitle implements the subtitle interface
// If there is an error, it will be of type *errors.Collector
func (m *Movie) GetSubtitle(log *logrus.Entry) error {
	c := errors.NewCollector()

	var subtitle Subtitle
	for _, subtitler := range m.Subtitlers {
		var err error
		subtitlerLog := log.WithField("subtitler", subtitler.Name())
		subtitle, err = subtitler.GetMovieSubtitle(m, subtitlerLog)
		if err == nil {
			break
		}

		c.Push(errors.Wrap(err).Ctx("Subtitler", subtitler.Name()))
	}

	if subtitle != nil {
		file, err := os.Create(m.File.SubtitlePath())
		if err != nil {
			c.Push(errors.Wrap(err).Fatal())
			return c
		}
		defer file.Close()
		defer subtitle.Close()

		if _, err := io.Copy(file, subtitle); err != nil {
			c.Push(errors.Wrap(err).Fatal())
			return c
		}
	}

	if c.HasErrors() {
		return c
	}
	return nil
}
Beispiel #15
0
// Watch implements the modules fsNotifier interface
func (fs *FsNotify) Watch(watchPath string, ctx polochon.FsNotifierCtx, log *logrus.Entry) error {
	// Create a new watcher
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		return err
	}
	fs.watcher = watcher

	// Ensure that the watch path exists
	if _, err := os.Stat(watchPath); os.IsNotExist(err) {
		return err
	}

	log = log.WithField("module", moduleName)

	// Run the event handler
	go fs.eventHandler(ctx, log)

	// Watch the path
	if err := fs.watcher.Add(watchPath); err != nil {
		return err
	}

	return nil
}
Beispiel #16
0
func (hook *SyslogHook) Fire(entry *logrus.Entry) error {
	line, err := entry.String()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Unable to read entry, %v", err)
		return err
	}

	switch entry.Level {
	case logrus.PanicLevel:
		return hook.Writer.Crit(line)
	case logrus.FatalLevel:
		return hook.Writer.Crit(line)
	case logrus.ErrorLevel:
		return hook.Writer.Err(line)
	case logrus.WarnLevel:
		return hook.Writer.Warning(line)
	case logrus.InfoLevel:
		return hook.Writer.Info(line)
	case logrus.DebugLevel:
		return hook.Writer.Debug(line)
	case logrus.TraceLevel:
		return hook.Writer.Debug(line)
	default:
		return nil
	}
}
Beispiel #17
0
func FindPackagesById(c *gin.Context, d *FindPackagesByIdCommand, q *NuGetQuery, logger *log.Entry) {
	logger.Info("FindPackagesById")

	var match *rekt.Entry
	for _, pkg := range registry.Packages {
		if pkg.Title == d.Id && (q.Filter != "IsLatestVersion" || pkg.IsLatestVersion) {
			match = pkg
		}
	}

	feed := &rekt.Feed{
		Id:      "https://www.example.com/api/v2/FindPackagesById",
		Title:   "FindPackagesById",
		Updated: time.Now(),
		Entries: []*rekt.Entry{match},
	}

	atom, err := feed.ToAtom()
	if err != nil {
		c.Status(500)
		return
	}

	c.Data(200, AtomMimeType, atom)
	return
}
Beispiel #18
0
// GetLogrusLogger returns the logrus logger for the context. If one more keys
// are provided, they will be resolved on the context and included in the
// logger. Only use this function if specific logrus functionality is
// required.
func getLogrusLogger(ctx Context, keys ...interface{}) *logrus.Entry {
	var logger *logrus.Entry

	// Get a logger, if it is present.
	loggerInterface := ctx.Value("logger")
	if loggerInterface != nil {
		if lgr, ok := loggerInterface.(*logrus.Entry); ok {
			logger = lgr
		}
	}

	if logger == nil {
		fields := logrus.Fields{}

		// Fill in the instance id, if we have it.
		instanceID := ctx.Value("instance.id")
		if instanceID != nil {
			fields["instance.id"] = instanceID
		}

		fields["go.version"] = runtime.Version()
		// If no logger is found, just return the standard logger.
		logger = logrus.StandardLogger().WithFields(fields)
	}

	fields := logrus.Fields{}
	for _, key := range keys {
		v := ctx.Value(key)
		if v != nil {
			fields[fmt.Sprint(key)] = v
		}
	}

	return logger.WithFields(fields)
}
Beispiel #19
0
// Format formats a message from container
func (f *formatter) Format(entry *log.Entry) ([]byte, error) {
	e := entry.WithFields(log.Fields{
		"container": fmt.Sprintf("%.12s", f.containerID),
	})
	e.Message = entry.Message
	e.Level = f.level
	return f.delegate.Format(e)
}
Beispiel #20
0
// Notify sends video to the notifiers
func (o *Organizer) Notify(v polochon.Video, log *logrus.Entry) {
	log = log.WithField("function", "notify")
	for _, n := range o.config.Notifiers {
		if err := n.Notify(v, log); err != nil {
			log.Warnf("failed to send a notification from notifier: %q: %q", n.Name(), err)
		}
	}
}
// Format formats a message from container
func (f *formatter) Format(entry *log.Entry) ([]byte, error) {
	e := entry.WithFields(log.Fields{
		"container": f.container.Name.String(),
	})
	e.Message = entry.Message
	e.Level = f.level
	return f.delegate.Format(e)
}
func reportCompleted(client client.Queue, task *TaskRun, log *logrus.Entry) *updateError {
	_, err := client.ReportCompleted(task.TaskID, strconv.Itoa(task.RunID))
	if err != nil {
		log.WithField("error", err).Warn("Not able to report successful completion for task.")
		return &updateError{err: err.Error()}
	}
	return nil
}
Beispiel #23
0
// DecorateRuntimeContext appends line, file and function context to the logger
func DecorateRuntimeContext(logger *logrus.Entry) *logrus.Entry {
	if pc, file, line, ok := runtime.Caller(1); ok {
		fName := runtime.FuncForPC(pc).Name()
		return logger.WithField("file", file).WithField("line", line).WithField("func", fName)
	} else {
		return logger
	}
}
Beispiel #24
0
// DefaultAfter is the default func assigned to *Middleware.After
func DefaultAfter(entry *logrus.Entry, res negroni.ResponseWriter, latency time.Duration, name string) *logrus.Entry {
	return entry.WithFields(logrus.Fields{
		"status":      res.Status(),
		"text_status": http.StatusText(res.Status()),
		"took":        latency,
		fmt.Sprintf("measure#%s.latency", name): latency.Nanoseconds(),
	})
}
Beispiel #25
0
func (a *Access) InfoForGood(entry *logrus.Entry, req *http.Request) {
	if host := RemoteHost(req); !a.Seen(host) {
		entry.Data["comment"] = fmt.Sprintf(
			";last info-logged successful request from %s", host)
		entry.Info("")
	} else {
		entry.Debug("")
	}
}
Beispiel #26
0
func (ms *MovieSearcher) isValidGuess(guess *guessit.Response, log *logrus.Entry) bool {
	// Check if the years match
	if ms.Year != 0 && guess.Year != 0 && guess.Year != ms.Year {
		log.Infof("invalid year: guessed (%d) wanted (%d)", guess.Year, ms.Year)
		return false
	}

	return true
}
Beispiel #27
0
// Fire fires the file logger hook and logs to the file.
func (l *localFile) Fire(entry *logrus.Entry) error {
	line, err := entry.String()
	if err != nil {
		return fmt.Errorf("Unable to read entry, %v", err)
	}
	l.File.Write([]byte(line + "\n"))
	l.File.Sync()
	return nil
}
Beispiel #28
0
/*
Open opens file from path
*/
func Open(path string, logger *logrus.Entry) *os.File {
	ret, err := os.Open(path)
	if err != nil {
		logger.WithFields(logrus.Fields{
			"type":  "error",
			"error": err.Error()}).Panic("File Open Failed")
	}
	return ret
}
Beispiel #29
0
func (gc *GuiClient) Fire(entry *log.Entry) error {
	line, err := entry.String()
	if err != nil {
		return err
	}

	gc.Warnf(line)
	return nil
}
Beispiel #30
0
/*
CreateFile creates file from path
*/
func CreateFile(name string, logger *logrus.Entry) *os.File {
	ret, err := os.Create(name)
	if err != nil {
		logger.WithFields(logrus.Fields{
			"type":  "error",
			"error": err.Error()}).Panic("File Creation Failed")
	}
	return ret
}