Exemple #1
0
        public static void ParseAndUpdateFeeds(string ChannelId)
        {
            try
            {
                var _ObjYtActivities = new Activities(AppSettings.googleClientId, AppSettings.googleClientSecret, AppSettings.googleRedirectionUrl);
                var apiKey           = AppSettings.googleApiKey_TestApp;



                MongoYoutubeFeeds _ObjMongoYtFeeds;
                var _ChannelVideos  = _ObjYtActivities.GetYtVideos(ChannelId, apiKey);
                var J_ChannelVideos = JObject.Parse(_ChannelVideos);

                foreach (var itemVideo in J_ChannelVideos.SelectTokens("items"))
                {
                    _lstGlobalComVideos.Clear();
                    _ObjMongoYtFeeds                = new MongoYoutubeFeeds();
                    _ObjMongoYtFeeds.Id             = ObjectId.GenerateNewId();
                    _ObjMongoYtFeeds.YtChannelId    = ChannelId;
                    _ObjMongoYtFeeds.YtVideoId      = itemVideo.SelectToken("contentDetails.upload.videoId")?.ToString();
                    _ObjMongoYtFeeds.VdoTitle       = itemVideo.SelectToken("snippet.title")?.ToString();
                    _ObjMongoYtFeeds.VdoDescription = itemVideo.SelectToken("snippet.description")?.ToString();
                    _ObjMongoYtFeeds.VdoPublishDate = itemVideo.SelectToken("snippet.publishedAt")?.ToString();
                    _ObjMongoYtFeeds.VdoImage       = itemVideo.SelectToken("snippet.thumbnails.high.url")?.ToString();
                    _ObjMongoYtFeeds.VdoImage       = itemVideo.SelectToken("snippet.thumbnails.medium.url")?.ToString();
                    _ObjMongoYtFeeds.VdoUrl         = "https://www.youtube.com/watch?v=" + _ObjMongoYtFeeds.YtVideoId;
                    _ObjMongoYtFeeds.VdoEmbed       = "https://www.youtube.com/embed/" + _ObjMongoYtFeeds.YtVideoId;

                    var YtFeedsRepo = new MongoRepository("YoutubeVideos");
                    var ret         = YtFeedsRepo.Find <MongoYoutubeFeeds>(t => t.YtVideoId.Equals(_ObjMongoYtFeeds.YtVideoId));
                    var task        = Task.Run(async() =>
                    {
                        return(await ret);
                    });
                    var count = task.Result.Count;
                    if (count < 1)
                    {
                        try
                        {
                            YtFeedsRepo.Add(_ObjMongoYtFeeds);
                        }
                        catch { }
                    }
                    else
                    {
                        try
                        {
                            var filter = new BsonDocument("YtVideoId", _ObjMongoYtFeeds.YtVideoId);
                            var update = Builders <BsonDocument> .Update.Set("VdoTitle", _ObjMongoYtFeeds.VdoTitle).Set("VdoDescription", _ObjMongoYtFeeds.VdoDescription).Set("VdoImage", _ObjMongoYtFeeds.VdoImage);

                            YtFeedsRepo.Update <MongoYoutubeFeeds>(update, filter);
                        }
                        catch { }
                    }

                    //new Thread(delegate () {
                    GetYtComments(_ObjMongoYtFeeds.YtVideoId, apiKey, ChannelId);
                    UpdateYtComments(_ObjMongoYtFeeds.YtVideoId, apiKey, ChannelId);
                    //}).Start();
                }
            }
            catch (Exception ex)
            {
            }
        }
        public static void GetYtFeeds(string ChannelId, string AcessToken)
        {
            oAuthTokenYoutube ObjoAuthTokenGPlus = new oAuthTokenYoutube(AppSettings.GoogleConsumerKey, AppSettings.GoogleConsumerSecret, AppSettings.GoogleRedirectUri);
            Activities        _ObjYtActivities   = new Activities(AppSettings.GoogleConsumerKey, AppSettings.GoogleConsumerSecret, AppSettings.GoogleRedirectUri);
            string            apiKey             = AppSettings.GoogleApiKey;

            try
            {
                MongoYoutubeFeeds _ObjMongoYtFeeds;
                string            _ChannelVideos  = _ObjYtActivities.GetYtVideos(ChannelId, apiKey);
                JObject           J_ChannelVideos = JObject.Parse(_ChannelVideos);
                foreach (var item in J_ChannelVideos["items"])
                {
                    _ObjMongoYtFeeds             = new MongoYoutubeFeeds();
                    _ObjMongoYtFeeds.Id          = ObjectId.GenerateNewId();
                    _ObjMongoYtFeeds.YtChannelId = ChannelId;
                    try
                    {
                        _ObjMongoYtFeeds.YtVideoId = item["contentDetails"]["upload"]["videoId"].ToString();
                    }
                    catch
                    {
                    }
                    try
                    {
                        _ObjMongoYtFeeds.VdoTitle = item["snippet"]["title"].ToString();
                    }
                    catch
                    {
                    }
                    try
                    {
                        _ObjMongoYtFeeds.VdoDescription = item["snippet"]["description"].ToString();
                        if (_ObjMongoYtFeeds.VdoDescription == "")
                        {
                            _ObjMongoYtFeeds.VdoDescription = "No Description";
                        }
                    }
                    catch
                    {
                    }

                    try
                    {
                        _ObjMongoYtFeeds.VdoPublishDate = item["snippet"]["publishedAt"].ToString();
                    }
                    catch
                    {
                    }
                    try
                    {
                        _ObjMongoYtFeeds.VdoImage = item["snippet"]["thumbnails"]["high"]["url"].ToString();
                    }
                    catch
                    {
                        _ObjMongoYtFeeds.VdoImage = item["snippet"]["thumbnails"]["medium"]["url"].ToString();
                    }
                    try
                    {
                        _ObjMongoYtFeeds.VdoUrl = "https://www.youtube.com/watch?v=" + _ObjMongoYtFeeds.YtVideoId;
                    }
                    catch
                    {
                    }
                    try
                    {
                        _ObjMongoYtFeeds.VdoEmbed = "https://www.youtube.com/embed/" + _ObjMongoYtFeeds.YtVideoId;
                    }
                    catch
                    {
                    }
                    try
                    {
                        MongoRepository YtFeedsRepo = new MongoRepository("YoutubeVideos");
                        var             ret         = YtFeedsRepo.Find <MongoYoutubeFeeds>(t => t.YtVideoId.Equals(_ObjMongoYtFeeds.YtVideoId));
                        var             task        = Task.Run(async() =>
                        {
                            return(await ret);
                        });
                        int count = task.Result.Count;
                        if (count < 1)
                        {
                            try
                            {
                                YtFeedsRepo.Add(_ObjMongoYtFeeds);
                            }
                            catch { }
                        }
                        else
                        {
                            try
                            {
                                FilterDefinition <BsonDocument> filter = new BsonDocument("YtVideoId", _ObjMongoYtFeeds.YtVideoId);
                                var update = Builders <BsonDocument> .Update.Set("VdoTitle", _ObjMongoYtFeeds.VdoTitle).Set("VdoDescription", _ObjMongoYtFeeds.VdoDescription).Set("VdoImage", _ObjMongoYtFeeds.VdoImage);

                                YtFeedsRepo.Update <MongoYoutubeFeeds>(update, filter);
                            }
                            catch { }
                        }

                        //new Thread(delegate () {
                        GetYtComments(_ObjMongoYtFeeds.YtVideoId, apiKey, ChannelId);
                        //}).Start();
                    }
                    catch { }
                }
            }
            catch (Exception ex)
            {
            }
        }