Example #1
0
        /* multiple youtube video fetcher */
        public object yt_multiple_proc(ApplicationDbContext context, YoutubeEntity entity)
        {
            var _youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                ApiKey          = Configs.YoutubeSettings.key,
                ApplicationName = this.GetType().ToString()
            });

            var model = new List <JGN_Videos>();

            var searchListRequest = _youtubeService.Search.List("snippet");

            searchListRequest.Q = entity.term;
            // order date
            switch (entity.order)
            {
            case 0:
                searchListRequest.Order = SearchResource.ListRequest.OrderEnum.Date;
                break;

            case 1:
                searchListRequest.Order = SearchResource.ListRequest.OrderEnum.Rating;
                break;

            case 2:
                searchListRequest.Order = SearchResource.ListRequest.OrderEnum.Relevance;
                break;

            case 3:
                searchListRequest.Order = SearchResource.ListRequest.OrderEnum.Title;
                break;

            case 4:
                searchListRequest.Order = SearchResource.ListRequest.OrderEnum.VideoCount;
                break;

            case 5:
                searchListRequest.Order = SearchResource.ListRequest.OrderEnum.ViewCount;
                break;
            }

            if (entity.youtubecategory != null && entity.youtubecategory != "")
            {
                searchListRequest.VideoCategoryId = entity.youtubecategory;
                searchListRequest.Type            = "video";
            }

            // upload date
            if (entity.uploaddate != 3)
            {
                switch (entity.uploaddate)
                {
                case 0:
                    searchListRequest.PublishedAfter = DateTime.Now.AddDays(-1);
                    break;

                case 1:
                    searchListRequest.PublishedAfter = DateTime.Now.AddDays(-7);
                    break;

                case 2:
                    searchListRequest.PublishedAfter = DateTime.Now.AddDays(-31);
                    break;
                }
            }

            var ChannelID    = "";
            var ChannelTitle = "";

            if (entity.userid != null && entity.userid != "")
            {
                var channeListRequest = _youtubeService.Channels.List("snippet");
                channeListRequest.ForUsername = entity.userid;
                try
                {
                    var channeListResponse = searchListRequest.Execute();
                    foreach (var searchResult in channeListResponse.Items)
                    {
                        var data = searchResult;
                        ChannelID    = searchResult.Snippet.ChannelId;
                        ChannelTitle = searchResult.Snippet.ChannelTitle;
                    }
                }
                catch (Exception ex)
                {
                    ErrorLgBLL.Add(context, "Exception Youtube Service - Fetch Channels", "", ex.Message);
                }
            }

            // Replace with your search term.
            //searchListRequest.ChannelId = ""; // Search Channel ID
            //searchListRequest.Order = SearchResource.ListRequest.OrderEnum.Date; //
            // searchListRequest.RelatedToVideoId = "";
            // searchListRequest.VideoCategoryId = "";
            //searchListRequest.VideoDuration = SearchResource.ListRequest.VideoDurationEnum.Any;
            // searchListRequest.VideoLicense = SearchResource.ListRequest.VideoLicenseEnum.Any;
            /* Note that you must set the type parameter to video if you set a value for the eventType, videoCaption, videoCategoryId, videoDefinition, videoDimension, videoDuration, videoEmbeddable, videoLicense, videoSyndicated, or videoType parameters. */
            searchListRequest.MaxResults = 50;
            // searchListRequest.Type = "video";
            if (ChannelID != null && ChannelID != "")
            {
                searchListRequest.ChannelId = ChannelID;
            }

            //searchListRequest.ChannelId = "mediasoftpro1";
            try
            {
                var searchListResponse = searchListRequest.Execute();

                var videos = new List <string>();
                //List<string> channels = new List<string>();
                //List<string> playlists = new List<string>();

                // Add each result to the appropriate list, and then display the lists of
                // matching videos, channels, and playlists.
                foreach (var searchResult in searchListResponse.Items)
                {
                    switch (searchResult.Id.Kind)
                    {
                    case "youtube#video":
                        // add youtube video functionality

                        string videoid = searchResult.Id.VideoId;
                        if (videoid.Contains(":"))
                        {
                            videoid = videoid.Remove(0, videoid.LastIndexOf(":") + 1);
                        }

                        string video_watch_id = "/watch?v=" + videoid;
                        if (!VideoBLL.Check_YOUTUBE_ID(context, video_watch_id))
                        {
                            string title       = searchResult.Snippet.Title;
                            string description = searchResult.Snippet.Description;

                            string    thumburl   = "";
                            ArrayList thumb_urls = new ArrayList();
                            if (searchResult.Snippet.Thumbnails.Maxres != null)
                            {
                                thumb_urls.Add(searchResult.Snippet.Thumbnails.Maxres.Url);
                            }
                            else if (searchResult.Snippet.Thumbnails.High != null)
                            {
                                thumb_urls.Add(searchResult.Snippet.Thumbnails.High.Url);
                            }
                            else if (searchResult.Snippet.Thumbnails.Standard != null)
                            {
                                thumb_urls.Add(searchResult.Snippet.Thumbnails.Standard.Url);
                            }
                            //else if (searchResult.Snippet.Thumbnails.Default != null)
                            //   thumb_urls.Add(searchResult.Snippet.Thumbnails.Default.Url);


                            if (thumb_urls.Count > 1)
                            {
                                int count = Convert.ToInt32(thumb_urls.Count / 2);
                                thumburl = thumb_urls[count].ToString();
                            }
                            else
                            {
                                thumburl = thumb_urls[0].ToString();
                            }

                            // process and add video
                            string flv_filename      = "";
                            string original_filename = "";
                            string thumb_filename    = "";
                            string duration          = "";
                            int    isapproved        = 1;

                            /*if (model.isapproved)
                             *  isapproved = 1;*/
                            // set video actions : 1 -> on, 0 -> off
                            int isenabled   = 1;
                            int ispublished = 1;
                            //int isapproved = 1;
                            int isprivate = 0;

                            int    isexternal = 1;
                            string pub_url    = "none";
                            string _embed     = "";

                            string ipaddress = "";
                            //string username = "";
                            //username = Site_Settings.Yt_UserName;

                            // remove <a tags from description
                            if (description != null && description != "")
                            {
                                Regex _href    = new Regex(@"<a[^<]+?>", RegexOptions.Singleline); // remove <a href.....>
                                Regex _hrefend = new Regex(@"</a>", RegexOptions.Singleline);      // remove </a>
                                description = _href.Replace(description, string.Empty);
                                description = _hrefend.Replace(description, string.Empty);

                                if (description.Length > 2000)
                                {
                                    description = description.Substring(0, 2000);
                                }
                            }

                            var vd = new JGN_Videos();
                            vd.userid = entity.userid;
                            vd.title  = title;
                            if (description != null)
                            {
                                vd.description = description;
                            }
                            vd.tags                  = "";
                            vd.duration              = duration;
                            vd.duration_sec          = 0; // Convert.ToInt32(duration_sec);
                            vd.originalvideofilename = original_filename;
                            vd.videofilename         = flv_filename;
                            vd.thumbfilename         = thumb_filename;
                            vd.isprivate             = (byte)isprivate;
                            vd.isenabled             = (byte)isenabled;

                            vd.ispublished  = (byte)ispublished;
                            vd.isapproved   = (byte)isapproved;
                            vd.pub_url      = pub_url;
                            vd.preview_url  = thumburl;
                            vd.embed_script = _embed;
                            vd.isexternal   = (byte)isexternal;
                            vd.ipaddress    = ipaddress;
                            vd.type         = 0;
                            vd.youtubeid    = video_watch_id;

                            model.Add(vd);
                        }

                        break;

                    case "youtube#channel":
                        //channels.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.ChannelId));
                        break;

                    case "youtube#playlist":
                        //playlists.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.PlaylistId));
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorLgBLL.Add(context, "Exception Youtube Service - Process Videos", "", ex.Message);
                return(new { status = "success", posts = ex.Message });
            }

            return(new { status = "success", posts = model });
        }