-
Notifications
You must be signed in to change notification settings - Fork 15
/
instagram_bot.py
155 lines (140 loc) · 5.37 KB
/
instagram_bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python
import datetime
import json
import logging
import os
import random
import time
import requests
from instagram_private_api import Client
from user_agent import generate_user_agent
from constraint import (
BASE_URL,
)
class InstagramBot:
"""
Created on base of https://github.com/LevPasha/instabot.py
"""
accept_language = "ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4"
user_agent = (
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
"AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.1 "
"Safari/605.1.15"
)
def __init__(self, login, password, target, log_to_file=False):
self.bot_start = datetime.datetime.now()
self.user_login = login.lower()
self.password = password
self.user_password = password
self.target = target.split(",")
self.login_post = None
self.user_login = login
self.password = password
self.csrftoken = None
self.client = None
self.log_to_file = log_to_file
self.session = requests.Session()
self.session.cookies.update(
{
"sessionid": "",
"mid": "",
"ig_pr": "1",
"ig_vw": "1920",
"csrftoken": "",
"s_network": "",
"ds_user_id": "",
}
)
self.session.headers.update(
{
"Accept-Encoding": "gzip, deflate",
"Accept-Language": self.accept_language,
"Connection": "keep-alive",
"Content-Length": "0",
"Host": "www.instagram.com",
"Origin": "https://www.instagram.com",
"Referer": "https://www.instagram.com/accounts/login/",
"User-Agent": generate_user_agent(),
"X-Instagram-AJAX": "1",
"X-Requested-With": "XMLHttpRequest",
}
)
if self.log_to_file:
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(message)s",
)
self.logger = logging.getLogger(self.user_login)
hdrl = logging.FileHandler(f"{self.user_login}.log", mode="w")
hdrl.setFormatter(formatter)
self.logger.setLevel(level=logging.INFO)
self.logger.addHandler(hdrl)
def login(self):
self.client = Client(self.user_login, self.password)
def writer_file(self, target_file, profile_data, already_liked_nodes):
current_folder = os.path.dirname(os.path.realpath(__file__))
file_path = os.path.join(current_folder, target_file)
with open(file_path, "a") as f:
for media in profile_data["graphql"]["user"][
"edge_owner_to_timeline_media"
][
"edges"
]: # noqa
if media["node"]["id"] not in already_liked_nodes:
if not self.client:
self.login()
data = self.client.media_info(media["node"]["id"])
if not data["items"][0]["has_liked"]:
self.client.post_like(data["items"][0]["id"])
sleep_time = random.randint(5, 15)
self.write_log("Sleeping %d" % sleep_time)
time.sleep(sleep_time)
f.write(media["node"]["id"] + "\n")
def like_all_users_media(self):
""" Send http request to like target's feed """
for target in self.target:
try:
resp = self.session.get(
os.path.join(BASE_URL, target),
headers={"User-agent": self.user_agent},
)
try:
data = json.loads(
resp.text.split("window._sharedData = ")[1].split(
";</script>"
)[0],
)
except IndexError:
# catch error when The link you followed may be broken
# or the page may have been removed
continue
target_file = f"{target}.txt"
if not data["entry_data"]:
self.logger.warning(
"You are trying to access a closed account %s,"
" currently this functional does not support" % target,
)
continue
profile_data = data["entry_data"]["ProfilePage"][0]
if not os.path.exists(target_file):
with open(target_file, "w"):
already_liked_nodes = []
else:
with open(target_file) as f:
already_liked_nodes = f.read().splitlines()
self.writer_file(
target_file, profile_data, already_liked_nodes
)
except Exception as e:
self.write_log("An error occurred %s" % e)
return
def write_log(self, log_text):
if self.log_to_file:
try:
self.logger.info(log_text)
except UnicodeEncodeError:
print("Your text has unicode problem!")
else:
try:
print(log_text)
except UnicodeEncodeError:
print("Your text has unicode problem!")