Beispiel #1
0
        public static string ToJson(YoutubeVideo video)
        {
            StringWriter   sw     = new StringWriter();
            JsonTextWriter writer = new JsonTextWriter(sw);

            writer.WriteStartObject();

            writer.WritePropertyName("name");
            writer.WriteValue(video.title);
            writer.WritePropertyName("description");
            writer.WriteValue(video.descriprion);
            writer.WritePropertyName("duration");
            writer.WriteValue(video.duration);
            writer.WritePropertyName("sources");
            writer.WriteStartArray();
            writer.WriteStartObject();

            writer.WritePropertyName("src");
            writer.WriteValue(video.source);

            writer.WritePropertyName("type");
            writer.WriteValue("video/youtube");
            writer.WriteEndObject();
            writer.WriteEndArray();

            writer.WritePropertyName("poster");
            writer.WriteValue(video.poster);

            writer.WritePropertyName("thumbnail");
            writer.WriteStartArray();
            writer.WriteStartObject();
            writer.WritePropertyName("srcset");
            writer.WriteValue(video.thumbnail);
            writer.WritePropertyName("type");
            writer.WriteValue("image/jpeg");
            writer.WritePropertyName("type");
            writer.WriteValue("(min-width: 200px;)");
            writer.WriteEndObject();
            writer.WriteStartObject();
            writer.WritePropertyName("src");
            writer.WriteValue(video.poster);
            writer.WriteEndObject();
            writer.WriteEndArray();


            writer.WriteEndObject();

            return(sw.ToString());
        }
Beispiel #2
0
        public async Task <IEnumerable <YoutubeVideo> > GetVideosFromLink(string source)
        {
            YoutubeClient       youtubeX  = new YoutubeClient();
            List <YoutubeVideo> newVideos = new List <YoutubeVideo>();

            if (source.Contains("list="))
            {
                YoutubeExpld.Playlists.Playlist playlist = await youtubeX.Playlists.GetAsync(source);



                HashSet <string> newVideoIds = new HashSet <string>();
                while (true)
                {
                    int i             = 0;
                    var playlistArray = await youtubeX.Playlists.GetVideosAsync(playlist.Id, 500, newVideoIds.Count(), 5, newVideoIds);

                    foreach (YoutubeExpld.Videos.Video vid in playlistArray)
                    {
                        YoutubeVideo newVid = new YoutubeVideo()
                        {
                            source    = "https://www.youtube.com/watch?v=" + vid.Id,
                            poster    = vid.Thumbnails.MediumResUrl,
                            title     = vid.Title,
                            duration  = vid.Duration.TotalSeconds,
                            thumbnail = vid.Thumbnails.StandardResUrl
                        };
                        lock (SynchronousReadLock)
                        {
                            newVideos.Add(newVid);
                        }
                        i++;
                    }
                    if (i == 0 || i == 500)
                    {
                        break;
                    }

                    Clients.Caller.previewLoadedVideos(ToJsonRange(newVideos));
                }

                //VideosInRoom.AddOrUpdate( roomName, newVideos, (key, oldValue) => { oldValue.AddRange(newVideos); return oldValue; });
            }
            else if (source.Contains("watch") && !source.Contains("list="))
            {
                // You can specify video ID or URL
                var video = await youtubeX.Videos.GetAsync(source);

                YoutubeVideo newVid = new YoutubeVideo()
                {
                    source    = "https://www.youtube.com/watch?v=" + video.Id,
                    poster    = video.Thumbnails.MediumResUrl,
                    title     = video.Title,
                    duration  = video.Duration.TotalSeconds,
                    thumbnail = video.Thumbnails.StandardResUrl
                };
                lock (SynchronousReadLock)
                {
                    newVideos.Add(newVid);
                }

                Clients.Caller.previewLoadedVideos(ToJsonRange(newVideos));
            }


            return(null);
        }
Beispiel #3
0
        //"[{"sources":[{"src":"www.youtube.com/watch?v=1cHXjnli2fI","type":"video/youtube"}],"poster":"https://img.youtube.com/vi/1cHXjnli2fI/mqdefault.jpg"},{"sources":[{"src":"www.youtube.com/watch?v=65r_1TzJXaQ","type":"video/youtube"}],"poster":"https://img.youtube.com/vi/65r_1TzJXaQ/mqdefault.jpg"},{"sources":[{"src":"www.youtube.com/watch?v=wuYPnkDbFFU","type":"video/youtube"}],"poster":"https://img.youtube.com/vi/wuYPnkDbFFU/mqdefault.jpg"},{"sources":[{"src":"www.youtube.com/watch?v=xU-3Zdei1N0","type":"video/youtube"}],"poster":"https://img.youtube.com/vi/xU-3Zdei1N0/mqdefault.jpg"},]"
        public async Task <IEnumerable <YoutubeVideo> > ChangeVideoSource(string roomName, string source)
        {
            if (roomName != null && source.Contains("yotu"))
            {
                using (UnitOfWork db = new UnitOfWork())
                {
                    YoutubeClient       youtubeX  = new YoutubeClient();
                    List <YoutubeVideo> newVideos = new List <YoutubeVideo>();
                    ConversationRoom    room      = db.conversationRoomRepository.GetById(roomName);

                    //YoutubeClient youtube = new YoutubeClient();

                    if (source.Contains("list="))
                    {
                        YoutubeExpld.Playlists.Playlist playlist = await youtubeX.Playlists.GetAsync(source);



                        HashSet <string> newVideoIds = new HashSet <string>();
                        while (true)
                        {
                            int i             = 0;
                            var playlistArray = await youtubeX.Playlists.GetVideosAsync(playlist.Id, 500, newVideoIds.Count(), 5, newVideoIds);

                            foreach (YoutubeExpld.Videos.Video vid in playlistArray)
                            {
                                YoutubeVideo newVid = new YoutubeVideo()
                                {
                                    source    = "https://www.youtube.com/watch?v=" + vid.Id,
                                    poster    = vid.Thumbnails.MediumResUrl,
                                    title     = vid.Title,
                                    duration  = vid.Duration.TotalSeconds,
                                    thumbnail = vid.Thumbnails.StandardResUrl
                                };
                                lock (SynchronousReadLock)
                                {
                                    newVideos.Add(newVid);
                                }
                                i++;
                            }
                            if (i == 0)
                            {
                                break;
                            }

                            //Clients.Caller.previewLoadedVideos(ToJsonRange(newVideos));
                        }
                        VideosInRoom.AddOrUpdate(roomName, newVideos, (key, oldValue) => { oldValue.AddRange(newVideos); return(oldValue); });
                    }
                    else if (source.Contains("watch") && !source.Contains("list="))
                    {
                        var youtube = new YoutubeClient();

                        // You can specify video ID or URL
                        var video = await youtube.Videos.GetAsync(source);

                        YoutubeVideo newVid = new YoutubeVideo()
                        {
                            source    = "https://www.youtube.com/watch?v=" + video.Id,
                            poster    = video.Thumbnails.MediumResUrl,
                            title     = video.Title,
                            duration  = video.Duration.TotalSeconds,
                            thumbnail = video.Thumbnails.StandardResUrl
                        };
                        lock (SynchronousReadLock)
                        {
                            newVideos.Add(newVid);
                        }
                        VideosInRoom.AddOrUpdate(roomName, newVideos, (key, oldValue) => { oldValue.AddRange(newVideos); return(oldValue); });
                    }

                    Log.Debug("APP DOMAIN ID = " + AppDomain.CurrentDomain.Id);
                    if (VideosInRoom.ContainsKey("roomName"))
                    {
                        Clients.Group(roomName).changeVideoSource(ToJsonRange(VideosInRoom[roomName]));
                    }
                }
            }
            return(null);
        }