Advertisement
Guest User

Untitled

a guest
May 24th, 2019
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 5.74 KB | None | 0 0
  1. public class Instagram extends Instagram4Android {
  2.     private String deviceId;
  3.     private String username;
  4.     private String password;
  5.     private String accessToken;
  6.     private boolean isLoggedIn;
  7.     private String uuid;
  8.     private String rankToken;
  9.     private long userId;
  10.     private Response lastResponse;
  11.     private OkHttpClient client;
  12.     private final HashMap<String, Cookie> cookieStore = new HashMap<>();
  13.  
  14.     public Instagram(String username, String password) {
  15.         super(username, password);
  16.         this.username = username;
  17.         this.password = password;
  18.     }
  19.  
  20.     public void setup() {
  21.         this.deviceId = InstagramHashUtil.generateDeviceId(this.username, this.password);
  22.         this.uuid = InstagramGenericUtil.generateUuid(true);
  23.         this.client = (new Builder()).cookieJar(new CookieJar() {
  24.  
  25.             public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
  26.                 if (cookies != null)
  27.                     for (Cookie cookie : cookies)
  28.                         Instagram.this.cookieStore.put(cookie.name(), cookie);
  29.  
  30.             }
  31.  
  32.             public List<Cookie> loadForRequest(HttpUrl url) {
  33.                 ArrayList<Cookie> validCookies = new ArrayList<>();
  34.  
  35.                 for (Entry<String, Cookie> entry : Instagram.this.cookieStore.entrySet()) {
  36.                     Cookie cookie = entry.getValue();
  37.                     if (cookie.expiresAt() >= System.currentTimeMillis())
  38.                         validCookies.add(cookie);
  39.                 }
  40.  
  41.                 return validCookies;
  42.             }
  43.         }).build();
  44.     }
  45.  
  46.     public InstagramLoginResult loginFb() throws IOException {
  47.         InstagramFbLoginPayload loginRequest = InstagramFbLoginPayload.builder()
  48.                 .dryrun(true)
  49.                 .adid(InstagramGenericUtil.generateUuid(false))
  50.                 .device_id(this.deviceId)
  51.                 .fb_access_token(this.password)
  52.                 .phone_id(InstagramGenericUtil.generateUuid(false))
  53.                 .waterfall_id(InstagramGenericUtil.generateUuid(false))
  54.                 .build();
  55.  
  56.         InstagramLoginResult loginResult = this.sendRequest(new InstagramFbLoginRequest(loginRequest));
  57.         if (loginResult.getStatus().equalsIgnoreCase("ok")) {
  58.             System.out.println(this.cookieStore.toString());
  59.             this.userId = loginResult.getLogged_in_user().getPk();
  60.             this.rankToken = this.userId + "_" + this.uuid;
  61.             this.isLoggedIn = true;
  62.         }
  63.  
  64.         System.out.println("Hello! --> " + loginResult.toString());
  65.         return loginResult;
  66.     }
  67.  
  68.     public InstagramLoginResult login() throws IOException {
  69.         InstagramLoginPayload loginRequest = InstagramLoginPayload.builder()
  70.                 .username(this.username)
  71.                 .password(this.password)
  72.                 .guid(this.uuid)
  73.                 .device_id(this.deviceId)
  74.                 .phone_id(InstagramGenericUtil.generateUuid(true))
  75.                 .login_attempt_account(0)
  76.                 ._csrftoken(this.getOrFetchCsrf(null))
  77.                 .build();
  78.  
  79.         InstagramLoginResult loginResult = this.sendRequest(new InstagramLoginRequest(loginRequest));
  80.         if (loginResult.getStatus().equalsIgnoreCase("ok")) {
  81.             this.userId = loginResult.getLogged_in_user().getPk();
  82.             this.rankToken = this.userId + "_" + this.uuid;
  83.             this.isLoggedIn = true;
  84.         }
  85.  
  86.         return loginResult;
  87.     }
  88.  
  89.     public String getOrFetchCsrf(HttpUrl url) throws IOException {
  90.         Cookie cookie = this.getCsrfCookie(url);
  91.         if (cookie == null) {
  92.             this.sendRequest(new InstagramFetchHeadersRequest());
  93.             cookie = this.getCsrfCookie(url);
  94.         }
  95.  
  96.         return cookie.value();
  97.     }
  98.  
  99.     public Cookie getCsrfCookie(HttpUrl url) {
  100.         Iterator var2 = this.client.cookieJar().loadForRequest(url).iterator();
  101.  
  102.         Cookie cookie;
  103.         do {
  104.             if (!var2.hasNext()) return null;
  105.  
  106.             cookie = (Cookie) var2.next();
  107.         } while (!cookie.name().equalsIgnoreCase("csrftoken"));
  108.  
  109.         return cookie;
  110.     }
  111.  
  112.     public <T> T sendRequest(InstagramRequest<T> request) throws IOException {
  113.         if (!this.isLoggedIn && request.requiresLogin()) {
  114.             throw new IllegalStateException("Need to login first!");
  115.         } else {
  116.             request.setApi(this);
  117.             return request.execute();
  118.         }
  119.     }
  120.  
  121.     public String getDeviceId() {
  122.         return this.deviceId;
  123.     }
  124.  
  125.     public String getUsername() {
  126.         return this.username;
  127.     }
  128.  
  129.     public void setUsername(String username) {
  130.         this.username = username;
  131.     }
  132.  
  133.     public String getPassword() {
  134.         return this.password;
  135.     }
  136.  
  137.     public void setPassword(String password) {
  138.         this.password = password;
  139.     }
  140.  
  141.     public String getAccessToken() {
  142.         return this.accessToken;
  143.     }
  144.  
  145.     public void setAccessToken(String accessToken) {
  146.         this.accessToken = accessToken;
  147.     }
  148.  
  149.     public boolean isLoggedIn() {
  150.         return this.isLoggedIn;
  151.     }
  152.  
  153.     public String getUuid() {
  154.         return this.uuid;
  155.     }
  156.  
  157.     public String getRankToken() {
  158.         return this.rankToken;
  159.     }
  160.  
  161.     public void setRankToken(String rankToken) {
  162.         this.rankToken = rankToken;
  163.     }
  164.  
  165.     public long getUserId() {
  166.         return this.userId;
  167.     }
  168.  
  169.     public void setUserId(long userId) {
  170.         this.userId = userId;
  171.     }
  172.  
  173.     public Response getLastResponse() {
  174.         return this.lastResponse;
  175.     }
  176.  
  177.     public void setLastResponse(Response lastResponse) {
  178.         this.lastResponse = lastResponse;
  179.     }
  180.  
  181.     public OkHttpClient getClient() {
  182.         return this.client;
  183.     }
  184.  
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement