Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Entity.AccountsEntity;
- import Entity.FollowListsEntity;
- import Entity.ProxyEntity;
- import org.apache.http.HttpHost;
- import org.apache.http.auth.AuthScope;
- import org.apache.http.auth.UsernamePasswordCredentials;
- import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
- import org.apache.log4j.Logger;
- import org.brunocvcunha.instagram4j.Instagram4j;
- import org.brunocvcunha.instagram4j.requests.*;
- import org.brunocvcunha.instagram4j.requests.payload.*;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- import javax.persistence.Query;
- import java.io.*;
- import java.util.List;
- import java.net.URL;
- import java.net.URLConnection;
- import java.util.Random;
- public class Main {
- private static final Logger log = Logger.getLogger(Main.class); //create logger
- public static void main(String[] args) throws InterruptedException {
- doProcessing(); //debug
- //while (true) doProcessing(); //production
- }
- /**
- * Main method
- */
- static private void doProcessing() throws InterruptedException {
- if (isConnected()) { // if isset internet connection
- log.info("isConnected");
- // create connection to database
- SessionFactory sessions = new Configuration().configure().buildSessionFactory();
- Session session = sessions.openSession();
- session.beginTransaction();
- //select all columns from accounts table where status = 1
- Query accountsQuery = session.createQuery("from AccountsEntity where status = :code ");
- accountsQuery.setParameter("code", "1");
- List<AccountsEntity> accountsList = (List<AccountsEntity>) accountsQuery.getResultList();
- if (!accountsList.isEmpty()) { // if accountsList not empty
- log.info("!accountsList.isEmpty");
- String accountId = accountsList.get(0).getAccountId(); //user account_id
- //select all columns from proxy table where accountId = this.accountId
- Query proxyQuery = session.createQuery("from ProxyEntity where accountId = :code ");
- proxyQuery.setParameter("code", accountId);
- List<ProxyEntity> proxyList = (List<ProxyEntity>) proxyQuery.getResultList();
- String proxyUser = proxyList.get(0).getLogin();
- String proxyPass = proxyList.get(0).getPassword();
- String proxyHost = proxyList.get(0).getHost();
- String[] proxyHostPortSplit = proxyHost.split(":");
- //select all from follow_list table where accountId = this.accountId
- Query followListQuery = session.createQuery("from FollowListsEntity where accountId = :accountId ");
- followListQuery.setParameter("accountId", accountId);
- List<FollowListsEntity> followListsEntityList = (List<FollowListsEntity>) followListQuery.getResultList();
- String[] list = followListsEntityList.get(0).getList().split("::");
- if (!followListsEntityList.isEmpty()) { //if followList not empty
- log.info("!followListsEntityList.isEmpty");
- String username = accountsList.get(0).getLogin(); //user login
- String password = accountsList.get(0).getPassword(); //user password
- //MixedArray followList = Pherialize.unserialize(followListsEntityList.get(0).getList()).toArray();//accounts list to following
- int action = accountsList.get(0).getAction(); //action (1-following; 2-following&liking; 3-unfollowing)
- int startFollows = accountsList.get(0).getStartFollows(); //count for resume work on restart app
- //get cookie file for login with it
- /*ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
- "/var/www/ciliogram.loc/vendor/mgp25/instagram-php/sessions/"+username+"/"+username+"-cookies.dat"));
- //"C:/Users/Admin/Desktop/yrozenketter-cookies.dat"));
- CookieStore cookieStore = (CookieStore) ois.readObject();
- ois.close();*/
- log.info("Creating thread");
- //set status = 2 in accounts where accountId = this.accountId
- Query query = session.createQuery("update AccountsEntity set status = :status where accountId = :account");
- query.setParameter("status", "2");
- query.setParameter("account", accountId);
- query.executeUpdate();
- session.getTransaction().commit();
- Thread myThready = new Thread(() -> { //create new thread
- try {
- switch (action) {
- case 1: // if action is 1 (following)
- doFollowing(username, password, list, startFollows, proxyHostPortSplit[0], Integer.parseInt(proxyHostPortSplit[1]), proxyUser, proxyPass);
- break;
- case 2: // if action is 2 (following&liking)
- doFollowingAndLiking(username, password, list, startFollows, proxyHostPortSplit[0], Integer.parseInt(proxyHostPortSplit[1]), proxyUser, proxyPass);
- break;
- case 3: // if action = 3 (unfollowing)
- doUnfollowing(username, password, Long.parseLong(accountId), proxyHostPortSplit[0], Integer.parseInt(proxyHostPortSplit[1]), proxyUser, proxyPass);
- break;
- default: //exception
- System.exit(0); //debug
- break;
- }
- } catch (IOException | InterruptedException e) {
- e.printStackTrace();
- }
- log.info("Start thread");
- });
- myThready.start(); // start created thread
- }
- }
- }
- log.info("[Main Thread] Sleep 5 mins.");
- Thread.sleep(300000); //sleep 5 mins
- }
- /**
- * Following method
- */
- static private void doFollowing(String username, String password, String[] list, int startFollows,
- String server, int port, String proxyUser, String pass) throws IOException, InterruptedException {
- }
- /**
- * Following and liking method
- */
- static private void doFollowingAndLiking(String username, String password, String[] list, int startFollows,
- String server, int port, String proxyUser, String pass) throws IOException, InterruptedException {
- Instagram4j instagram = Instagram4j.builder().username(username).password(password).build();//create instagram object
- instagram.setup(); //setup settings before login
- //Login with proxy:
- HttpHost proxy = new HttpHost(server, port, "http");
- DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
- instagram.getClient().setRoutePlanner(routePlanner);
- instagram.getClient().getParams().setIntParameter("http.connection.timeout", 6000000);
- instagram.getClient().getCredentialsProvider().setCredentials(new AuthScope(server, port), new UsernamePasswordCredentials(proxyUser, pass));
- log.info("Using proxy: " + server + ":" + port + ", user: " + proxyUser + ", pass: " + pass);
- InstagramLoginResult loginResult = instagram.login(); //login
- if (instagram.isLoggedIn() && loginResult.getStatus().equals("ok")) { // if login = true
- log.info("isLoggedIn");
- int count = 0;
- for (int i = startFollows; i < list.length-1; i++) {//starting loop from startFollows count
- log.info("Start iteration " + i);
- SessionFactory sessions = new Configuration().configure().buildSessionFactory();
- Session session = sessions.openSession();
- session.beginTransaction();
- Query startFollowsQuery = session.createQuery("update AccountsEntity set startFollows = :startFollows where accountId = :account");
- startFollowsQuery.setParameter("startFollows", i);
- startFollowsQuery.setParameter("account", String.valueOf(instagram.getUserId()));
- startFollowsQuery.executeUpdate();
- session.getTransaction().commit();
- //get friendship status (following, followed_by)
- InstagramFriendshipStatus status = instagram.sendRequest(new InstagramGetFriendshipRequest(Long.parseLong(list[i].trim())));
- if (!status.following && !status.followed_by && !status.outgoing_request && !status.incoming_request && !status.is_private && !status.blocking) {
- log.info("Following user " + list[i]);
- StatusResult followRequest = instagram.sendRequest(new InstagramFollowRequest(Long.parseLong(list[i].trim())));//follow account from list
- count++;
- if(followRequest.isSpam()){
- setAccountStatus(instagram, "Spam block.");
- int rnd = getRandomNumberInRange(1320000, 1980000);// create random delay from 52 to 77 seconds
- log.warn("Spam block. Sleep 22-33 minutes.");
- Thread.sleep(rnd); //sleep random delay
- --i;
- count = 0;
- } else if (followRequest.getStatus().equals("fail")){
- setAccountStatus(instagram, followRequest.getFeedback_title());
- int rnd = getRandomNumberInRange(1320000, 1980000);// create random delay from 52 to 77 seconds
- log.warn(followRequest.getFeedback_title() + ". Sleep 22-33 minutes.");
- Thread.sleep(rnd); //sleep random delay
- --i;
- count = 0;
- } else if (!status.is_private && !status.isBlocking()) {
- int rnd_int = getRandomNumberInRange(15000, 25000); // create random delay from 15 to 25 seconds
- log.info("Sleep " + rnd_int / 1000 + "sec.");
- Thread.sleep(rnd_int); //sleep random delay
- //get account from list feed
- InstagramFeedResult media = instagram.sendRequest(new InstagramUserFeedRequest(Long.parseLong(list[i].trim())));
- List<InstagramFeedItem> listMedia = media.getItems(); //get items from feed
- log.info("Liking random media");
- if (listMedia.size() >= 6) { //if account have more then 5 items in feed
- //like random item from 1 to 6
- instagram.sendRequest(new InstagramLikeRequest(listMedia.get(getRandomNumberInRange(0, 5)).getPk()));
- } else if (listMedia.size() >= 3) { //if account have more then 2 items in feed
- //like random item from 1 to 3
- instagram.sendRequest(new InstagramLikeRequest(listMedia.get(getRandomNumberInRange(0, 2)).getPk()));
- } else { //if account have less then 3 items in feed
- //like first item
- instagram.sendRequest(new InstagramLikeRequest(listMedia.get(0).getPk()));
- }
- count++;
- int rnd = getRandomNumberInRange(52000, 77000);// create random delay from 52 to 77 seconds
- log.info("Sleep " + rnd / 1000 + "sec.");
- Thread.sleep(rnd); //sleep random delay
- if (count >= 60) {
- count = 0;
- Thread.sleep(getRandomNumberInRange(1320000, 1980000));
- log.info("Count > 60, sleep 22-33 minutes.");
- }
- }
- } else { //if we followed account from list OR account from list followed us
- log.info("Skipping following user " + list[i]); //skip
- }
- }
- } else {
- setAccountStatus(instagram, "Login failed.");
- }
- }
- /**
- * Unfollowing method
- */
- static private void doUnfollowing(String username, String password, Long accId,
- String server, int port, String proxyUser, String pass) throws IOException, InterruptedException {
- Instagram4j instagram = Instagram4j.builder().username(username).password(password).build();//create instagram object
- instagram.setup();//setup settings before login
- //Login with proxy:
- HttpHost proxy = new HttpHost(server, port, "http");
- DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
- instagram.getClient().setRoutePlanner(routePlanner);
- instagram.getClient().getParams().setIntParameter("http.connection.timeout", 600000);
- instagram.getClient().getCredentialsProvider().setCredentials(new AuthScope(server, port), new UsernamePasswordCredentials(proxyUser, pass));
- instagram.login();//login into account
- if (instagram.isLoggedIn()) {// if login = true
- log.info("isLoggedIn");
- //get user following list
- InstagramGetUserFollowersResult userFollowing = instagram.sendRequest(new InstagramGetUserFollowingRequest(accId));
- List<InstagramUserSummary> users = userFollowing.getUsers();
- for (InstagramUserSummary user : users) { //for this list...
- //get friendship status (following, followed_by)
- InstagramFriendshipStatus status = instagram.sendRequest(new InstagramGetFriendshipRequest(user.getPk()));
- if (status.following || status.followed_by) { //if we followed account from list OR account from list followed us
- log.info("Unfollowing user " + user.getPk());
- instagram.sendRequest(new InstagramUnfollowRequest(user.getPk())); //unfollow account from list
- int rnd = getRandomNumberInRange(52000, 77000);// create random delay from 52 to 77 seconds
- log.info("Sleep " + rnd / 1000 + "sec.");
- Thread.sleep(rnd);//sleep random delay
- } else {//if we don`t followed account from list OR account from list don`t followed us
- log.info("Skipping unfollowing user " + user.getPk()); //skip
- }
- }
- }
- }
- /**
- * Random int method
- */
- private static int getRandomNumberInRange(int min, int max) {
- if (min >= max) {
- throw new IllegalArgumentException("max must be greater than min");
- }
- Random r = new Random();
- return r.nextInt((max - min) + 1) + min;
- }
- /**
- * @return bool Connected server to internet or not
- */
- private static boolean isConnected() {
- try {
- URL url = new URL("http://google.com");
- URLConnection connection = url.openConnection();
- connection.connect();
- return true;
- } catch (Exception e) {
- return false;
- }
- }
- private static void setAccountStatus(Instagram4j instagram, String status) {
- if(status == null && status == "") {
- status = "NullStatus";
- }
- SessionFactory sessions = new Configuration().configure().buildSessionFactory();
- Session session = sessions.openSession();
- session.beginTransaction();
- Query query = session.createQuery("update AccountsEntity set status = :status where accountId = :account");
- query.setParameter("status", status);
- query.setParameter("account", String.valueOf(instagram.getUserId()));
- query.executeUpdate();
- session.getTransaction().commit();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement