This commit is contained in:
nicco 2019-01-14 19:36:22 +01:00
parent 1e7086e0cf
commit e71a95b7a3
21 changed files with 643 additions and 235 deletions

0
bot/src/__init__.py Normal file → Executable file
View File

0
bot/src/check_status.py Normal file → Executable file
View File

0
bot/src/feed_scanner.py Normal file → Executable file
View File

0
bot/src/follow_protocol.py Normal file → Executable file
View File

534
bot/src/instabot.py Normal file → Executable file
View File

@ -2,6 +2,49 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import print_function from __future__ import print_function
import os
import sys
import pickle
if (sys.version_info < (3, 0)):
# Python < 3 code in this block
print('Python v3.5 or above required for Instaloader module at the moment. Exiting...')
quit()
import importlib
try:
from pip._internal import main
except:
print('>>> Please install the latest version of pip')
#Required Dependencies and Modules, offer to install them automatically
required_modules = ['requests', 'instaloader', 'fake_useragent', 'threading']
for modname in required_modules:
try:
# try to import the module normally and put it in globals
globals()[modname] = importlib.import_module(modname)
except ImportError as e:
module_install_question = str(input("\nOne or more required modules are missing.\n Would you like to try install them automatically? (yes/no): "))
if(module_install_question == "yes" or module_install_question == "y"):
try:
result = main(['install', '-r', 'requirements.txt', '--quiet'])
if result != 0: # if pip could not install it reraise the error
print('Error installing modules. Please install manually using requirements.txt')
raise
else:
# if the install was sucessful, put modname in globals
print("Modules in requirements.txt installed successfuly. Loading...\n\n")
globals()[modname] = importlib.import_module(modname)
except:
print('Error installing modules. Please make sure you have installed the latest version of pip.\n You can install manually using requirements.txt')
raise
else:
print('Cannot continue without module ' + modname + '. Please install dependencies in requirements.txt. Exiting.')
quit()
from .unfollow_protocol import unfollow_protocol from .unfollow_protocol import unfollow_protocol
from .userinfo import UserInfo from .userinfo import UserInfo
import atexit import atexit
@ -11,21 +54,22 @@ import json
import logging import logging
import random import random
import signal import signal
import sys
import sqlite3 import sqlite3
import time import time
import requests import os
from .sql_updates import check_and_update, check_already_liked, check_already_followed import re
from .sql_updates import check_and_update, check_already_liked
from .sql_updates import check_already_followed, check_already_unfollowed
from .sql_updates import insert_media, insert_username, insert_unfollow_count from .sql_updates import insert_media, insert_username, insert_unfollow_count
from .sql_updates import get_usernames_first, get_usernames, get_username_random from .sql_updates import get_usernames_first, get_usernames, get_username_row_count, check_if_userid_exists
from .sql_updates import get_username_random, get_username_to_unfollow_random
from .sql_updates import check_and_insert_user_agent from .sql_updates import check_and_insert_user_agent
from fake_useragent import UserAgent from fake_useragent import UserAgent
import re
class InstaBot: class InstaBot:
""" """
Instagram bot v 1.2.0 Instagram bot v 1.2.1
like_per_day=1000 - How many likes set bot in one day. like_per_day=1000 - How many likes set bot in one day.
media_max_like=0 - Don't like media (photo or video) if it have more than media_max_like=0 - Don't like media (photo or video) if it have more than
@ -44,11 +88,13 @@ class InstaBot:
https://github.com/LevPasha/instabot.py https://github.com/LevPasha/instabot.py
""" """
database_name = "follows_db.db" database_name = "follows_db.db"
session_file = None
follows_db = None follows_db = None
follows_db_c = None follows_db_c = None
url = 'https://www.instagram.com/' url = 'https://www.instagram.com/'
url_tag = 'https://www.instagram.com/explore/tags/%s/?__a=1' url_tag = 'https://www.instagram.com/explore/tags/%s/?__a=1'
url_location = 'https://www.instagram.com/explore/locations/%s/?__a=1'
url_likes = 'https://www.instagram.com/web/likes/%s/like/' url_likes = 'https://www.instagram.com/web/likes/%s/like/'
url_unlike = 'https://www.instagram.com/web/likes/%s/unlike/' url_unlike = 'https://www.instagram.com/web/likes/%s/unlike/'
url_comment = 'https://www.instagram.com/web/comments/%s/add/' url_comment = 'https://www.instagram.com/web/comments/%s/add/'
@ -57,8 +103,10 @@ class InstaBot:
url_login = 'https://www.instagram.com/accounts/login/ajax/' url_login = 'https://www.instagram.com/accounts/login/ajax/'
url_logout = 'https://www.instagram.com/accounts/logout/' url_logout = 'https://www.instagram.com/accounts/logout/'
url_media_detail = 'https://www.instagram.com/p/%s/?__a=1' url_media_detail = 'https://www.instagram.com/p/%s/?__a=1'
url_media = 'https://www.instagram.com/p/%s/'
url_user_detail = 'https://www.instagram.com/%s/' url_user_detail = 'https://www.instagram.com/%s/'
api_user_detail = 'https://i.instagram.com/api/v1/users/%s/info/' api_user_detail = 'https://i.instagram.com/api/v1/users/%s/info/'
instabot_repo_update = 'https://github.com/instabot-py/instabot.py/raw/master/version.txt'
user_agent = "" "" user_agent = "" ""
accept_language = 'en-US,en;q=0.5' accept_language = 'en-US,en;q=0.5'
@ -110,6 +158,7 @@ class InstaBot:
media_on_feed = [] media_on_feed = []
media_by_user = [] media_by_user = []
login_status = False login_status = False
by_location = False
# Running Times # Running Times
start_at_h = 0, start_at_h = 0,
@ -118,22 +167,24 @@ class InstaBot:
end_at_m = 59, end_at_m = 59,
# For new_auto_mod # For new_auto_mod
next_iteration = {"Like": 0, "Follow": 0, "Unfollow": 0, "Comments": 0} next_iteration = {"Like": 0, "Follow": 0, "Unfollow": 0, "Comments": 0, "Populate": 0}
prog_run = True
def __init__(self, def __init__(self,
login, login,
password, password,
like_per_day=1000, like_per_day=1000,
media_max_like=50, media_max_like=150,
media_min_like=0, media_min_like=0,
follow_per_day=0, follow_per_day=0,
follow_time=5 * 60 * 60, follow_time=5 * 60 * 60, #Cannot be zero
unfollow_per_day=0, unfollow_per_day=0,
start_at_h=0, start_at_h=0,
start_at_m=0, start_at_m=0,
end_at_h=23, end_at_h=23,
end_at_m=59, end_at_m=59,
database_name='follows_db.db', database_name='follows_db.db',
session_file=None,
comment_list=[["this", "the", "your"], comment_list=[["this", "the", "your"],
["photo", "picture", "pic", "shot", "snapshot"], ["photo", "picture", "pic", "shot", "snapshot"],
["is", "looks", "feels", "is really"], ["is", "looks", "feels", "is really"],
@ -157,15 +208,15 @@ class InstaBot:
unwanted_username_list=[], unwanted_username_list=[],
unfollow_whitelist=[]): unfollow_whitelist=[]):
self.session_file = session_file
self.database_name = database_name self.database_name = database_name
self.follows_db = sqlite3.connect( self.follows_db = sqlite3.connect(database_name, timeout=0, isolation_level=None)
database_name, timeout=0, isolation_level=None)
self.follows_db_c = self.follows_db.cursor() self.follows_db_c = self.follows_db.cursor()
check_and_update(self) check_and_update(self)
fake_ua = UserAgent() fake_ua = UserAgent()
self.user_agent = check_and_insert_user_agent( self.user_agent = check_and_insert_user_agent(self, str(fake_ua.random))
self, str(fake_ua.random))
self.bot_start = datetime.datetime.now() self.bot_start = datetime.datetime.now()
self.bot_start_ts = time.time()
self.start_at_h = start_at_h self.start_at_h = start_at_h
self.start_at_m = start_at_m self.start_at_m = start_at_m
self.end_at_h = end_at_h self.end_at_h = end_at_h
@ -176,6 +227,7 @@ class InstaBot:
self.tag_blacklist = tag_blacklist self.tag_blacklist = tag_blacklist
self.unfollow_whitelist = unfollow_whitelist self.unfollow_whitelist = unfollow_whitelist
self.comment_list = comment_list self.comment_list = comment_list
self.instaloader = instaloader.Instaloader()
self.time_in_day = 24 * 60 * 60 self.time_in_day = 24 * 60 * 60
# Like # Like
@ -184,7 +236,7 @@ class InstaBot:
self.like_delay = self.time_in_day / self.like_per_day self.like_delay = self.time_in_day / self.like_per_day
# Follow # Follow
self.follow_time = follow_time self.follow_time = follow_time #Cannot be zero
self.follow_per_day = follow_per_day self.follow_per_day = follow_per_day
if self.follow_per_day != 0: if self.follow_per_day != 0:
self.follow_delay = self.time_in_day / self.follow_per_day self.follow_delay = self.time_in_day / self.follow_per_day
@ -211,6 +263,7 @@ class InstaBot:
# log_mod 0 to console, 1 to file # log_mod 0 to console, 1 to file
self.log_mod = log_mod self.log_mod = log_mod
self.s = requests.Session() self.s = requests.Session()
self.c = requests.Session()
# if you need proxy make something like this: # if you need proxy make something like this:
# self.s.proxies = {"https" : "http://proxyip:proxyport"} # self.s.proxies = {"https" : "http://proxyip:proxyport"}
# by @ageorgios # by @ageorgios
@ -220,6 +273,7 @@ class InstaBot:
'https': 'http://' + proxy, 'https': 'http://' + proxy,
} }
self.s.proxies.update(proxies) self.s.proxies.update(proxies)
self.c.proxies.update(proxies)
# convert login to lower # convert login to lower
self.user_login = login.lower() self.user_login = login.lower()
self.user_password = password self.user_password = password
@ -227,15 +281,32 @@ class InstaBot:
self.media_by_tag = [] self.media_by_tag = []
self.media_on_feed = [] self.media_on_feed = []
self.media_by_user = [] self.media_by_user = []
self.current_user_info = ''
self.unwanted_username_list = unwanted_username_list self.unwanted_username_list = unwanted_username_list
now_time = datetime.datetime.now() now_time = datetime.datetime.now()
log_string = 'Instabot v1.2.0 started at %s:\n' % \ self.check_for_bot_update()
log_string = 'Instabot v1.2.1/0 started at %s:' % \
(now_time.strftime("%d.%m.%Y %H:%M")) (now_time.strftime("%d.%m.%Y %H:%M"))
self.write_log(log_string) self.write_log(log_string)
self.login() self.login()
self.populate_user_blacklist() self.populate_user_blacklist()
signal.signal(signal.SIGINT, self.cleanup)
signal.signal(signal.SIGTERM, self.cleanup) signal.signal(signal.SIGTERM, self.cleanup)
atexit.register(self.cleanup) atexit.register(self.cleanup)
self.instaload = instaloader.Instaloader()
def check_for_bot_update(self):
self.write_log('Checking for updates...')
try:
updated_timestamp = self.c.get(self.instabot_repo_update) #CHANGE THIS TO OFFICIAL REPO IF KEPT
current_version_timestamp = open('version.txt','r')
if(int(updated_timestamp.text) > int(current_version_timestamp.read()) ):
self.write_log('>>> UPDATE AVAILABLE <<< Please update Instabot. You are running an older version.')
else:
self.write_log('You are running the latest stable version')
except:
self.write_log('Could not check for updates')
def populate_user_blacklist(self): def populate_user_blacklist(self):
for user in self.user_blacklist: for user in self.user_blacklist:
@ -260,19 +331,15 @@ class InstaBot:
time.sleep(5 * random.random()) time.sleep(5 * random.random())
def login(self): def login(self):
log_string = 'Trying to login as %s...\n' % (self.user_login) log_string = 'Trying to login as %s...' % (self.user_login)
self.write_log(log_string)
self.login_post = { successfulLogin = False
'username': self.user_login,
'password': self.user_password
}
self.s.headers.update({ self.s.headers.update({
'Accept': '*/*', 'Accept': '*/*',
'Accept-Language': self.accept_language, 'Accept-Language': self.accept_language,
'Accept-Encoding': 'gzip, deflate, br', 'Accept-Encoding': 'gzip, deflate, br',
'Connection': 'keep-alive', 'Connection': 'keep-alive',
'Content-Length': '0',
'Host': 'www.instagram.com', 'Host': 'www.instagram.com',
'Origin': 'https://www.instagram.com', 'Origin': 'https://www.instagram.com',
'Referer': 'https://www.instagram.com/', 'Referer': 'https://www.instagram.com/',
@ -282,33 +349,137 @@ class InstaBot:
'X-Requested-With': 'XMLHttpRequest' 'X-Requested-With': 'XMLHttpRequest'
}) })
if self.session_file != None and os.path.isfile(self.session_file):
self.write_log("Found session file %s" % self.session_file)
successfulLogin = True
with open(self.session_file, 'rb') as i:
cookies = pickle.load(i)
self.s.cookies.update(cookies)
else:
self.write_log(log_string)
self.login_post = {
'username': self.user_login,
'password': self.user_password
}
r = self.s.get(self.url) r = self.s.get(self.url)
csrf_token = re.search('(?<=\"csrf_token\":\")\w+', r.text).group(0) csrf_token = re.search('(?<=\"csrf_token\":\")\w+', r.text).group(0)
self.s.headers.update({'X-CSRFToken': csrf_token}) self.s.headers.update({'X-CSRFToken': csrf_token})
time.sleep(5 * random.random()) time.sleep(5 * random.random())
login = self.s.post( login = self.s.post(
self.url_login, data=self.login_post, allow_redirects=True) self.url_login, data=self.login_post, allow_redirects=True)
self.s.headers.update({'X-CSRFToken': login.cookies['csrftoken']}) loginResponse = login.json()
successfulLogin = login.status_code == 200
try:
if successfulLogin:
self.csrftoken = login.cookies['csrftoken'] self.csrftoken = login.cookies['csrftoken']
self.s.headers.update({'X-CSRFToken': login.cookies['csrftoken']})
except:
self.write_log('Something wrong with login')
self.write_log(login.text)
if loginResponse.get('errors'):
self.write_log('Something is wrong with Instagram! Please try again later...')
for error in loginResponse['errors']['error']:
self.write_log('Error => ' + error)
return
if loginResponse.get('message') == 'checkpoint_required':
try:
if 'instagram.com' in loginResponse['checkpoint_url']:
challenge_url = loginResponse['checkpoint_url']
else:
challenge_url = 'https://instagram.com' + loginResponse['checkpoint_url']
self.write_log('Challenge required at ' + challenge_url)
with self.s as clg:
clg.headers.update({
'Accept': '*/*',
'Accept-Language': self.accept_language,
'Accept-Encoding': 'gzip, deflate, br',
'Connection': 'keep-alive',
'Host': 'www.instagram.com',
'Origin': 'https://www.instagram.com',
'User-Agent': self.user_agent,
'X-Instagram-AJAX': '1',
'Content-Type': 'application/x-www-form-urlencoded',
'x-requested-with': "XMLHttpRequest",
})
#Get challenge page
challenge_request_explore = clg.get(challenge_url)
#Get CSRF Token from challenge page
challenge_csrf_token = re.search('(?<=\"csrf_token\":\")\w+', challenge_request_explore.text).group(0)
#Get Rollout Hash from challenge page
rollout_hash = re.search('(?<=\"rollout_hash\":\")\w+', challenge_request_explore.text).group(0)
#Ask for option 1 from challenge, which is usually Email or Phone
challenge_post = {
'choice': 1
}
#Update headers for challenge submit page
clg.headers.update({'X-CSRFToken': challenge_csrf_token})
clg.headers.update({'Referer': challenge_url})
#Request instagram to send a code
challenge_request_code = clg.post(challenge_url, data=challenge_post, allow_redirects=True)
#User should receive a code soon, ask for it
challenge_userinput_code = input("Challenge Required.\n\nEnter the code sent to your mail/phone: ")
challenge_security_post = {
'security_code': challenge_userinput_code
}
clg.headers.update({'Content-Type': 'application/x-www-form-urlencoded'})
complete_challenge = clg.post(challenge_url, data=challenge_security_post, allow_redirects=True)
if complete_challenge.status_code != 200:
self.write_log('Entered code is wrong, Try again later!')
return
self.csrftoken = complete_challenge.cookies['csrftoken']
self.s.headers.update({'X-CSRFToken': self.csrftoken, 'X-Instagram-AJAX': '1'})
successfulLogin = complete_challenge.status_code == 200
except Exception as err:
print("Login failed, response: \n\n" + login.text, err)
quit()
else:
rollout_hash = re.search('(?<=\"rollout_hash\":\")\w+', r.text).group(0)
self.s.headers.update({'X-Instagram-AJAX': rollout_hash})
#ig_vw=1536; ig_pr=1.25; ig_vh=772; ig_or=landscape-primary; #ig_vw=1536; ig_pr=1.25; ig_vh=772; ig_or=landscape-primary;
self.s.cookies['csrftoken'] = self.csrftoken
self.s.cookies['ig_vw'] = '1536' self.s.cookies['ig_vw'] = '1536'
self.s.cookies['ig_pr'] = '1.25' self.s.cookies['ig_pr'] = '1.25'
self.s.cookies['ig_vh'] = '772' self.s.cookies['ig_vh'] = '772'
self.s.cookies['ig_or'] = 'landscape-primary' self.s.cookies['ig_or'] = 'landscape-primary'
time.sleep(5 * random.random()) time.sleep(5 * random.random())
if login.status_code == 200:
if successfulLogin:
r = self.s.get('https://www.instagram.com/') r = self.s.get('https://www.instagram.com/')
self.csrftoken = re.search('(?<=\"csrf_token\":\")\w+', r.text).group(0)
self.s.cookies['csrftoken'] = self.csrftoken
self.s.headers.update({'X-CSRFToken': self.csrftoken})
finder = r.text.find(self.user_login) finder = r.text.find(self.user_login)
if finder != -1: if finder != -1:
ui = UserInfo() ui = UserInfo()
self.user_id = ui.get_user_id_by_login(self.user_login) self.user_id = ui.get_user_id_by_login(self.user_login)
self.login_status = True self.login_status = True
log_string = '%s login success!' % (self.user_login) log_string = "%s login success!\n" % (self.user_login)
self.write_log(log_string) self.write_log(log_string)
if self.session_file != None:
self.write_log("Saving cookies to session file %s" % self.session_file)
with open(self.session_file, 'wb') as output:
pickle.dump(self.s.cookies, output, pickle.HIGHEST_PROTOCOL)
else: else:
self.login_status = False self.login_status = False
self.write_log('Login error! Check your login data!') self.write_log('Login error! Check your login data!')
if self.session_file != None and os.path.isfile(self.session_file):
try:
os.remove(self.session_file)
except:
self.write_log("Could not delete session file. Please delete manually")
self.prog_run = False;
else: else:
self.write_log('Login error! Connection error!') self.write_log('Login error! Connection error!')
@ -333,7 +504,12 @@ class InstaBot:
def cleanup(self, *_): def cleanup(self, *_):
# Unfollow all bot follow # Unfollow all bot follow
if self.follow_counter >= self.unfollow_counter: if self.follow_counter >= self.unfollow_counter:
for f in self.bot_follow_list: for i in range(len(self.bot_follow_list)):
f = self.bot_follow_list[0]
if check_already_unfollowed(self, f[0]):
log_string = "Already unfollowed before, skipping: %s" % (f[0])
self.write_log(log_string)
else:
log_string = "Trying to unfollow: %s" % (f[0]) log_string = "Trying to unfollow: %s" % (f[0])
self.write_log(log_string) self.write_log(log_string)
self.unfollow_on_cleanup(f[0]) self.unfollow_on_cleanup(f[0])
@ -346,22 +522,42 @@ class InstaBot:
self.bot_follow_list.remove(f) self.bot_follow_list.remove(f)
# Logout # Logout
if self.login_status: if self.login_status and self.session_file == None:
self.logout() self.logout()
self.prog_run = False
def get_media_id_by_tag(self, tag): def get_media_id_by_tag(self, tag):
""" Get media ID set, by your hashtag """ """ Get media ID set, by your hashtag or location """
if self.login_status: if self.login_status:
log_string = "Get media id by tag: %s" % (tag) if tag.startswith('l:'):
tag = tag.replace('l:', '')
self.by_location = True
log_string = "Get Media by location: %s" % (tag)
self.write_log(log_string)
if self.login_status == 1:
url_location = self.url_location % (tag)
try:
r = self.s.get(url_location)
all_data = json.loads(r.text)
self.media_by_tag = list(all_data['graphql']['location']['edge_location_to_media']['edges'])
except:
self.media_by_tag = []
self.write_log("Except on get_media!")
logging.exception("get_media_id_by_tag")
else:
return 0
else:
log_string = "Get Media by tag: %s" % (tag)
self.by_location = False
self.write_log(log_string) self.write_log(log_string)
if self.login_status == 1: if self.login_status == 1:
url_tag = self.url_tag % (tag) url_tag = self.url_tag % (tag)
try: try:
r = self.s.get(url_tag) r = self.s.get(url_tag)
all_data = json.loads(r.text) all_data = json.loads(r.text)
self.media_by_tag = list( self.media_by_tag = list(all_data['graphql']['hashtag']['edge_hashtag_to_media']['edges'])
all_data['graphql']['hashtag']['edge_hashtag_to_media']['edges'])
except: except:
self.media_by_tag = [] self.media_by_tag = []
self.write_log("Except on get_media!") self.write_log("Except on get_media!")
@ -372,33 +568,28 @@ class InstaBot:
def get_instagram_url_from_media_id(self, media_id, url_flag=True, only_code=None): def get_instagram_url_from_media_id(self, media_id, url_flag=True, only_code=None):
""" Get Media Code or Full Url from Media ID Thanks to Nikished """ """ Get Media Code or Full Url from Media ID Thanks to Nikished """
media_id = int(media_id) media_id = int(media_id)
if url_flag is False: if url_flag is False: return ""
return ""
else: else:
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_' alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
shortened_id = '' shortened_id = ''
while media_id > 0: while media_id > 0:
media_id, idx = divmod(media_id, 64) media_id, idx = divmod(media_id, 64)
shortened_id = alphabet[idx] + shortened_id shortened_id = alphabet[idx] + shortened_id
if only_code: if only_code: return shortened_id
return shortened_id else: return 'instagram.com/p/' + shortened_id + '/'
else:
return 'instagram.com/p/' + shortened_id + '/'
def get_username_by_media_id(self, media_id): def get_username_by_media_id(self, media_id):
""" Get username by media ID Thanks to Nikished """ """ Get username by media ID Thanks to Nikished """
if self.login_status: if self.login_status:
if self.login_status == 1: if self.login_status == 1:
media_id_url = self.get_instagram_url_from_media_id( media_id_url = self.get_instagram_url_from_media_id(int(media_id), only_code=True)
int(media_id), only_code=True)
url_media = self.url_media_detail % (media_id_url) url_media = self.url_media_detail % (media_id_url)
try: try:
r = self.s.get(url_media) r = self.s.get(url_media)
all_data = json.loads(r.text) all_data = json.loads(r.text)
username = str( username = str(all_data['graphql']['shortcode_media']['owner']['username'])
all_data['graphql']['shortcode_media']['owner']['username'])
self.write_log("media_id=" + media_id + ", media_id_url=" + self.write_log("media_id=" + media_id + ", media_id_url=" +
media_id_url + ", username_by_media_id=" + username) media_id_url + ", username_by_media_id=" + username)
return username return username
@ -409,13 +600,10 @@ class InstaBot:
return "" return ""
def get_username_by_user_id(self, user_id): def get_username_by_user_id(self, user_id):
""" Get username by user_id """
if self.login_status: if self.login_status:
try: try:
url_info = self.api_user_detail % user_id profile = instaloader.Profile.from_id(self.instaload.context, user_id)
r = self.s.get(url_info, headers="") username = profile.username
all_data = json.loads(r.text)
username = all_data["user"]["username"]
return username return username
except: except:
logging.exception("Except on get_username_by_user_id") logging.exception("Except on get_username_by_user_id")
@ -437,8 +625,7 @@ class InstaBot:
follower = user_info['followed_by']['count'] follower = user_info['followed_by']['count']
follow_viewer = user_info['follows_viewer'] follow_viewer = user_info['follows_viewer']
if follower > 3000 or follows > 1500: if follower > 3000 or follows > 1500:
self.write_log( self.write_log(' >>>This is probably Selebgram, Business or Fake account')
' >>>This is probably Selebgram, Business or Fake account')
if follow_viewer: if follow_viewer:
return None return None
return user_info return user_info
@ -481,8 +668,7 @@ class InstaBot:
"Keep calm - It's your own media ;)") "Keep calm - It's your own media ;)")
return False return False
if check_already_liked(self, media_id=self.media_by_tag[i]['node']['id']) == 1: if check_already_liked(self, media_id=self.media_by_tag[i]['node']['id']) == 1:
self.write_log( self.write_log("Keep calm - It's already liked ;)")
"Keep calm - It's already liked ;)")
return False return False
try: try:
if (len(self.media_by_tag[i]['node']['edge_media_to_caption']['edges']) > 1): if (len(self.media_by_tag[i]['node']['edge_media_to_caption']['edges']) > 1):
@ -515,15 +701,13 @@ class InstaBot:
+ matching_tags) + matching_tags)
return False return False
except: except:
logging.exception( logging.exception("Except on like_all_exist_media")
"Except on like_all_exist_media")
return False return False
log_string = "Trying to like media: %s" % \ log_string = "Trying to like media: %s" % \
(self.media_by_tag[i]['node']['id']) (self.media_by_tag[i]['node']['id'])
self.write_log(log_string) self.write_log(log_string)
like = self.like( like = self.like(self.media_by_tag[i]['node']['id'])
self.media_by_tag[i]['node']['id'])
# comment = self.comment(self.media_by_tag[i]['id'], 'Cool!') # comment = self.comment(self.media_by_tag[i]['id'], 'Cool!')
# follow = self.follow(self.media_by_tag[i]["owner"]["id"]) # follow = self.follow(self.media_by_tag[i]["owner"]["id"])
if like != 0: if like != 0:
@ -645,6 +829,7 @@ class InstaBot:
log_string = "Unfollowed: %s #%i." % (user_id, log_string = "Unfollowed: %s #%i." % (user_id,
self.unfollow_counter) self.unfollow_counter)
self.write_log(log_string) self.write_log(log_string)
insert_unfollow_count(self, user_id=user_id)
return unfollow return unfollow
except: except:
logging.exception("Exept on unfollow!") logging.exception("Exept on unfollow!")
@ -661,6 +846,7 @@ class InstaBot:
log_string = "Unfollow: %s #%i of %i." % ( log_string = "Unfollow: %s #%i of %i." % (
user_id, self.unfollow_counter, self.follow_counter) user_id, self.unfollow_counter, self.follow_counter)
self.write_log(log_string) self.write_log(log_string)
insert_unfollow_count(self, user_id=user_id)
else: else:
log_string = "Slow Down - Pausing for 5 minutes so we don't get banned!" log_string = "Slow Down - Pausing for 5 minutes so we don't get banned!"
self.write_log(log_string) self.write_log(log_string)
@ -672,6 +858,7 @@ class InstaBot:
user_id, self.unfollow_counter, user_id, self.unfollow_counter,
self.follow_counter) self.follow_counter)
self.write_log(log_string) self.write_log(log_string)
insert_unfollow_count(self, user_id=user_id)
else: else:
log_string = "Still no good :( Skipping and pausing for another 5 minutes" log_string = "Still no good :( Skipping and pausing for another 5 minutes"
self.write_log(log_string) self.write_log(log_string)
@ -686,18 +873,19 @@ class InstaBot:
def auto_mod(self): def auto_mod(self):
""" Star loop, that get media ID by your tag list, and like it """ """ Star loop, that get media ID by your tag list, and like it """
if self.login_status: if self.login_status:
while True: while self.prog_run:
random.shuffle(self.tag_list) random.shuffle(self.tag_list)
self.get_media_id_by_tag(random.choice(self.tag_list)) self.get_media_id_by_tag(random.choice(self.tag_list))
self.like_all_exist_media(random.randint self.like_all_exist_media(random.randint \
(1, self.max_like_for_one_tag)) (1, self.max_like_for_one_tag))
self.write_log("Exit Program... GoodBye")
sys.exit(0)
def new_auto_mod(self): def new_auto_mod(self):
while True: while self.prog_run and self.login_status:
now = datetime.datetime.now() now = datetime.datetime.now()
if ( if (
datetime.time(self.start_at_h, datetime.time(self.start_at_h, self.start_at_m) <= now.time()
self.start_at_m) <= now.time()
and now.time() <= datetime.time(self.end_at_h, self.end_at_m) and now.time() <= datetime.time(self.end_at_h, self.end_at_m)
): ):
# ------------------- Get media_id ------------------- # ------------------- Get media_id -------------------
@ -719,9 +907,11 @@ class InstaBot:
time.sleep(3) time.sleep(3)
# print("Tic!") # print("Tic!")
else: else:
print("sleeping until {hour}:{min}".format(hour=self.start_at_h, print("!!sleeping until {hour}:{min}".format(hour=self.start_at_h,
min=self.start_at_m), end="\r") min=self.start_at_m), end="\r")
time.sleep(100) time.sleep(100)
self.write_log("Exit Program... GoodBye")
sys.exit(0)
def remove_already_liked(self): def remove_already_liked(self):
self.write_log("Removing already liked medias..") self.write_log("Removing already liked medias..")
@ -745,23 +935,30 @@ class InstaBot:
if self.this_tag_like_count >= self.max_tag_like_count: if self.this_tag_like_count >= self.max_tag_like_count:
self.media_by_tag = [0] self.media_by_tag = [0]
# Del first media_id # Del first media_id
try:
del self.media_by_tag[0] del self.media_by_tag[0]
except:
print('Could not remove media')
def new_auto_mod_follow(self): def new_auto_mod_follow(self):
if time.time() < self.next_iteration["Follow"]:
return
if time.time() > self.next_iteration["Follow"] and \ if time.time() > self.next_iteration["Follow"] and \
self.follow_per_day != 0 and len(self.media_by_tag) > 0: self.follow_per_day != 0 and len(self.media_by_tag) > 0:
if self.media_by_tag[0]['node']["owner"]["id"] == self.user_id: if self.media_by_tag[0]['node']["owner"]["id"] == self.user_id:
self.write_log("Keep calm - It's your own profile ;)") self.write_log("Keep calm - It's your own profile ;)")
return return
if check_already_followed(self, user_id=self.media_by_tag[0]['node']["owner"]["id"]) == 1: if check_already_followed(self, user_id=self.media_by_tag[0]['node']["owner"]["id"]) == 1:
self.write_log("Already followed before " + self.write_log("Already followed before " + self.media_by_tag[0]['node']["owner"]["id"])
self.media_by_tag[0]['node']["owner"]["id"])
self.next_iteration["Follow"] = time.time() + \ self.next_iteration["Follow"] = time.time() + \
self.add_time(self.follow_delay/2) self.add_time(self.follow_delay/2)
return return
log_string = "Trying to follow: %s" % ( log_string = "Trying to follow: %s" % (
self.media_by_tag[0]['node']["owner"]["id"]) self.media_by_tag[0]['node']["owner"]["id"])
self.write_log(log_string) self.write_log(log_string)
self.next_iteration["Follow"] = time.time() + \
self.add_time(self.follow_delay)
if self.follow(self.media_by_tag[0]['node']["owner"]["id"]) != False: if self.follow(self.media_by_tag[0]['node']["owner"]["id"]) != False:
self.bot_follow_list.append( self.bot_follow_list.append(
@ -769,11 +966,49 @@ class InstaBot:
self.next_iteration["Follow"] = time.time() + \ self.next_iteration["Follow"] = time.time() + \
self.add_time(self.follow_delay) self.add_time(self.follow_delay)
def populate_from_feed(self):
self.get_media_id_recent_feed()
try:
for mediafeed_user in self.media_on_feed:
feed_username = mediafeed_user["node"]["owner"]["username"]
feed_user_id = mediafeed_user["node"]["owner"]["id"]
#print(check_if_userid_exists(self, userid=feed_user_id))
if check_if_userid_exists(self, userid=feed_user_id) is False:
insert_username(self, user_id=feed_user_id, username=feed_username)
self.write_log("Inserted user "+feed_username+" from recent feed")
except:
self.write_log("Notice: could not populate from recent feed")
def new_auto_mod_unfollow(self): def new_auto_mod_unfollow(self):
if time.time() > self.next_iteration["Unfollow"] and self.unfollow_per_day != 0: if time.time() > self.next_iteration["Unfollow"] and self.unfollow_per_day != 0:
if self.bot_mode == 0:
log_string = "Trying to unfollow #%i: " % ( if (time.time() - self.bot_start_ts) < 30:
self.unfollow_counter + 1) #let bot initialize
return
if get_username_row_count(self) < 20:
self.write_log(' >>>Waiting for database to populate before unfollowing (progress '+str(get_username_row_count(self))+"/20)\n (Will try to populate using recent feed)")
self.populate_from_feed()
self.next_iteration["Unfollow"] = time.time() + \
(self.add_time(self.unfollow_delay)/2)
return #DB doesn't have enough followers yet
if self.bot_mode == 0 or self.bot_mode == 3:
try:
if time.time() > self.next_iteration["Populate"]:
self.populate_from_feed()
self.next_iteration["Populate"] = time.time() + \
(self.add_time(360))
except:
self.write_log('Notice: Could not populate from recent feed right now')
log_string = "Trying to unfollow #%i: " % (self.unfollow_counter + 1)
self.write_log(log_string) self.write_log(log_string)
self.auto_unfollow() self.auto_unfollow()
self.next_iteration["Unfollow"] = time.time() + \ self.next_iteration["Unfollow"] = time.time() + \
@ -786,8 +1021,7 @@ class InstaBot:
and len(self.media_by_tag) > 0 \ and len(self.media_by_tag) > 0 \
and self.check_exisiting_comment(self.media_by_tag[0]['node']['shortcode']) == False: and self.check_exisiting_comment(self.media_by_tag[0]['node']['shortcode']) == False:
comment_text = self.generate_comment() comment_text = self.generate_comment()
log_string = "Trying to comment: %s" % ( log_string = "Trying to comment: %s" % (self.media_by_tag[0]['node']['id'])
self.media_by_tag[0]['node']['id'])
self.write_log(log_string) self.write_log(log_string)
if self.comment(self.media_by_tag[0]['node']['id'], comment_text) != False: if self.comment(self.media_by_tag[0]['node']['id'], comment_text) != False:
self.next_iteration["Comments"] = time.time() + \ self.next_iteration["Comments"] = time.time() + \
@ -807,17 +1041,20 @@ class InstaBot:
return res.capitalize() return res.capitalize()
def check_exisiting_comment(self, media_code): def check_exisiting_comment(self, media_code):
url_check = self.url_media_detail % (media_code) url_check = self.url_media % (media_code)
check_comment = self.s.get(url_check) check_comment = self.s.get(url_check)
if check_comment.status_code == 200: if check_comment.status_code == 200:
all_data = json.loads(check_comment.text) all_data = json.loads(re.search('window._sharedData = (.*?);', check_comment.text, re.DOTALL).group(1))['entry_data']['PostPage'][0] #window._sharedData = (.*?);
if all_data['graphql']['shortcode_media']['owner']['id'] == self.user_id: if all_data['graphql']['shortcode_media']['owner']['id'] == self.user_id:
self.write_log("Keep calm - It's your own media ;)") self.write_log("Keep calm - It's your own media ;)")
# Del media to don't loop on it # Del media to don't loop on it
del self.media_by_tag[0] del self.media_by_tag[0]
return True return True
comment_list = list( try:
all_data['graphql']['shortcode_media']['edge_media_to_comment']['edges']) comment_list = list(all_data['graphql']['shortcode_media']['edge_media_to_comment']['edges'])
except:
comment_list = list(all_data['graphql']['shortcode_media']['edge_media_to_parent_comment']['edges'])
for d in comment_list: for d in comment_list:
if d['node']['owner']['id'] == self.user_id: if d['node']['owner']['id'] == self.user_id:
self.write_log("Keep calm - Media already commented ;)") self.write_log("Keep calm - Media already commented ;)")
@ -826,15 +1063,14 @@ class InstaBot:
return True return True
return False return False
else: else:
insert_media(self, self.media_by_tag[0]['node']['id'], str( insert_media(self, self.media_by_tag[0]['node']['id'], str(check_comment.status_code))
check_comment.status_code))
self.media_by_tag.remove(self.media_by_tag[0]) self.media_by_tag.remove(self.media_by_tag[0])
return False return False
def auto_unfollow(self): def auto_unfollow(self):
checking = True checking = True
while checking: while checking:
username_row = get_username_random(self) username_row = get_username_to_unfollow_random(self)
if not username_row: if not username_row:
self.write_log("Looks like there is nobody to unfollow.") self.write_log("Looks like there is nobody to unfollow.")
return False return False
@ -864,8 +1100,13 @@ class InstaBot:
url_tag = self.url_user_detail % (current_user) url_tag = self.url_user_detail % (current_user)
try: try:
r = self.s.get(url_tag) r = self.s.get(url_tag)
all_data = json.loads(re.search( if r.text.find('The link you followed may be broken, or the page may have been removed.') != -1:
'{"activity.+show_app', r.text, re.DOTALL).group(0)+'":""}')['entry_data']['ProfilePage'][0] log_string = "Looks like account was deleted, skipping : %s" % current_user
self.write_log(log_string)
insert_unfollow_count(self, user_id=current_id)
time.sleep(3)
return False
all_data = json.loads(re.search('window._sharedData = (.*?);</script>', r.text, re.DOTALL).group(1))['entry_data']['ProfilePage'][0]
user_info = all_data['graphql']['user'] user_info = all_data['graphql']['user']
i = 0 i = 0
@ -891,37 +1132,38 @@ class InstaBot:
if follows == 0 or follower / follows > 2: if follows == 0 or follower / follows > 2:
self.is_selebgram = True self.is_selebgram = True
self.is_fake_account = False self.is_fake_account = False
print(' >>>This is probably Selebgram account') self.write_log(' >>>This is probably Selebgram account')
elif follower == 0 or follows / follower > 2: elif follower == 0 or follows / follower > 2:
self.is_fake_account = True self.is_fake_account = True
self.is_selebgram = False self.is_selebgram = False
print(' >>>This is probably Fake account') self.write_log(' >>>This is probably Fake account')
else: else:
self.is_selebgram = False self.is_selebgram = False
self.is_fake_account = False self.is_fake_account = False
print(' >>>This is a normal account') self.write_log(' >>>This is a normal account')
if media > 0 and follows / media < 25 and follower / media < 25: if media > 0 and follows / media < 25 and follower / media < 25:
self.is_active_user = True self.is_active_user = True
print(' >>>This user is active') self.write_log(' >>>This user is active')
else: else:
self.is_active_user = False self.is_active_user = False
print(' >>>This user is passive') self.write_log(' >>>This user is passive')
if follow_viewer or has_requested_viewer: if follow_viewer or has_requested_viewer:
self.is_follower = True self.is_follower = True
print(" >>>This account is following you") self.write_log(" >>>This account is following you")
else: else:
self.is_follower = False self.is_follower = False
print(' >>>This account is NOT following you') self.write_log(' >>>This account is NOT following you')
if followed_by_viewer or requested_by_viewer: if followed_by_viewer or requested_by_viewer:
self.is_following = True self.is_following = True
print(' >>>You are following this account') self.write_log(' >>>You are following this account')
else: else:
self.is_following = False self.is_following = False
print(' >>>You are NOT following this account') self.write_log(' >>>You are NOT following this account')
except: except:
logging.exception("Except on auto_unfollow!") logging.exception("Except on auto_unfollow!")
@ -938,21 +1180,137 @@ class InstaBot:
): ):
self.write_log(current_user) self.write_log(current_user)
self.unfollow(current_id) self.unfollow(current_id)
# don't insert unfollow count as it is done now inside unfollow()
#insert_unfollow_count(self, user_id=current_id)
elif self.is_following is not True:
# we are not following this account, hence we unfollowed it, let's keep track
insert_unfollow_count(self, user_id=current_id) insert_unfollow_count(self, user_id=current_id)
def unfollow_recent_feed(self):
if len(self.media_on_feed) == 0:
self.get_media_id_recent_feed()
if len(self.media_on_feed) != 0 and self.is_follower_number < 5 and time.time() > self.next_iteration["Unfollow"] and self.unfollow_per_day != 0:
self.get_media_id_recent_feed()
chooser = random.randint(0, len(self.media_on_feed) - 1)
self.current_user = self.media_on_feed[chooser]["node"]["owner"]["username"]
self.current_id = self.media_on_feed[chooser]["node"]["owner"]["id"]
current_user = self.current_user
current_id = self.current_id
if self.login_status:
log_string = "Getting user info : %s" % current_user
self.write_log(log_string)
if self.login_status == 1:
url_tag = self.url_user_detail % (current_user)
try:
r = self.s.get(url_tag)
if r.text.find('The link you followed may be broken, or the page may have been removed.') != -1:
log_string = "Looks like account was deleted, skipping : %s" % current_user
self.write_log(log_string)
insert_unfollow_count(self, user_id=current_id)
time.sleep(3)
return False
all_data = json.loads(re.search('window._sharedData = (.*?);</script>', r.text, re.DOTALL).group(1))['entry_data']['ProfilePage'][0]
user_info = all_data['graphql']['user']
i = 0
log_string = "Checking user info.."
self.write_log(log_string)
follows = user_info['edge_follow']['count']
follower = user_info['edge_followed_by']['count']
media = user_info['edge_owner_to_timeline_media']['count']
follow_viewer = user_info['follows_viewer']
followed_by_viewer = user_info[
'followed_by_viewer']
requested_by_viewer = user_info[
'requested_by_viewer']
has_requested_viewer = user_info[
'has_requested_viewer']
log_string = "Follower : %i" % (follower)
self.write_log(log_string)
log_string = "Following : %s" % (follows)
self.write_log(log_string)
log_string = "Media : %i" % (media)
self.write_log(log_string)
if follows == 0 or follower / follows > 2:
self.is_selebgram = True
self.is_fake_account = False
self.write_log(' >>>This is probably Selebgram account')
elif follower == 0 or follows / follower > 2:
self.is_fake_account = True
self.is_selebgram = False
self.write_log(' >>>This is probably Fake account')
else:
self.is_selebgram = False
self.is_fake_account = False
self.write_log(' >>>This is a normal account')
if media > 0 and follows / media < 25 and follower / media < 25:
self.is_active_user = True
self.write_log(' >>>This user is active')
else:
self.is_active_user = False
self.write_log(' >>>This user is passive')
if follow_viewer or has_requested_viewer:
self.is_follower = True
self.write_log(" >>>This account is following you")
else:
self.is_follower = False
self.write_log(' >>>This account is NOT following you')
if followed_by_viewer or requested_by_viewer:
self.is_following = True
self.write_log(' >>>You are following this account')
else:
self.is_following = False
self.write_log(' >>>You are NOT following this account')
except:
logging.exception("Except on auto_unfollow!")
time.sleep(3)
return False
else:
return False
if (
self.is_selebgram is not False
or self.is_fake_account is not False
or self.is_active_user is not True
or self.is_follower is not True
):
self.write_log(current_user)
self.unfollow(current_id)
self.next_iteration["Unfollow"] = time.time() + self.add_time(self.unfollow_delay)
# don't insert unfollow count as it is done now inside unfollow()
#insert_unfollow_count(self, user_id=current_id)
elif self.is_following is not True:
# we are not following this account, hence we unfollowed it, let's keep track
insert_unfollow_count(self, user_id=current_id)
time.sleep(8)
def get_media_id_recent_feed(self): def get_media_id_recent_feed(self):
if self.login_status: if self.login_status:
now_time = datetime.datetime.now() now_time = datetime.datetime.now()
log_string = "%s : Get media id on recent feed" % (self.user_login) log_string = "%s : Get media id on recent feed" % (self.user_login)
self.write_log(log_string) self.write_log(log_string)
if self.login_status == 1: if self.login_status == 1:
url_tag = 'https://www.instagram.com/?__a=1' url_tag = 'https://www.instagram.com/'
try: try:
r = self.s.get(url_tag) r = self.s.get(url_tag)
all_data = json.loads(r.text)
jsondata = re.search('additionalDataLoaded\(\'feed\',({.*})\);', r.text).group(1)
all_data = json.loads(jsondata.strip())
self.media_on_feed = list( self.media_on_feed = list(
all_data['graphql']['user']['edge_web_feed_timeline'][ all_data['user']['edge_web_feed_timeline'][
'edges']) 'edges'])
log_string = "Media in recent feed = %i" % ( log_string = "Media in recent feed = %i" % (

0
bot/src/likers_graber_protocol.py Normal file → Executable file
View File

7
bot/src/likers_protocol.py Normal file → Executable file
View File

@ -15,9 +15,10 @@ def likers_protocol(self):
self.current_index, len(self.media_by_user)) self.current_index, len(self.media_by_user))
self.write_log(log_string) self.write_log(log_string)
if self.media_by_user[self.
current_index]["likes"]["count"] >= 10 and self.media_by_user[self.
current_index]["likes"]["count"] < 100: if self.media_by_user[self.current_index]["likes"]["count"] >= 10 \
and self.media_by_user[self.current_index]["likes"]["count"] < 100:
get_user_id_post_page( get_user_id_post_page(
self, self.media_by_user[self.current_index]["code"]) self, self.media_by_user[self.current_index]["code"])
username_checker(self) username_checker(self)

