61 lines
1.2 KiB
Go
61 lines
1.2 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"sync"
|
||
|
|
||
|
"forge.cadoles.com/pcaseiro/namecheck"
|
||
|
"forge.cadoles.com/pcaseiro/namecheck/twitter"
|
||
|
)
|
||
|
|
||
|
func checkUsername(ch chan<- string, wg *sync.WaitGroup, sn namecheck.SocialNetwork, username string) (bool, error) {
|
||
|
defer wg.Done()
|
||
|
// s1 := rand.NewSource(time.Now().UnixNano())
|
||
|
// time.Sleep(rand.New(s1).Intn(100))
|
||
|
res, err := sn.IsAvailable(username)
|
||
|
if err != nil {
|
||
|
ch <- fmt.Sprintf("Error : {%v}\n", err)
|
||
|
return false, err
|
||
|
}
|
||
|
|
||
|
if !res {
|
||
|
ch <- fmt.Sprintf("User %s is not available\n", username)
|
||
|
return false, nil
|
||
|
}
|
||
|
ch <- fmt.Sprintf("User %s is available\n", username)
|
||
|
return true, nil
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
var wg sync.WaitGroup
|
||
|
var socialNetworks []namecheck.SocialNetwork
|
||
|
|
||
|
// Create a channel for result
|
||
|
ch := make(chan string, 20)
|
||
|
|
||
|
// Create the "social networks"
|
||
|
for i := 0; i < 20; i++ {
|
||
|
socialNetworks = append(socialNetworks, &twitter.Twitter{})
|
||
|
}
|
||
|
|
||
|
// Start the routines for check
|
||
|
username := os.Args[1]
|
||
|
for _, sn := range socialNetworks {
|
||
|
wg.Add(1)
|
||
|
go checkUsername(ch, &wg, sn, username)
|
||
|
}
|
||
|
|
||
|
// Wait in background
|
||
|
go func() {
|
||
|
wg.Wait()
|
||
|
close(ch)
|
||
|
}()
|
||
|
|
||
|
// Print the result
|
||
|
for li := range ch {
|
||
|
fmt.Printf(li)
|
||
|
}
|
||
|
|
||
|
}
|