Skip to content

Instantly share code, notes, and snippets.

@gim-
Last active April 11, 2024 11:05
Show Gist options
  • Save gim-/f8f9790c74d42ef344774d9fbd7544b9 to your computer and use it in GitHub Desktop.
Save gim-/f8f9790c74d42ef344774d9fbd7544b9 to your computer and use it in GitHub Desktop.
Unfollows an account from all non-followers on Instagram. Java 8 or higher.
/*
* Copyright (c) 2017 Andrejs Mivreniks
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import org.brunocvcunha.instagram4j.Instagram4j;
import org.brunocvcunha.instagram4j.requests.InstagramGetUserFollowersRequest;
import org.brunocvcunha.instagram4j.requests.InstagramGetUserFollowingRequest;
import org.brunocvcunha.instagram4j.requests.InstagramUnfollowRequest;
import org.brunocvcunha.instagram4j.requests.payload.InstagramGetUserFollowersResult;
import org.brunocvcunha.instagram4j.requests.payload.InstagramUserSummary;
import java.io.IOException;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
/**
* Unfollows an account from all non-followers on Instagram. Requires latest version of {@link Instagram4j}.
*
* @see <a href="https://github.com/brunocvcunha/instagram4j">Instagram4j</a>
*/
public class InstagramUnfollowScript {
private static final String USERNAME = ""; // Your username
private static final String PASSWORD = ""; // Your password
private static final long DELAY_TIME_MIN = 1000L; // Minimal delay time between requests
private static final long DELAY_TIME_MAX = 20000L; // Maximal delay time between requests
private static final int LONG_DELAY_FREQ = 10; // How often to add additional delay time
private static final long LONG_DELAY_ADDED_TIME = 60000L; // Additional delay time
public static void main(String[] args) throws InterruptedException, IOException {
Instagram4j instagram = Instagram4j.builder()
.username(USERNAME)
.password(PASSWORD)
.build();
instagram.setup();
try {
instagram.login();
} catch (IOException e) {
e.printStackTrace();
return;
}
// Get my followers
long userId = instagram.getUserId();
InstagramGetUserFollowersResult myFollowers;
try {
myFollowers = instagram.sendRequest(new InstagramGetUserFollowersRequest(userId));
} catch (IOException e) {
e.printStackTrace();
return;
}
while (myFollowers.getNext_max_id() != null) {
InstagramGetUserFollowersResult newRequest = instagram.sendRequest(new InstagramGetUserFollowersRequest(userId, myFollowers.getNext_max_id()));
myFollowers.getUsers().addAll(newRequest.getUsers());
myFollowers.setNext_max_id(newRequest.getNext_max_id());
}
// Get my following
InstagramGetUserFollowersResult following;
try {
following = instagram.sendRequest(new InstagramGetUserFollowingRequest(userId));
} catch (IOException e) {
e.printStackTrace();
return;
}
// Generate list of users who to unfollow
List<InstagramUserSummary> toUnfollow = following.getUsers().stream()
.filter(u -> {
boolean result = false;
for (InstagramUserSummary u2 : myFollowers.getUsers()) {
result = u.getPk() == u2.getPk();
if (result)
break;
}
return !result;
})
.collect(Collectors.toList());
System.out.println("Followers: " + myFollowers.getUsers().size());
System.out.println("Following: " + following.users.size());
System.out.println("To unfollow: " + toUnfollow.size());
// Do job
Random random = new Random();
int counter = 0;
for (InstagramUserSummary u : toUnfollow) {
System.out.println("Unfollowing from " + u.getUsername());
instagram.sendRequest(new InstagramUnfollowRequest(u.getPk()));
long sleepTime = DELAY_TIME_MIN * (1 + random.nextInt((int) (DELAY_TIME_MAX / DELAY_TIME_MIN)));
if (counter++ % LONG_DELAY_FREQ == 0)
sleepTime += LONG_DELAY_ADDED_TIME;
System.out.println("Waiting for " + sleepTime + " ms");
Thread.sleep(sleepTime);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment