Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.12 KB | None | 0 0
  1. namespace HashtagIT.Services.Data
  2. {
  3.     using System.Linq;
  4.     using System.Threading.Tasks;
  5.  
  6.     using HashtagIT.Data.Common.Repositories;
  7.     using HashtagIT.Data.Models;
  8.     using InstagramApiSharp;
  9.     using InstagramApiSharp.API;
  10.     using InstagramApiSharp.API.Builder;
  11.     using InstagramApiSharp.Classes;
  12.  
  13.     public class IGService : IIGService
  14.     {
  15.         private readonly IDeletableEntityRepository<IGUser> igUsersRepository;
  16.         private IInstaApi instaApi;
  17.  
  18.         public IGService(IDeletableEntityRepository<IGUser> igUsersRepository)
  19.         {
  20.             this.igUsersRepository = igUsersRepository;
  21.             this.instaApi = InstaApiBuilder.CreateBuilder().Build();
  22.         }
  23.  
  24.         public async Task<string> Login(string userId, string username, string password)
  25.         {
  26.             var user = new UserSessionData
  27.             {
  28.                 UserName = username,
  29.                 Password = password,
  30.             };
  31.             this.instaApi = InstaApiBuilder.CreateBuilder()
  32.                 .SetUser(user).Build();
  33.  
  34.                 // login
  35.             var logInResult = await this.instaApi.LoginAsync();
  36.             if (!logInResult.Succeeded)
  37.             {
  38.                 var challenge = await this.instaApi.GetChallengeRequireVerifyMethodAsync();
  39.                 if (challenge.Succeeded)
  40.                 {
  41.                     var phoneNumber = await this.instaApi.RequestVerifyCodeToSMSForChallengeRequireAsync();
  42.                     return phoneNumber.Value.StepData.PhoneNumberPreview;
  43.                 }
  44.             }
  45.  
  46.             var followersCount = await this.instaApi.UserProcessor.GetUserFollowersAsync(username, PaginationParameters.Empty);
  47.             var followingCount = await this.instaApi.UserProcessor.GetUserFollowingAsync(username, PaginationParameters.Empty);
  48.             IGUser igUser = new IGUser
  49.             {
  50.                 UserId = userId,
  51.                 IGFullname = user.LoggedInUser.FullName,
  52.                 IGProfileId = user.LoggedInUser.Pk,
  53.                 IGUserName = user.LoggedInUser.UserName,
  54.                 ProfilePicUrl = user.LoggedInUser.ProfilePicUrl,
  55.                 FollowersCount = followersCount.Value.Count,
  56.                 FollowingCount = followingCount.Value.Count,
  57.             };
  58.             var existingUser = this.igUsersRepository.All().Where(u => u.IGUserName == igUser.IGUserName).FirstOrDefault();
  59.             if (existingUser != null)
  60.             {
  61.                 this.igUsersRepository.Update(igUser);
  62.             }
  63.             else
  64.             {
  65.                 await this.igUsersRepository.AddAsync(igUser);
  66.             }
  67.  
  68.             await this.igUsersRepository.SaveChangesAsync();
  69.             return string.Empty;
  70.         }
  71.  
  72.         public async Task<bool> TwoFactor(string code, string phoneNumber)
  73.         {
  74.             var logInResult = await this.instaApi.LoginAsync(false);
  75.             var verifyLogin = await this.instaApi.VerifyCodeForChallengeRequireAsync(code);
  76.             var info = await this.instaApi.GetLoggedInChallengeDataInfoAsync();
  77.  
  78.             return verifyLogin.Succeeded;
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement