This repository has been archived by the owner on Oct 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic downloader API to retrieve and search for Youtube Data feed…
…s from API.
- Loading branch information
Nyah Check
committed
Jul 31, 2017
1 parent
0259e97
commit 64887e3
Showing
2 changed files
with
287 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} |