0
bot/src/new_auto_mod_like2.py Normal file → Executable file
View File

16
bot/src/new_auto_mod_likeall.py Normal file → Executable file
View File

@ -1,17 +1,21 @@
import pprint
def new_like_all_exist_media(self): def new_like_all_exist_media(self):
i = self.current_index i = self.current_index
# Media count by this user. # Media count by this user.
l_c = self.media_by_user[i]['likes']['count'] #print(*self.media_by_user[i]['node'])
#quit()
l_c = self.media_by_user[i]['node']['edge_liked_by']['count']
if l_c <= self.media_max_like and l_c >= self.media_min_like: if l_c <= self.media_max_like and l_c >= self.media_min_like:
log_string = "Trying to like media: %s" %\ log_string = "Trying to like media: %s" %\
(self.media_by_user[i]['id']) (self.media_by_user[i]['node']['id'])
self.write_log(log_string) self.write_log(log_string)
like = self.like(self.media_by_user[i]['id']) like = self.like(self.media_by_user[i]['node']['id'])
if like != 0: if like != 0:
if like.status_code == 200: if like.status_code == 200:
log_string = "Liked: %s. Likes: #%i." %\ log_string = "Liked: %s. Likes: #%i." %\
(self.media_by_user[i]['id'], (self.media_by_user[i]['node']['id'],
self.media_by_user[i]['likes']['count']) self.media_by_user[i]['node']['edge_liked_by']['count'])
self.write_log(log_string) self.write_log(log_string)
elif like.status_code == 400: elif like.status_code == 400:
log_string = "Not liked: %i" \ log_string = "Not liked: %i" \
@ -25,5 +29,5 @@ def new_like_all_exist_media(self):
else: else:
return False return False
else: else:
print('Too much liker for this media!!! LC = %i' % (l_c)) print('Too many likes on this media LC = %i' % (l_c))
return True return True

0
bot/src/new_auto_mod_unfollow2.py Normal file → Executable file
View File

2
bot/src/new_unfollow.py Normal file → Executable file
View File

@ -1,6 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from .sql_updates import insert_unfollow_count
def new_unfollow(self, user_id, user_name): def new_unfollow(self, user_id, user_name):
""" Send http request to unfollow """ """ Send http request to unfollow """
@ -12,6 +13,7 @@ def new_unfollow(self, user_id, user_name):
log_string = "Unfollow: %s #%i." % (user_name, log_string = "Unfollow: %s #%i." % (user_name,
self.unfollow_counter) self.unfollow_counter)
self.write_log(log_string) self.write_log(log_string)
insert_unfollow_count(self, user_id=user_id)
return unfollow return unfollow
except: except:
self.write_log("Exept on unfollow!") self.write_log("Exept on unfollow!")

0
bot/src/post_page.py Normal file → Executable file
View File

11
bot/src/recent_feed.py Normal file → Executable file
View File

@ -3,6 +3,7 @@
import datetime import datetime
import json import json
import time import time
import re
def get_media_id_recent_feed(self): def get_media_id_recent_feed(self):
@ -11,12 +12,16 @@ def get_media_id_recent_feed(self):
log_string = "%s : Get media id on recent feed \n %s" % ( log_string = "%s : Get media id on recent feed \n %s" % (
self.user_login, now_time.strftime("%d.%m.%Y %H:%M")) self.user_login, now_time.strftime("%d.%m.%Y %H:%M"))
self.write_log(log_string) self.write_log(log_string)
url = 'https://www.instagram.com/?__a=1' url = 'https://www.instagram.com/'
try: try:
r = self.s.get(url) r = self.s.get(url)
all_data = json.loads(r.text) jsondata = re.search('additionalDataLoaded\(\'feed\',({.*})\);', r.text).group(1)
all_data = json.loads(jsondata.strip())
self.media_on_feed = list(all_data['graphql']['user']['edge_web_feed_timeline']['edges']) self.media_on_feed = list(all_data['user']['edge_web_feed_timeline']['edges'])
log_string = "Media in recent feed = %i" % ( log_string = "Media in recent feed = %i" % (
len(self.media_on_feed)) len(self.media_on_feed))
self.write_log(log_string) self.write_log(log_string)

50
bot/src/sql_updates.py Normal file → Executable file
View File

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import sqlite3 import sqlite3
from datetime import datetime, time from datetime import datetime, time, timedelta
def check_and_update(self): def check_and_update(self):
""" At the Program start, i does look for the sql updates """ """ At the Program start, i does look for the sql updates """
@ -23,7 +23,7 @@ def check_and_update(self):
if not table_column_status: if not table_column_status:
qry = """ qry = """
CREATE TABLE "usernames_new" ( `username_id` varchar ( 300 ), `username` TEXT ); CREATE TABLE "usernames_new" ( `username_id` varchar ( 300 ), `username` TEXT );
INSERT INTO "usernames_new" (username_id) Select username from usernames; INSERT INTO "usernames_new" (username) Select username from usernames;
DROP TABLE "usernames"; DROP TABLE "usernames";
ALTER TABLE "usernames_new" RENAME TO "usernames"; ALTER TABLE "usernames_new" RENAME TO "usernames";
""" """
@ -61,6 +61,13 @@ def check_already_followed(self, user_id):
return 1 return 1
return 0 return 0
def check_already_unfollowed(self, user_id):
""" controls if user was already unfollowed before """
if self.follows_db_c.execute("SELECT EXISTS(SELECT 1 FROM usernames WHERE username_id='"+
user_id + "' AND unfollow_count > 0 LIMIT 1)").fetchone()[0] > 0:
return 1
return 0
def insert_media(self, media_id, status): def insert_media(self, media_id, status):
""" insert media to medias """ """ insert media to medias """
now = datetime.now() now = datetime.now()
@ -112,6 +119,45 @@ def get_username_random(self):
else: else:
return False return False
def get_username_to_unfollow_random(self):
""" Gets random username that is older than follow_time and has zero unfollow_count """
now_time = datetime.now()
cut_off_time = now_time - timedelta(seconds=self.follow_time)
username = self.follows_db_c.execute("SELECT * FROM usernames WHERE \
DATETIME(last_followed_time) < DATETIME('"+str(cut_off_time)+"') \
AND unfollow_count=0 ORDER BY RANDOM() LIMIT 1").fetchone()
if username:
return username
else:
username = self.follows_db_c.execute("SELECT * FROM usernames WHERE \
unfollow_count=0 ORDER BY RANDOM() LIMIT 1").fetchone()
if username:
return username
else:
return False
def get_username_row_count(self):
""" Gets the number of usernames in table """
count = self.follows_db_c.execute("select count(*) from usernames").fetchone()
if count:
return count[0]
else:
return False
def check_if_userid_exists(self, userid):
""" Checks if username exists """
#print("select count(*) from usernames WHERE username_id = " + userid)
count = self.follows_db_c.execute("select count(*) from usernames WHERE username_id = " + userid).fetchone()
if count:
if count[0] > 0:
return True
else:
return False
else:
return False
def check_and_insert_user_agent(self, user_agent): def check_and_insert_user_agent(self, user_agent):
""" Check user agent """ """ Check user agent """
qry = "SELECT settings_val from settings where settings_name = 'USERAGENT'" qry = "SELECT settings_val from settings where settings_name = 'USERAGENT'"

0
bot/src/unfollow_protocol.py Normal file → Executable file
View File

0
bot/src/unfollowpub.py Normal file → Executable file
View File

13
bot/src/user_feed.py Normal file → Executable file
View File

@ -15,22 +15,13 @@ def get_media_id_user_feed(self):
if self.is_checked != True: if self.is_checked != True:
get_user_info(self, self.current_user) get_user_info(self, self.current_user)
if self.is_fake_account != True and self.is_active_user != False and self.is_selebgram != True or self.is_by_tag != False: if self.is_fake_account != True and self.is_active_user != False and self.is_selebgram != True or self.is_by_tag != False:
url = 'https://www.instagram.com/%s/?__a=1' % (self.current_user) url = 'https://www.instagram.com/%s/' % (self.current_user)
else:
log_string = "======> Get media id by Tag <======"
url = 'https://www.instagram.com/explore/tags/%s/?__a=1' % (
random.choice(self.tag_list))
self.write_log(log_string) self.write_log(log_string)
if self.login_status == 1 and self.is_fake_account != True and self.is_active_user != False and self.is_selebgram != True or self.is_by_tag != False: if self.login_status == 1 and self.is_fake_account != True and self.is_active_user != False and self.is_selebgram != True or self.is_by_tag != False:
try: try:
r = self.s.get(url)
all_data = json.loads(r.text)
if self.is_by_tag != True: self.media_by_user = list(self.current_user_info['edge_owner_to_timeline_media']['edges'])
self.media_by_user = list(all_data['user']['media']['nodes'])
else:
self.media_by_user = list(all_data['graphql']['hashtag']['edge_hashtag_to_media']['edges'])
log_string = "Get media by user success!" log_string = "Get media by user success!"
self.write_log(log_string) self.write_log(log_string)
except: except:

2
bot/src/user_feed_protocol.py Normal file → Executable file
View File

@ -22,7 +22,7 @@ def user_feed_protocol(self):
return 0 return 0
if self.is_follower is not False: if self.is_follower is not False:
print( print(
"@@@@@@@@@@@@@@ This is your follower B****h!!! @@@@@@@@@@@@@") "This is your follower")
self.is_follower_number += 1 self.is_follower_number += 1
time.sleep(5) time.sleep(5)
return return

79
bot/src/user_info.py Normal file → Executable file
View File

@ -4,38 +4,40 @@ import datetime
import json import json
import random import random
import time import time
import re
def get_user_info(self, username): def get_user_info(self, username):
if self.login_status: current_user = username
now_time = datetime.datetime.now() log_string = "Getting user info : %s" % current_user
log_string = "%s : Get user info \n%s" % (
self.user_login, now_time.strftime("%d.%m.%Y %H:%M"))
self.write_log(log_string) self.write_log(log_string)
if self.login_status == 1: if self.login_status == 1:
url = 'https://www.instagram.com/%s/?__a=1' % (username) url_tag = self.url_user_detail % (current_user)
try: if self.login_status == 1:
r = self.s.get(url) r = self.s.get(url_tag)
if r.text.find('The link you followed may be broken, or the page may have been removed.') != -1:
user_info = json.loads(r.text) log_string = "Looks like account was deleted, skipping : %s" % current_user
self.write_log(log_string)
insert_unfollow_count(self, user_id=current_id)
time.sleep(3)
return False
all_data = json.loads(re.search('window._sharedData = (.*?);</script>', r.text, re.DOTALL).group(1))['entry_data']['ProfilePage'][0]
user_info = all_data['graphql']['user']
self.current_user_info = user_info
i = 0
log_string = "Checking user info.." log_string = "Checking user info.."
self.write_log(log_string) self.write_log(log_string)
follows = user_info['user']['follows']['count'] follows = user_info['edge_follow']['count']
follower = user_info['user']['followed_by']['count'] follower = user_info['edge_followed_by']['count']
if self.is_self_checking is not False: media = user_info['edge_owner_to_timeline_media']['count']
self.self_following = follows follow_viewer = user_info['follows_viewer']
self.self_follower = follower followed_by_viewer = user_info[
self.is_self_checking = False 'followed_by_viewer']
self.is_checked = True requested_by_viewer = user_info[
return 0
media = user_info['user']['media']['count']
follow_viewer = user_info['user']['follows_viewer']
followed_by_viewer = user_info['user']['followed_by_viewer']
requested_by_viewer = user_info['user'][
'requested_by_viewer'] 'requested_by_viewer']
has_requested_viewer = user_info['user'][ has_requested_viewer = user_info[
'has_requested_viewer'] 'has_requested_viewer']
log_string = "Follower : %i" % (follower) log_string = "Follower : %i" % (follower)
self.write_log(log_string) self.write_log(log_string)
@ -43,45 +45,44 @@ def get_user_info(self, username):
self.write_log(log_string) self.write_log(log_string)
log_string = "Media : %i" % (media) log_string = "Media : %i" % (media)
self.write_log(log_string) self.write_log(log_string)
if follows == 0 or follower / follows > 2: if follows == 0 or follower / follows > 2:
self.is_selebgram = True self.is_selebgram = True
self.is_fake_account = False self.is_fake_account = False
print(' >>>This is probably Selebgram account') self.write_log(' >>>This is probably Selebgram account')
elif follower == 0 or follows / follower > 2: elif follower == 0 or follows / follower > 2:
self.is_fake_account = True self.is_fake_account = True
self.is_selebgram = False self.is_selebgram = False
print(' >>>This is probably Fake account') self.write_log(' >>>This is probably Fake account')
else: else:
self.is_selebgram = False self.is_selebgram = False
self.is_fake_account = False self.is_fake_account = False
print(' >>>This is a normal account') self.write_log(' >>>This is a normal account')
if media > 0 and follows / media < 10 and follower / media < 10: if media > 0 and follows / media < 25 and follower / media < 25:
self.is_active_user = True self.is_active_user = True
print(' >>>This user is active') self.write_log(' >>>This user is active')
else: else:
self.is_active_user = False self.is_active_user = False
print(' >>>This user is passive') self.write_log(' >>>This user is passive')
if follow_viewer or has_requested_viewer: if follow_viewer or has_requested_viewer:
self.is_follower = True self.is_follower = True
print(" >>>This account is following you") self.write_log(" >>>This account is following you")
else: else:
self.is_follower = False self.is_follower = False
print(' >>>This account is NOT following you') self.write_log(' >>>This account is NOT following you')
if followed_by_viewer or requested_by_viewer: if followed_by_viewer or requested_by_viewer:
self.is_following = True self.is_following = True
print(' >>>You are following this account') self.write_log(' >>>You are following this account')
else: else:
self.is_following = False self.is_following = False
print(' >>>You are NOT following this account') self.write_log(' >>>You are NOT following this account')
self.is_checked = True
except: else:
self.media_on_feed = [] logging.exception("Except on auto_unfollow!")
self.write_log("Except on get_info!") time.sleep(3)
time.sleep(20) return False
return 0
else: else:
return 0 return 0

2
bot/src/userinfo.py Normal file → Executable file
View File

@ -39,7 +39,7 @@ class UserInfo:
def get_user_id_by_login(self, user_name): def get_user_id_by_login(self, user_name):
url_info = self.url_user_info % (user_name) url_info = self.url_user_info % (user_name)
info = self.s.get(url_info) info = self.s.get(url_info)
json_info = json.loads(re.search('{"activity.+show_app', info.text, re.DOTALL).group(0)+'":""}') json_info = json.loads(re.search('window._sharedData = (.*?);</script>', info.text, re.DOTALL).group(1))
id_user = json_info['entry_data']['ProfilePage'][0]['graphql']['user']['id'] id_user = json_info['entry_data']['ProfilePage'][0]['graphql']['user']['id']
return id_user return id_user

0
bot/src/username_checker.py Normal file → Executable file
View File