예제 #1
0
        public void GenerateContent(int DaysOfSet)
        {
            //Get all topics modifield more then 30 day behind from now
            var courseTopics = this.List().Where(x => x.Modificated < DateTime.Now.AddDays(-DaysOfSet)).OrderBy(x => x.Modificated);
            //var courseTopics = this.List().OrderByDescending(x => x.Modificated).Take(3);

            YoutubeClientService youtubeService = new YoutubeClientService();
            int videosToReturn = 10;

            foreach (CourseTopic c in courseTopics)
            {
                try
                {
                    var videos = youtubeService.FindContent(c.Title, videosToReturn);

                    foreach (Content content in videos)
                    {
                        content.CourseTopicId = c.Id;

                        //Tenta buscar na base se o video já existe
                        var contentsInBase = serviceContent.Get().Where(x => x.Data == content.Data);

                        //se existe
                        if (contentsInBase.Any())
                        {
                            content.Id = contentsInBase.FirstOrDefault().Id;
                            serviceContent.Put <ContentValidator>(content);
                        }
                        else
                        {
                            serviceContent.Post <ContentValidator>(content);
                        }
                    }

                    //If api return the videos solicitaded, update course topic to avoid repeting FindContent execution
                    if (videos.Count() == videosToReturn)
                    {
                        c.Modificated = DateTime.Now;
                        this.Update(c);
                    }
                }
                catch (Exception ex) {
                    if (ex.Message == "Cota excedida")
                    {
                        break;
                    }
                    else
                    {
                        LogManager.Error(ex.Message);
                    }
                }
            }
        }
예제 #2
0
        public async Task <IActionResult> ImportPlaylist([FromServices] YoutubeClientService youtubeService, [FromServices] ChannelService channelService,
                                                         [FromBody] ImportLibraryForm data)
        {
            using (var ctx = DataFactory.GetDataContext())
            {
                var channel = await ctx.Channel.FirstOrDefaultAsync(c => c.OwnerId == User.GetUserId() && c.Id == data.channelId && !c.IsDisabled);

                if (channel == null)
                {
                    return(NotFound());
                }
            }

            string playlistId = null;

            try
            {
                Uri playlistUri = new Uri(data.playlistUrl);
                playlistId = HttpUtility.ParseQueryString(playlistUri.Query).Get("list");
            }
            catch (Exception e)
            {
                _log.Information(e, "Invalid playlist url [{0}]", data.playlistUrl);
                return(BadRequest("Invalid playlist"));
            }

            var videos = await youtubeService.ImportPlaylist(playlistId);

            var videoIds = videos.Select(v => v.Id).ToList();

            var libraryItems = await channelService.AddVideosToLibrary(data.channelId, videoIds);

            if (data.addToPlaylist)
            {
                await channelService.AddVideosToPlaylist(data.channelId, libraryItems);
            }

            return(Ok(new { imported = libraryItems.Count }));
        }
 public void RemoveClient(IYoutubeClient client)
 {
     YoutubeClientService.RemoveClient(client);
 }
 public void AddClient(string clientId, string clientSecret, string name)
 {
     YoutubeClientService.AddClient(clientId, clientSecret, name);
 }