コード例 #1
0
ファイル: shittyclient.go プロジェクト: norcalli/megashares
func main() {
	usernamePt := flag.String("u", "", "Megashares username.")
	passwordPt := flag.String("p", "", "Megashares password.")
	queryPt := flag.String("q", "", "Query to search for.")
	nPt := flag.Int("n", -1, "Which result to download. n < 0 => Print results")

	flag.Parse()
	username, password, query, n := *usernamePt, *passwordPt, *queryPt, *nPt
	if username == "" || password == "" || query == "" {
		flag.Usage()
		os.Exit(0)
	}

	m := megashares.New()
	if err := m.Login(username, password); err != nil {
		log.Fatalf("Couldn't login! Reason: %s\n", err)
	}
	entries, _ := m.SearchEntries(query)
	if n < 0 {
		for i, entry := range entries {
			fmt.Printf("%d: %s\n", i, entry.Filename)
		}
	} else {
		n = n % len(entries) // I'm lazy and don't feel like checking for invalid.
		entry := entries[n]
		fmt.Printf("Downloading entry %d: %s...\n", n, entry.Filename)
		if err := DownloadEntry(entry); err != nil {
			log.Fatalf("Failed to download because %s\n", err)
		}
	}
}
コード例 #2
0
// TODO: File cookiejar
func main() {
	creds, err := loadCredentials()
	if err != nil {
		fmt.Printf("Failed to load credentials. (%s)\n", err)
		return
	}
	if len(os.Args) == 1 {
		fmt.Printf("Usage: %s <query>\n", os.Args[0])
		return
	}
	// Form a query by joining all the arguments.
	query := strings.Join(os.Args[1:], " ")

	client := megashares.New()
	// Attempt to login.
	for {
		if err := client.Login(creds.Username, creds.Password); err != nil {
			fmt.Printf("Failed to login: %s\n", err)
			creds = askForCredentials()
			// log.Fatalf("Couldn't login! Reason: %s\n", err)
		} else {
			break
		}
	}

	// Perform the search
	entries, _ := client.SearchEntries(query)

	// Print out the results of the search for the user to pick from.
	for i, entry := range entries {
		fmt.Fprintf(os.Stderr, "%d: %s\n", i, entry.Filename)
	}

	// Get a valid number to choose from from the input loop.
	// TODO: Allow for pagination by returning (choice, page).
	choice := getValidNumber(0, len(entries)-1)
	entry := entries[choice]

	fmt.Print(entry.Url)
	return
	if file, response, err := ContinueDownload(client.Client, entry.Filename, entry.Url); err != nil {
		log.Fatal(err)
	} else {
		defer file.Close()
		defer response.Body.Close()
		length := response.ContentLength
		// Initialize progress bar.
		bar := pb.StartNew(int(length)).SetUnits(pb.U_BYTES)
		bar.ShowSpeed = true
		writer := io.MultiWriter(file, bar)
		io.Copy(writer, response.Body)
	}
}