To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

Commit fb16074e authored by Christof Gerber's avatar Christof Gerber
Browse files

GetPlaylistVideos method. Uses YouTube Data API.

parent a56b762f
package youtubedl
import (
"context"
"encoding/json"
"fmt"
"github.com/asticode/go-astisub"
log "github.com/sirupsen/logrus"
"google.golang.org/api/googleapi/transport"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
"net/http"
"net/url"
"os"
"os/exec"
......@@ -264,3 +269,26 @@ func GetVideoURL(id string) (video *url.URL, audio *url.URL, err error) {
}
return video, audio, nil
}
func GetPlaylistVideos(playListID string, apiKey string) (members []string, err error) {
client := &http.Client{
Transport: &transport.APIKey{Key: apiKey},
}
service, err := youtube.NewService(context.Background(), option.WithHTTPClient(client))
if err != nil {
return nil, err
}
// Make the API call to YouTube.
call := service.PlaylistItems.List("id,contentDetails").PlaylistId(playListID)
response, err := call.Do()
if err != nil {
return nil, err
}
for _, item := range response.Items {
members = append(members, item.ContentDetails.VideoId)
}
return members, nil
}
package youtubedl
import (
"context"
"flag"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"google.golang.org/api/googleapi/transport"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
"net/http"
"os"
"path/filepath"
"testing"
)
var (
query = flag.String("query", "Google", "Search term")
maxResults = flag.Int64("max-results", 25, "Max YouTube results")
)
func TestGetPlaylistVideos(t *testing.T) {
// 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")
}
func TestYouTubeAPICall(t *testing.T) {
client := &http.Client{
Transport: &transport.APIKey{Key: ""},
}
log.SetLevel(log.TraceLevel)
service, err := youtube.NewService(context.Background(), option.WithHTTPClient(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()
videos, err := GetPlaylistVideos("PLpTJzCmGYaZFjE7jfH1tKFZt9rtqvvuj0", os.Getenv("YOUTUBE_DATA_API_KEY"))
assert.Equal(t, nil, 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)
}
func TestYouTubeCaptionCall(t *testing.T) {
client := &http.Client{
Transport: &transport.APIKey{Key: "AIzaSyBerFPRGbBxZayOhvGnUhXmfjmEKVyFe8E"},
}
service, err := youtube.NewService(context.Background(), option.WithHTTPClient(client))
if err != nil {
log.Fatalf("Error creating new YouTube client: %v", err)
}
// TODO list captions
// TODO download captions
// TODO migrate functions
// TODO video/audo stream url
// TODO migrate them as well
//service.Captions.List()
assert.Equal(t, 3, len(videos))
}
func TestGetYouTubeDownloadLink(t *testing.T) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment