Beispiel #1
0
// NewNotifier returns a new instance of Notifier.
func NewNotifier() *Notifier {
	n := &Notifier{Notifier: twitter.NewNotifier()}
	n.Notifier.Client = &n.Client
	return n
}
Beispiel #2
0
// Run executes the program.
func (m *Main) Run() error {
	logger := log.New(m.Stderr, "", log.LstdFlags)

	// Validate options.
	if m.DataDir == "" {
		return errors.New("data directory required")
	}

	// Create base directory, if not exists.
	if err := os.MkdirAll(m.DataDir, 0777); err != nil {
		return err
	}

	// Open data store.
	m.store = scuttlebutt.NewStore(filepath.Join(m.DataDir, "db"))
	m.store.RemoteStore = github.NewStore(m.Config.GitHub.Token)
	if err := m.store.Open(); err != nil {
		return fmt.Errorf("open store: %s", err)
	}

	// Initialize poller.
	m.poller = twitter.NewPoller()
	m.poller.Client = twittergo.NewClient(&oauth1a.ClientConfig{
		ConsumerKey:    m.Config.Twitter.Key,
		ConsumerSecret: m.Config.Twitter.Secret,
	}, nil)

	// Initialize notifiers for each account
	for _, acc := range m.Config.Accounts {
		client := twittergo.NewClient(
			&oauth1a.ClientConfig{
				ConsumerKey:    m.Config.Twitter.Key,
				ConsumerSecret: m.Config.Twitter.Secret,
			},
			oauth1a.NewAuthorizedConfig(acc.Key, acc.Secret),
		)

		n := twitter.NewNotifier()
		n.Username = acc.Username
		n.Language = acc.Language
		n.Client = client

		m.notifiers = append(m.notifiers, n)
	}

	// Open HTTP listener.
	ln, err := net.Listen("tcp", m.Addr)
	if err != nil {
		return err
	}
	m.Listener = ln
	m.Handler = &scuttlebutt.Handler{Store: m.store}

	// Run HTTP server is separate goroutine.
	logger.Printf("Listening on http://localhost%s", m.Addr)
	go http.Serve(m.Listener, m.Handler)

	// Create a poller & notify monitor.
	m.wg.Add(2)
	go m.runPoller()
	go m.runNotifier()

	return nil
}