Exemplo n.º 1
0
        public static string GetVideoDuration(string videoID)
        {
            //Starts the youtube service
            YouTubeService yt = new YouTubeService(new BaseClientService.Initializer()
            {
                ApiKey = SerenityCredentials.GoogleAPIKey()
            });

            //Starts a video search request, to return video objects
            var searchVideoRequest = yt.Videos.List("contentDetails");


            //We'll search for the video directly with its ID
            //searchVideoRequest.
            searchVideoRequest.Id = videoID;
            var res = searchVideoRequest.Execute();

            //If the ID is invalid
            if (res.Items.Count == 0)
            {
                return(null);
            }

            //Otherwise, since the ID is unique to each video, we'll always get exactly one result
            return(FormatVideoDuration(res.Items[0].ContentDetails.Duration));
        }
Exemplo n.º 2
0
        public async Task SearchImgAsync([Remainder] string search)
        {
            var customSearchService = new CustomsearchService(new BaseClientService.Initializer {
                ApiKey = SerenityCredentials.GoogleAPIKey()
            });

            var    listRequest = customSearchService.Cse.List(search);
            Random rnd         = new Random();

            listRequest.Cx = SerenityCredentials.CustomSearchEngineKey();

            //Restricting the search to only images
            listRequest.SearchType = CseResource.ListRequest.SearchTypeEnum.Image;

            //The results will be put inside this list
            IList <Result> paging = new List <Result>();

            //Begins the search
            paging = listRequest.Execute().Items;

            int index = 0;

            var embedImg = new EmbedBuilder()
                           .WithColor(new Color(240, 230, 231))
                           .WithAuthor(eab => eab.WithName(search))
                           .WithDescription(paging.ElementAt(index).Link)
                           .WithImageUrl(paging.ElementAt(index).Link);

            await Context.Channel.SendMessageAsync("", false, embedImg);
        }
Exemplo n.º 3
0
        public async Task RunBotAsync()
        {
            _client   = new DiscordSocketClient();
            _commands = new CommandService();
            _audio    = new AudioService();
            //Music music = new Music(_audio);

            _services = new ServiceCollection()
                        .AddSingleton(_client)
                        .AddSingleton(_commands)
                        .AddSingleton(_audio)
                        .BuildServiceProvider();

            String botToken = SerenityCredentials.BotToken();

            //Log and UserJoined event handlers
            _client.Log        += Log;
            _client.UserJoined += AnnounceUserJoined;


            await RegisterCommandsAsync();

            await _client.LoginAsync(TokenType.Bot, botToken);

            await _client.StartAsync();

            await Task.Delay(-1);
        }
Exemplo n.º 4
0
        /*public static int PlaylistSize(string playlistID)
         * {
         *
         * }*/



        public static PlaylistItem[] PlaylistSearch(string playlistID)
        {
            YouTubeService yt = new YouTubeService(new BaseClientService.Initializer()
            {
                ApiKey = SerenityCredentials.GoogleAPIKey()
            });

            var playlistItems = yt.PlaylistItems.List("snippet,status,contentDetails");
            var nextPageToken = "";

            playlistItems.PlaylistId = playlistID;
            playlistItems.MaxResults = 50;
            playlistItems.PageToken  = nextPageToken;


            int i = 0;

            var res       = playlistItems.Execute();
            int totalSize = res.PageInfo.TotalResults.Value;

            PlaylistItem[] videos = new PlaylistItem[totalSize];

            IList <PlaylistItem> resItems = res.Items;

            resItems.CopyTo(videos, i);

            i += 50;

            nextPageToken = res.NextPageToken;

            while (nextPageToken != null)
            {
                playlistItems            = yt.PlaylistItems.List("snippet,status,contentDetails");
                playlistItems.PlaylistId = playlistID;
                playlistItems.MaxResults = 50;
                playlistItems.PageToken  = nextPageToken;

                var resp = playlistItems.Execute();

                resItems = resp.Items;

                resItems.CopyTo(videos, i);

                nextPageToken = resp.NextPageToken;

                i += 50;
            }



            return(videos);
        }
Exemplo n.º 5
0
        //Starts a youtube search with the given query, will return null if no results are found
        //Returns the first result that matches certain criterias explained further on.
        public static Video YoutubeSearch(string query)
        {
            //Starts the youtube service
            YouTubeService yt = new YouTubeService(new BaseClientService.Initializer()
            {
                ApiKey = SerenityCredentials.GoogleAPIKey()
            });

            //Creates the search request
            var searchListRequest = yt.Search.List("snippet");

            //checks if the query is a youtube url
            //if it is, we will directly get the video's id
            //that is inside the url
            var res = TryParseID(query);

            if (res.Valid)
            {
                return(SearchVideoByID(res.ID));
            }

            //sets the search type and the query
            //setting to video type excludes channels and playlists from the results,
            //however live broadcasts wont be excluded, but this is dealt afterwards.
            searchListRequest.Type = "video";
            searchListRequest.Q    = query;

            //the video will be attempted to be found inside a maximum of 10 results
            searchListRequest.MaxResults = 10;

            try
            {
                //Starts the request
                SearchListResponse searchRequest = searchListRequest.Execute();
                //This means no results were encountered
                if (searchRequest.Items.Count < 1)
                {
                    return(null);
                }
                else
                {
                    var items = searchRequest.Items;
                    foreach (var item in items)
                    {
                        //This is where we deal with the live broadcast results
                        //we'll get the first item that has "none" set to the LiveBroadcastContent property,
                        if (item.Snippet.LiveBroadcastContent == "none")
                        {
                            return(SearchVideoByID(item.Id.VideoId));
                        }
                    }
                }

                //There's a possibility that all 10 results were live broadcasts, so we have
                //to return null here also
                return(null);
            }
            catch (Exception e)
            {
                ExceptionHandler.WriteToFile(e);
            }
            return(null);
        }