Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Diagnostics;
- using System.Net;
- using System.Net.Http;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using InstagramApiSharp.API;
- using InstagramApiSharp.API.Builder;
- using InstagramApiSharp.Classes;
- using InstagramApiSharp.Classes.SessionHandlers;
- using InstaPrimed.Models;
- namespace InstaPrimed.Login
- {
- public static class LoginProcessor
- {
- private static IInstaApi CreateApi(string username, string password, string stateFile, Proxy proxy = null)
- {
- // TODO Get instagram login from available accounts
- var userSession = new UserSessionData()
- {
- UserName = username,
- Password = password
- };
- if (proxy == null)
- {
- return InstaApiBuilder.CreateBuilder()
- .SetUser(userSession)
- //.UseHttpClientHandler(GetHttpClientHandler(proxy))
- //.SetRequestDelay(RequestDelay.FromSeconds(0, 1))
- // Session handler, set a file path to save/load your state/session data
- .SetSessionHandler(new FileSessionHandler() { FilePath = stateFile })
- .Build();
- }
- else
- {
- return InstaApiBuilder.CreateBuilder()
- .SetUser(userSession)
- .UseHttpClientHandler(GetHttpClientHandler(proxy))
- //.SetRequestDelay(RequestDelay.FromSeconds(0, 1))
- // Session handler, set a file path to save/load your state/session data
- .SetSessionHandler(new FileSessionHandler() { FilePath = stateFile })
- .Build();
- }
- }
- public static async Task<InstagramLogin> LoginToInstagram(string username, string password,
- string stateFile, Proxy proxy = null)
- {
- var instaApi = CreateApi(username, password, stateFile, proxy);
- // Load previous sessions if it exists
- instaApi?.SessionHandler?.Load();
- if (instaApi != null && instaApi.IsUserAuthenticated)
- {
- // session load logged us in
- return new InstagramLogin()
- {
- InstaApi = instaApi,
- InstaLoginResult = InstaLoginResult.Success,
- Username = username,
- Password = password
- };
- }
- if (instaApi != null && !instaApi.IsUserAuthenticated)
- {
- var logInResult = await instaApi.LoginAsync();
- Debug.WriteLine(logInResult.Value);
- if (logInResult.Succeeded)
- {
- // We logged in
- // lets save the session
- instaApi.SessionHandler.Save();
- return new InstagramLogin()
- {
- InstaApi = instaApi,
- InstaLoginResult = InstaLoginResult.Success,
- Username = username,
- Password = password
- };
- }
- if (logInResult.Value == InstaLoginResult.TwoFactorRequired)
- {
- return new InstagramLogin()
- {
- InstaApi = instaApi,
- InstaLoginResult = InstaLoginResult.TwoFactorRequired,
- Username = username,
- Password = password
- };
- }
- // Could not log in, lets see why
- if (logInResult.Value == InstaLoginResult.ChallengeRequired)
- {
- return new InstagramLogin()
- {
- InstaApi = instaApi,
- InstaLoginResult = InstaLoginResult.ChallengeRequired,
- Username = username,
- Password = password
- };
- }
- }
- return new InstagramLogin()
- {
- InstaApi = instaApi,
- InstaLoginResult = InstaLoginResult.Exception,
- Username = username,
- Password = password
- };
- }
- public static HttpClientHandler GetHttpClientHandler(Proxy p)
- {
- var proxy = new WebProxy()
- {
- Address = p.Address, //i.e: http://1.2.3.4.5:8080
- BypassProxyOnLocal = false,
- UseDefaultCredentials = false,
- // *** These creds are given to the proxy server, not the web server ***
- // We have 8,354 proxies available
- // The -NUMBER behind the username will choose on of those proxies
- // We will choose a random number between 1-8,354 to get a random proxy
- Credentials = new NetworkCredential(
- userName: p.Username,
- password: p.Password)
- };
- // Now create a client handler which uses that proxy
- var httpClientHandler = new HttpClientHandler()
- {
- Proxy = proxy,
- };
- return httpClientHandler;
- }
- public static async Task<bool> SendVerificationCodeToEmail(IInstaApi instaApi)
- {
- // check login
- await instaApi.ResetChallengeRequireVerifyMethodAsync();
- var challenge = await instaApi.GetChallengeRequireVerifyMethodAsync();
- if (challenge.Value.SubmitPhoneRequired)
- {
- return false;
- }
- var emailSend = await instaApi.RequestVerifyCodeToEmailForChallengeRequireAsync();
- return emailSend.Succeeded;
- }
- public static async Task<bool> CheckAndVerifyCode(IInstaApi instaApi, string code)
- {
- code = code.Trim();
- code = code.Replace(" ", "");
- var regex = new Regex(@"^-*[0-9,\.]+$");
- if (!regex.IsMatch(code) || code.Length != 6)
- {
- return false;
- }
- if (!regex.IsMatch(code) || code.Length != 6) return false;
- var verifyLogin = await instaApi.VerifyCodeForChallengeRequireAsync(code);
- if (!verifyLogin.Succeeded) return false;
- // save session
- instaApi.SessionHandler.Save();
- return true;
- }
- public static bool IsAuthenticated(IInstaApi instaApi)
- {
- return instaApi.IsUserAuthenticated;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement