-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
130 lines (92 loc) · 4.1 KB
/
main.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
import random, argparse, shutil, datetime
from tools import instagram
from tools.instagram import actions, exceptions
from tools import statistics, config
from pathlib import Path
from tools.logger import Logger
def handle_args():
# Define the program
parser = argparse.ArgumentParser(prog="Instagram PyBot",
usage='python main.py <command> <dirpath>',
description="Instagram Bot written in Python and Selenium. It can like, comment, follow and unfollow.")
parser.version = '0.1.0'
# Define arguments
subparsers = parser.add_subparsers(metavar="COMMANDS:", dest="command", required=True)
init_parser = subparsers.add_parser("init", usage="python main.py init <dirpath>", help="Init a folder with settings for the bot")
init_parser.set_defaults(func=init)
init_parser.add_argument("dirpath", help="Path to folder where setting files for the bot will be initiated")
start_parser = subparsers.add_parser("start", usage="python main.py start <dirpath>", help="Start the bot")
start_parser.set_defaults(func=start)
start_parser.add_argument("dirpath", help="Path to folder with bot settings and data")
# Parse arguments
args = parser.parse_args()
config.set_dirpath(args.dirpath)
return args
def main():
args = handle_args()
# Start init or start function
args.func(args)
def init(args):
if Path(args.dirpath).exists():
answer = input("Specified dirpath already exists in filesystem. All files from this folder will be deleted and replaced by default settings.\nDo you want to continue? y/[n]: ").strip().lower()
if answer == 'y' or answer == 'yes':
shutil.rmtree(args.dirpath)
else:
exit()
shutil.copytree(Path(__file__).parent / "sample", args.dirpath)
def start(args):
if not Path(args.dirpath).exists():
print("[ERROR] Specified dirpath does not exist!")
exit()
config.check_json_config()
if config.data.web_browser_driver == "":
print("[ERROR]: web_browser_driver parameter wasn't specified in neither config file nor command line arguments.")
exit()
Logger.getInstance()
while True:
config.check_json_config()
if not (datetime.datetime.now().hour > config.data.start_hour and datetime.datetime.now().hour < config.data.end_hour):
actions.sleep()
continue
instagram.actions.driver_init()
# Log In
instagram.actions.log_in()
instagram.actions.get_following_count()
instagram.actions.get_followers_count()
ActionBlock = False
def run():
nonlocal ActionBlock
try: instagram.actions.like_likelist(3)
except instagram.exceptions.LimitReached:
return
except instagram.exceptions.ActionBlock:
actions.change_site_main()
ActionBlock = True
return
SITES = []
with open(Path(config.data.sites_file), encoding="UTF-8") as f:
SITES = [line.strip() for line in f.readlines()]
while len(SITES) > 0:
site = SITES.pop(random.randint(0, len(SITES)-1))
instagram.actions.change_site(site)
try:
instagram.actions.work_on_site()
except instagram.exceptions.LimitReached:
return
except instagram.exceptions.ActionBlock:
actions.change_site_main()
ActionBlock = True
return
run()
if not ActionBlock:
try: instagram.actions.unfollow_in_profile()
except instagram.exceptions.ActionBlock:
instagram.actions.change_site_profile()
ActionBlock = True
instagram.actions.change_site_profile()
instagram.actions.log_out()
instagram.actions.driver_close()
if ActionBlock:
config.delete_cookies()
instagram.actions.sleep()
main()