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 });
        }
Example #2
0
        /* single youtbue video fetcher */
        public object yt_single_proc(ApplicationDbContext context, YoutubeEntity query)
        {
            var    model = new JGN_Videos();
            string ytid  = Fetch_YoutubeID(query.term);

            if (ytid == "")
            {
                return(new { status = "error", message = "Youtube video url failed to validate, please enter proper youtube id" });
            }

            string DeveloperID = Configs.YoutubeSettings.key; // SiteConfiguration.YoutubeDeveloperKey;

            try
            {
                var youtubeService = new YouTubeService(new BaseClientService.Initializer()
                {
                    ApiKey          = DeveloperID,
                    ApplicationName = this.GetType().ToString()
                });
                var searchListRequest = youtubeService.Videos.List("snippet,contentDetails");
                searchListRequest.Id = ytid;

                var searchListResponse = searchListRequest.Execute();

                List <string> videos = new List <string>();

                foreach (var searchResult in searchListResponse.Items)
                {
                    string videoid = searchResult.Id;
                    if (videoid.Contains(":"))
                    {
                        videoid = videoid.Remove(0, videoid.LastIndexOf(":") + 1);
                    }

                    string video_watch_id = "/watch?v=" + videoid;
                    model.title       = searchResult.Snippet.Title;
                    model.description = searchResult.Snippet.Description;
                    model.duration    = ""; // searchResult.ContentDetails.Duration.ToString();

                    ArrayList thumb_urls = new ArrayList();

                    /* foreach (MediaThumbnail thumbnail in video.Thumbnails)
                     * {
                     *  thumb_urls.Add(thumbnail.Url);
                     *  // str.AppendLine("\tThumbnail URL: " + thumbnail.Url + "<br />");
                     *  // str.AppendLine("\tThumbnail time index: " + thumbnail.Time + "<br />");
                     * }*/
                    if (searchResult.Snippet.Thumbnails.High != null)
                    {
                        thumb_urls.Add(searchResult.Snippet.Thumbnails.High.Url);
                    }
                    else if (searchResult.Snippet.Thumbnails.Medium != null)
                    {
                        thumb_urls.Add(searchResult.Snippet.Thumbnails.Medium.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);
                        model.preview_url = thumb_urls[count].ToString();
                    }
                    else
                    {
                        model.preview_url = thumb_urls[0].ToString();
                    }

                    /*foreach (Google.GData.YouTube.MediaContent mediaContent in searchResult.Snippet.contents)
                     * {
                     * // str.AppendLine("\tMedia Location: " + mediaContent.Url + "<br />");
                     * // str.AppendLine("\tMedia Type: " + mediaContent.Format + "<br />");
                     * this.duration = mediaContent.duration;
                     * }*/

                    model.youtubeid = video_watch_id;
                }
            }
            catch (Exception ex)
            {
                ErrorLgBLL.Add(context, "Embed Video - Fetch Error", "", ex.Message);
                return(new { status = "error", message = ex.Message });
            }


            /*YouTubeQuery query = new YouTubeQuery("http://gdata.youtube.com/feeds/api/videos/" + ytid);
             * //Feed<Video> videoFeed = req.Get<Video>(query);
             * Video video = req.Retrieve<Video>(query);
             * ProcessVideoEntry(video); */
            var lst = new List <JGN_Videos>();

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