forked from coskundeniz/ad_clicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ad_clicker.py
165 lines (125 loc) · 4.38 KB
/
ad_clicker.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
156
157
158
159
160
161
162
163
164
165
"""
BAŞLANGIÇ
Doğanın bana verdiği bu ödülden
Çıldırıp yitmemek için
İki insan gibi kaldım
Birbiriyle konuşan iki insan
-- Edip Cansever
"""
import random
import shutil
import string
import traceback
from argparse import ArgumentParser
from pathlib import Path
from config import logger, update_log_formats
from proxy import get_proxies
from utils import create_webdriver
from search_controller import SearchController
__author__ = "Coşkun Deniz <coskun.denize@gmail.com>"
def get_arg_parser() -> ArgumentParser:
"""Get argument parser
:rtype: ArgumentParser
:returns: ArgumentParser object
"""
arg_parser = ArgumentParser()
arg_parser.add_argument("-q", "--query", help="Search query")
arg_parser.add_argument(
"-l",
"--max_scroll_limit",
default=0,
type=int,
help="Number of maximum scrolls on the search results page",
)
arg_parser.add_argument("--headless", action="store_true", help="Use headless browser")
arg_parser.add_argument(
"-p",
"--proxy",
help="""Use the given proxy in "ip:port" or "username:password@host:port" format""",
)
arg_parser.add_argument(
"-pf",
"--proxy_file",
help="Select a proxy from the given file",
)
arg_parser.add_argument(
"--auth",
action="store_true",
help="""Use proxy with username and password.
If this is passed, proxy parameter should be in "username:password@host:port" format
""",
)
arg_parser.add_argument(
"-e",
"--excludes",
help="Exclude the ads that contain given words in url or title",
)
arg_parser.add_argument("--id", help="Browser id for multiprocess run")
arg_parser.add_argument("--incognito", action="store_true", help="Run in incognito mode")
arg_parser.add_argument("--poem", help="Get the poem for the given module")
return arg_parser
def get_poem(module_name: str) -> None:
"""Get the poem for the given module
:type module_name: str
:param module_name: Module name without .py extension
"""
import sys
import importlib
module = module_name if module_name != "ad_clicker" else __name__
if module in ["run_ad_clicker", "run_in_loop"]:
importlib.import_module(module)
try:
module_doc = sys.modules[module].__doc__
print(f"{module_doc}\n")
except KeyError:
logger.error("No module exists with this name!")
def main():
"""Entry point for the tool"""
arg_parser = get_arg_parser()
args = arg_parser.parse_args()
if args.poem:
get_poem(args.poem)
raise SystemExit()
if args.id:
update_log_formats(args.id)
if not args.query:
logger.error("Run with search query!")
arg_parser.print_help()
raise SystemExit()
if args.proxy:
proxy = args.proxy
elif args.proxy_file:
proxies = get_proxies(args.proxy_file)
logger.debug(f"Proxies: {proxies}")
proxy = random.choice(proxies)
else:
proxy = None
plugin_folder_name = "".join(random.choices(string.ascii_lowercase, k=5))
driver = create_webdriver(proxy, args.auth, args.headless, args.incognito, plugin_folder_name)
search_controller = None
try:
search_controller = SearchController(
driver, args.query, args.max_scroll_limit, args.excludes
)
ads = search_controller.search_for_ads()
if not ads:
logger.info("No ads in the search results!")
else:
logger.info(f"Found {len(ads)} ads")
search_controller.click_ads(ads)
except Exception as exp:
logger.error("Exception occurred. See the details in the log file.")
message = str(exp).split("\n")[0]
logger.debug(f"Exception: {message}")
details = traceback.format_tb(exp.__traceback__)
logger.debug(f"Exception details: \n{''.join(details)}")
logger.debug(f"Exception cause: {exp.__cause__}") if exp.__cause__ else None
finally:
if search_controller:
search_controller.end_search()
if proxy and args.auth:
plugin_folder = Path.cwd() / "proxy_auth_plugin" / plugin_folder_name
logger.debug(f"Removing '{plugin_folder}' folder...")
shutil.rmtree(plugin_folder, ignore_errors=True)
if __name__ == "__main__":
main()