Esempio n. 1
0
func main() {
	// Fixes a broken console if the executable was killed.
	//InputState("cooked").Revert()

	var (
		exit         bool = false
		err          error
		arrayOutput  interface{}
		structOutput interface{}
		client       *twittergo.Client
	)

	clientConfig, err := GetClientConfig()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	userConfig, err := GetUserConfig(clientConfig)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	client = twittergo.NewClient(clientConfig, userConfig)
	registry := CommandRegistry{}

	registry.Register(Command{"help", "Prints this message", func() {
		registry.PrintHelp()
	}})

	registry.Register(Command{"exit", "Exits", func() {
		exit = true
	}})

	registry.Register(Command{"status/public_timeline", "", func() {
		arrayOutput, err = client.GetPublicTimeline(nil)
	}})

	registry.Register(Command{"status/retweeted_by_me", "", func() {
		arrayOutput, err = client.GetRetweetedByMe(nil)
	}})

	registry.Register(Command{"statuses/:id/retweeted_by", "", func() {
		id := PromptUser("Tweet ID")
		arrayOutput, err = client.GetStatusRetweetedBy(id, true, nil)
	}})

	registry.Register(Command{"statuses/update", "Posts a tweet", func() {
		status := PromptUser("What's happening?")
		structOutput, err = client.Update(status, nil)
	}})

	var command string
	for exit == false {
		fmt.Println("\nEnter a command (\"help\" for options, up/down to cycle through commands)")
		command = GetCommand(&registry)
		registry.Execute(command)

		if err != nil {
			fmt.Println("Error:", err)
			err = nil
		}
		if arrayOutput != nil {
			PrintArray(arrayOutput, "")
			arrayOutput = nil
		}
		if structOutput != nil {
			PrintStruct(structOutput, "")
			structOutput = nil
		}
	}
}
Esempio n. 2
0
// Obtains an OAuthUserConfig.
// Prompts the user to load a specified config, or starts a new OAuth flow.
func GetUserConfig(config *twittergo.OAuthConfig) (*twittergo.OAuthUserConfig, error) {
	dirPath := GetExecutableDirectory()
	globPath := dirPath + "*.twitter"
	matches, _ := filepath.Glob(globPath)
	names := make([]string, len(matches))
	for i, match := range matches {
		_, names[i] = filepath.Split(match)
		names[i] = strings.Replace(names[i], ".twitter", "", 1)
	}
	var choice int = 0
	if len(names) > 0 {
		for {
			fmt.Println("Choose an account to use:")
			fmt.Println("  0. Authorize new account")
			for i, name := range names {
				fmt.Printf("  %v. %v\n", i+1, name)
			}
			input := PromptUser("Choice")
			_, err := fmt.Sscanf(input, "%d", &choice)
			if err == nil && choice >= 0 && choice <= len(names) {
				break
			}
			fmt.Println("There was a problem parsing your input.")
		}
	}
	var userConfig twittergo.OAuthUserConfig
	if choice > 0 {
		configPath := path.Join(dirPath, names[choice-1]+".twitter")
		if err := LoadConfig(configPath, &userConfig); err == nil {
			if userConfig.AccessTokenKey != "" {
				return &userConfig, nil
			} else {
				fmt.Println("Config was not initialized")
			}
		} else {
			fmt.Println("Problem loading the requested config.")
		}
	} else {
		userConfig = twittergo.OAuthUserConfig{}
	}
	fmt.Println("Starting a new auth flow")
	client := twittergo.NewClient(config, &userConfig)
	if err := client.GetRequestToken(); err != nil {
		return nil, err
	}
	url, err := client.GetAuthorizeUrl()
	if err != nil {
		return nil, err
	}
	fmt.Println("Please visit this URL in your browser:", url)
	pin := PromptUser("Please input the PIN displayed")
	if err := client.GetAccessToken(userConfig.RequestTokenKey, pin); err != nil {
		return nil, err
	}
	configPath := path.Join(dirPath, userConfig.AccessValues["screen_name"][0]+".twitter")
	err = SaveConfig(configPath, userConfig)
	if err != nil {
		return nil, err
	}
	return &userConfig, nil
}