public async Task Process() { while (IsEnabled) { using (var db = new InstaBotContext()) { var activeQueues = await db.Queues .Where(x => x.LastActivity < DateTime.UtcNow - TimeSpan.FromSeconds(x.DelayInSeconds) && x.QueueState == QueueState.InProgress && x.IsActive) .Include(x => x.LoginData) .ToListAsync(); foreach (var queue in activeQueues) { try { IInstaApi instaApi = await InstagramApiFactory.GetInstaApiAsync(new InstagramUser(queue.LoginData.Name, queue.LoginData.Password)); IInstagramExecutor instagramExecutor = InstagramServiceFactory.CreateExecutor(queue.QueueType, instaApi); await instagramExecutor.Execute(queue, db); Console.WriteLine(DateTime.Now); } catch (Exception e) { Console.WriteLine(e); //throw; } } } } }
public async Task Execute(Queue queue, InstaBotContext db) { var allGroups = queue.LoadId.Split(' '); bool isMediaPosted = false; foreach (var group in allGroups) { var instaMediaList = await _instaApi.GetUserMediaAsync(group, PaginationParameters.MaxPagesToLoad(0)); InstaMedia firstMedia = instaMediaList?.Value?.FirstOrDefault(); if (firstMedia != null && firstMedia.MediaType != InstaMediaType.Video && !IsAlreadyPosted(firstMedia.InstaIdentifier, queue.LoginData.Name, db)) { var caption = GetCaption(queue, firstMedia); isMediaPosted = await PostMediaAsync(firstMedia, caption); } if (isMediaPosted || group.Equals(allGroups.LastOrDefault())) { await UpdateQueueLastActivityAsync(queue, db); await AddFinishedQueuToHistory(firstMedia.InstaIdentifier, queue, db); Console.WriteLine($"PostMediaExecutor for {queue.LoginData.Name}"); return; } } }
private async Task <bool> PostPhotoAsync(Queue queue, InstaBotContext db, PhotoPost photoPost) { var imageURI = photoPost.PhotoURI; var success = false; var isPhotoAlreadyPosted = db.UserActivityHistories.Any(x => x.PostedImageURI == imageURI && x.Queue.QueueType == QueueType.PostMedia); if (!isPhotoAlreadyPosted) { await UploadPhotoAsync(photoPost.Caption, imageURI); queue.LastActivity = DateTime.UtcNow; db.UserActivityHistories.Add(new UserActivityHistory { Queue = queue, CreatedOn = DateTime.UtcNow, PostedImageURI = imageURI, }); await db.SaveChangesAsync(); success = true; } return(success); }
private async Task ExecuteQueueFollowRecomendations(Queue queue, InstaBotContext db) { var top = await _api.GetExploreFeedAsync(PaginationParameters.MaxPagesToLoad(0)); var firstRecomendation = top.Value.Medias.FirstOrDefault(); await _api.FollowUserAsync(firstRecomendation.User.Pk); Console.WriteLine("Followed for user - " + firstRecomendation.User.Pk); queue.LastActivity = DateTime.UtcNow; await db.SaveChangesAsync(); Thread.Sleep(1000); }
private async Task ExecuteQueueLikePhotosAsync(Queue queue, InstaBotContext db) { var tags = queue.LoadId.Split(' '); foreach (var tag in tags) { var top = await _api.GetExploreFeedAsync(PaginationParameters.MaxPagesToLoad(0)); var media = top.Value.Medias.FirstOrDefault(); //var foundPhotos = await _api.GetTagFeedAsync(tag, PaginationParameters.MaxPagesToLoad(0)); //var media = foundPhotos.Value.Medias.FirstOrDefault(); var isPhotoAlreadyLiked = db.UserActivityHistories.Any(x => x.PostedImageURI == media.InstaIdentifier && x.Queue.QueueType == QueueType.LikePhoto); if (isPhotoAlreadyLiked) { break; } await _api.LikeMediaAsync(media.InstaIdentifier); var rand = new Random(); var isNeedToComment = rand.Next(3); if (isNeedToComment == 1) { var randNumber = rand.Next(0, comments.Length); var instaComment = await _api.CommentMediaAsync(media.InstaIdentifier, comments[randNumber]); Console.WriteLine("Photo liked and commented!" + Environment.NewLine + instaComment.Info); } else { Console.WriteLine("Photo liked only!"); } queue.LastActivity = DateTime.UtcNow; db.UserActivityHistories.Add(new UserActivityHistory { Queue = queue, CreatedOn = DateTime.UtcNow, PostedImageURI = media.InstaIdentifier, }); await db.SaveChangesAsync(); Thread.Sleep(1000); } }
public async Task Execute(Queue queue, InstaBotContext db) { string tag = queue.LoadId; var instaTagFeed = await _instaApi.GetTagFeedAsync(tag, PaginationParameters.MaxPagesToLoad(0)); var lastPost = instaTagFeed.Value?.Medias?.FirstOrDefault(); if (instaTagFeed.Succeeded && lastPost != null) { await _instaApi.LikeMediaAsync(lastPost.InstaIdentifier); await UpdateQueueLastActivityAsync(queue, db); Console.WriteLine("LikeExecutor for " + queue.LoginData.Name); } }
private async Task Executer() { using (var db = new InstaBotContext()) { var activeQueues = await db.Queues .Where(x => x.LastActivity < DateTime.UtcNow - TimeSpan.FromSeconds(x.DelayInSeconds) && x.QueueState == QueueState.InProgress) .Include(x => x.LoginData) .ToListAsync(); foreach (var queue in activeQueues) { if (await LoginAsync(queue.LoginData.Name, queue.LoginData.Password)) { switch (queue.QueueType) { case QueueType.PostMedia: await ExecuteQueuePostPhotosAsync(queue, db); break; case QueueType.LikePhoto: await ExecuteQueueLikePhotosAsync(queue, db); break; case QueueType.FollowUsers: await ExecuteQueueFollowRecomendations(queue, db); break; default: break; } Console.WriteLine(DateTime.UtcNow); } else { //queue.QueueState = QueueState.StoppedWithError; queue.Notes = "LoginData can not Auth. Check Username and Password."; await db.SaveChangesAsync(); } } } }
public async Task Execute(Queue queue, InstaBotContext db) { long userId = 0; if (queue.LoadId != null) { userId = await GetUserIdByTag(queue, userId); } else { userId = await GetUserIdFromFeed(userId); } if (userId != 0) { await _instaApi.FollowUserAsync(userId); await UpdateQueueLastActivityAsync(queue, db); Console.WriteLine("FollowUserExecutor for " + queue.LoginData.Name); } }
public async Task AddFinishedQueuToHistory(string postIdentifier, Queue queue, InstaBotContext db) { db.UserActivityHistories.Add(new UserActivityHistory { Queue = queue, CreatedOn = DateTime.UtcNow, PostedImageURI = postIdentifier }); await db.SaveChangesAsync(); }
private bool IsAlreadyPosted(string id, string userName, InstaBotContext db) { return(db.UserActivityHistories.Any(x => x.PostedImageURI == id && x.Queue.QueueType == QueueType.PostMedia && x.Queue.LoginData.Name == userName)); }
private async Task ExecuteQueuePostPhotosAsync(Queue queue, InstaBotContext db) { var allGroups = queue.LoadId.Split(' '); foreach (var group in allGroups) { var photosForPost = await _api.GetUserMediaAsync(group, PaginationParameters.MaxPagesToLoad(0)); if (photosForPost.Value != null) { var firstPhotoForPost = photosForPost.Value.FirstOrDefault(); if (firstPhotoForPost.MediaType == InstaMediaType.Video) { continue; } PhotoPost photoPost = new PhotoPost { Caption = firstPhotoForPost.Caption?.Text }; if (firstPhotoForPost.Images.Any()) { photoPost.PhotoURI = firstPhotoForPost.Images.FirstOrDefault().URI; } else { photoPost.PhotoURI = firstPhotoForPost.Carousel.FirstOrDefault().Images.FirstOrDefault().URI; } var isPhotoPosted = await PostPhotoAsync(queue, db, photoPost); if (isPhotoPosted) { break; } else { if (allGroups.LastOrDefault() == group) { queue.LastActivity = DateTime.UtcNow; db.UserActivityHistories.Add(new UserActivityHistory { Queue = queue, CreatedOn = DateTime.UtcNow }); await db.SaveChangesAsync(); } } } else { if (allGroups.LastOrDefault() == group) { queue.LastActivity = DateTime.UtcNow; db.UserActivityHistories.Add(new UserActivityHistory { Queue = queue, CreatedOn = DateTime.UtcNow }); await db.SaveChangesAsync(); } } } }
protected async Task UpdateQueueLastActivityAsync(Queue queue, InstaBotContext db) { queue.LastActivity = DateTime.UtcNow; await db.SaveChangesAsync(); }