diff --git a/ytdata/ytdata.go b/ytdata/ytdata.go new file mode 100644 index 0000000..8ba93a8 --- /dev/null +++ b/ytdata/ytdata.go @@ -0,0 +1,209 @@ +// Copyright 2017 YTD Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. +// Download Youtube video data as mp3 + +package ytdata + +import ( + "flag" + "fmt" + "log" + "os" + "strings" + + "code.google.com/p/google-api-go-client/youtube/v3" +) + +//Youtube Downloader Data file. +type Ytdd struct { + FileName string + Title string + description string + category string + keywords string + privacy string + DataStream []byte +} + +var ( + filename = flag.String("filename", "", "Name of video file to upload") + title = flag.String("title", "Test Title", "Video title") + description = flag.String("description", "Test Description", "Video description") + category = flag.String("category", "22", "Video category") + keywords = flag.String("keywords", "", "Comma separated list of video keywords") + privacy = flag.String("privacy", "unlisted", "Video privacy status") +) + +func main() { + flag.Parse() + + if *filename == "" { + log.Fatalf("You must provide a filename of a video file to upload") + } + + client, err := buildOAuthHTTPClient(youtube.YoutubeUploadScope) + if err != nil { + log.Fatalf("Error building OAuth client: %v", err) + } + + service, err := youtube.New(client) + if err != nil { + log.Fatalf("Error creating YouTube client: %v", err) + } + + upload := &youtube.Video{ + Snippet: &youtube.VideoSnippet{ + Title: *title, + Description: *description, + CategoryId: *category, + }, + Status: &youtube.VideoStatus{PrivacyStatus: *privacy}, + } + + // The API returns a 400 Bad Request response if tags is an empty string. + if strings.Trim(*keywords, "") != "" { + upload.Snippet.Tags = strings.Split(*keywords, ",") + } + + call := service.Videos.Insert("snippet,status", upload) + + file, err := os.Open(*filename) + defer file.Close() + if err != nil { + log.Fatalf("Error opening %v: %v", *filename, err) + } + + response, err := call.Media(file).Do() + if err != nil { + log.Fatalf("Error making YouTube API call: %v", err) + } + fmt.Printf("Upload successful! Video ID: %v\n", response.Id) +} + +//retrieve uploads +func main() { + flag.Parse() + + client, err := buildOAuthHTTPClient(youtube.YoutubeReadonlyScope) + if err != nil { + log.Fatalf("Error building OAuth client: %v", err) + } + + service, err := youtube.New(client) + if err != nil { + log.Fatalf("Error creating YouTube client: %v", err) + } + + // Start making YouTube API calls. + // Call the channels.list method. Set the mine parameter to true to + // retrieve the playlist ID for uploads to the authenticated user's + // channel. + call := service.Channels.List("contentDetails").Mine(true) + + response, err := call.Do() + if err != nil { + // The channels.list method call returned an error. + log.Fatalf("Error making API call to list channels: %v", err.Error()) + } + + for _, channel := range response.Items { + playlistId := channel.ContentDetails.RelatedPlaylists.Uploads + // Print the playlist ID for the list of uploaded videos. + fmt.Printf("Videos in list %s\r\n", playlistId) + + nextPageToken := "" + for { + // Call the playlistItems.list method to retrieve the + // list of uploaded videos. Each request retrieves 50 + // videos until all videos have been retrieved. + playlistCall := service.PlaylistItems.List("snippet"). + PlaylistId(playlistId). + MaxResults(50). + PageToken(nextPageToken) + + playlistResponse, err := playlistCall.Do() + + if err != nil { + // The playlistItems.list method call returned an error. + log.Fatalf("Error fetching playlist items: %v", err.Error()) + } + + for _, playlistItem := range playlistResponse.Items { + title := playlistItem.Snippet.Title + videoId := playlistItem.Snippet.ResourceId.VideoId + fmt.Printf("%v, (%v)\r\n", title, videoId) + } + + // Set the token to retrieve the next page of results + // or exit the loop if all results have been retrieved. + nextPageToken = playlistResponse.NextPageToken + if nextPageToken == "" { + break + } + fmt.Println() + } + } +} + +var ( + query = flag.String("query", "Google", "Search term") + maxResults = flag.Int64("max-results", 25, "Max YouTube results") +) + +const developerKey = "YOUR DEVELOPER KEY" + +func main() { + flag.Parse() + + client := &http.Client{ + Transport: &transport.APIKey{Key: developerKey}, + } + + service, err := youtube.New(client) + if err != nil { + log.Fatalf("Error creating new YouTube client: %v", err) + } + + // Make the API call to YouTube. + call := service.Search.List("id,snippet"). + Q(*query). + MaxResults(*maxResults) + response, err := call.Do() + if err != nil { + log.Fatalf("Error making search API call: %v", err) + } + + // Group video, channel, and playlist results in separate lists. + videos := make(map[string]string) + channels := make(map[string]string) + playlists := make(map[string]string) + + // Iterate through each item and add it to the correct list. + for _, item := range response.Items { + switch item.Id.Kind { + case "youtube#video": + videos[item.Id.VideoId] = item.Snippet.Title + case "youtube#channel": + channels[item.Id.ChannelId] = item.Snippet.Title + case "youtube#playlist": + playlists[item.Id.PlaylistId] = item.Snippet.Title + } + } + + printIDs("Videos", videos) + printIDs("Channels", channels) + printIDs("Playlists", playlists) +} + +// Print the ID and title of each result in a list as well as a name that +// identifies the list. For example, print the word section name "Videos" +// above a list of video search results, followed by the video ID and title +// of each matching video. +func printIDs(sectionName string, matches map[string]string) { + fmt.Printf("%v:\n", sectionName) + for id, title := range matches { + fmt.Printf("[%v] %v\n", id, title) + } + fmt.Printf("\n\n") +} diff --git a/ytdata/ytdsearch.go b/ytdata/ytdsearch.go new file mode 100644 index 0000000..e5d2b94 --- /dev/null +++ b/ytdata/ytdsearch.go @@ -0,0 +1,78 @@ +// Copyright 2017 YTD Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. +// Search Youtube API for Video Data + +package ytdata + +import ( + "flag" + "fmt" + "log" + "net/http" + + "code.google.com/p/google-api-go-client/googleapi/transport" + "code.google.com/p/google-api-go-client/youtube/v3" +) + +var ( + query = flag.String("query", "Google", "Search term") + maxResults = flag.Int64("max-results", 25, "Max YouTube results") +) + +const developerKey = "YOUR DEVELOPER KEY" + +func main() { + flag.Parse() + + client := &http.Client{ + Transport: &transport.APIKey{Key: developerKey}, + } + + service, err := youtube.New(client) + if err != nil { + log.Fatalf("Error creating new YouTube client: %v", err) + } + + // Make the API call to YouTube. + call := service.Search.List("id,snippet"). + Q(*query). + MaxResults(*maxResults) + response, err := call.Do() + if err != nil { + log.Fatalf("Error making search API call: %v", err) + } + + // Group video, channel, and playlist results in separate lists. + videos := make(map[string]string) + channels := make(map[string]string) + playlists := make(map[string]string) + + // Iterate through each item and add it to the correct list. + for _, item := range response.Items { + switch item.Id.Kind { + case "youtube#video": + videos[item.Id.VideoId] = item.Snippet.Title + case "youtube#channel": + channels[item.Id.ChannelId] = item.Snippet.Title + case "youtube#playlist": + playlists[item.Id.PlaylistId] = item.Snippet.Title + } + } + + printIDs("Videos", videos) + printIDs("Channels", channels) + printIDs("Playlists", playlists) +} + +// Print the ID and title of each result in a list as well as a name that +// identifies the list. For example, print the word section name "Videos" +// above a list of video search results, followed by the video ID and title +// of each matching video. +func printIDs(sectionName string, matches map[string]string) { + fmt.Printf("%v:\n", sectionName) + for id, title := range matches { + fmt.Printf("[%v] %v\n", id, title) + } + fmt.Printf("\n\n") +}