-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
242 lines (199 loc) · 7.73 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
from datetime import datetime
from time import sleep
from random import random
from getpass import getpass
import argparse
import logging
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Global constants
BASE_URL = "http://www.milanuncios.com/mis-anuncios/"
def get_credentials():
"""Return credentials entered by the user on standard input."""
mail = input("Mail: ")
password = getpass()
return mail, password
def login(driver, mail, password):
"""Log in using the mail and password provided as arguments."""
id_box = driver.find_element_by_id('email')
id_box.clear()
id_box.send_keys(mail)
pwd_box = driver.find_element_by_id('contra')
pwd_box.clear()
pwd_box.send_keys(password)
pwd_box.send_keys(Keys.RETURN)
# Check if login failed
try:
WebDriverWait(driver, 1).until(
EC.alert_is_present()
)
logging.error("Error: Wrong credentials")
return False
except Exception:
logging.info(f"Successful login to {mail}")
return True
def can_renew(driver, id):
"""Check whether or not the ad can be renewed.
Ads can be renewed after at least 20 hours after the last renew.
This function will return True if the ad was renewed over 20 hours ago,
and False otherwise.
"""
id = "sp" + id
try:
time_text = driver.find_element_by_xpath(
f"//span[@id=\"{id}\"]//div[@class=\"x6\"]"
).text
time_value, unit = time_text.split()
time_value = int(time_value)
if unit == "día" or unit == "días":
return True
if unit == "horas" and time_value >= 20:
return True
return False
except Exception:
return False
def renew_ads_in_page(driver, id_list, sleep_time, quiet=False, random_wait=False):
"""Renew ads from a list containing the full ad IDs.
Iteratively renews all ads from the list, following human-like behavior.
A sleep time can be supplied to not raise any suspicion.
"""
# Initialize wait time variable which may change if random_wait is enabled
wait_time = sleep_time
for id in id_list:
id = id.text[1:]
# Skip ads that can't be renewed yet
if not can_renew(driver, id):
if not quiet:
logging.info(
f"Skipping ad with id {id} since it can't be renewed yet")
continue
# Get the javascript command for opening the pop-up using only the ID numbers
open_popup = f"ventana('renovar/?id=','{id}')"
driver.execute_script(open_popup)
# Switch to pop-up and click renew link
driver.switch_to.frame("ifrw")
clickable = driver.find_element_by_id("lren")
clickable.click()
# Check for any alerts, in case the ad couldn't be renewed
# In case there's an alert, close it and the pop-up
try:
alert = WebDriverWait(driver, 1).until(
EC.alert_is_present()
)
alert.accept()
except Exception:
pass
# Switch back to parent frame
driver.switch_to.default_content()
if random_wait:
# Randomly increase or decrease up to 10% of the original value
wait_time = sleep_time * (0.9 + random() * 0.2)
if not quiet:
logging.info(
f"Ad {id} renewed. Waiting {wait_time:.2f} seconds...")
sleep(wait_time)
def renew_ads(driver, sleep_time, quiet=False, random_wait=False):
"""With the driver on the first ad page, renews all ads on all pages.
A sleep time can be supplied to wait between page switching and not raise any suspicion.
"""
page = 1
while True:
# Get tags containing ad IDs
id_list = driver.find_elements_by_class_name("x5")
# If there are no ads in the page, we are done
if not id_list:
if not quiet:
logging.info("No more ads found. Finishing...")
return
# Renew all ads in the current page
if not quiet:
logging.info(
f"{len(id_list)} ads found in page {page}. Renewing...")
renew_ads_in_page(driver, id_list, sleep_time=sleep_time, quiet=quiet,
random_wait=random_wait)
# Go to the next page
if not quiet:
logging.info("Done. Going to next page.")
page += 1
driver.get(BASE_URL + "?pagina=" + str(page))
sleep(1)
def setup_logger(path=None):
# We must have write permissions on the path
if path is not None and os.access(path, os.W_OK):
# Logger will use log file in the path
filename = datetime.now().strftime('%Y%m%d%H%M') + '.log'
logging.basicConfig(filename=os.path.join(path, filename),
format='%(asctime)s:%(message)s',
datefmt='%H:%M:%S',
level=logging.INFO)
else:
# Logger will print to stdout
logging.basicConfig(format='%(asctime)s %(message)s',
datefmt='%H:%M:%S',
level=logging.INFO)
def main():
# Check python version
if sys.version_info[0] < 3:
raise Exception(
"Old Python version detected. Must be using Python 3 or newer.")
parser = argparse.ArgumentParser()
parser.add_argument('-f', type=argparse.FileType('r'),
help="read credentials from file")
parser.add_argument('--head', '-H', action='store_true',
help="don't run in headless mode")
parser.add_argument('--chrome', '-c', action='store_true',
help="use Chrome webdriver")
parser.add_argument('--quiet', '-q', action='store_true',
help="run in quiet mode")
parser.add_argument('--wait', '-w', type=float, default=1,
help="seconds to wait between ads", metavar="t")
parser.add_argument('--random-wait', action='store_true',
help="randomize wait time")
parser.add_argument(
'--log', '-l', help="path in which to save logs, if not set stdout will be used")
args = parser.parse_args()
# Setup logger
setup_logger(args.log)
# Get user credentials
if args.f:
try:
mail, password = [line.strip() for line in args.f if line.strip()]
except ValueError:
logging.error(
"Error: Credentials file must have mail and password each on a separate line!")
return
else:
mail, password = get_credentials()
# Start a Firefox or Chrome driver
if args.chrome:
options = webdriver.ChromeOptions()
options.headless = not args.head
driver = webdriver.Chrome(options=options)
else:
options = webdriver.FirefoxOptions()
options.headless = not args.head
driver = webdriver.Firefox(options=options)
if args.wait > 0:
sleep_time = args.wait
else:
if not args.quiet:
logging.warning(
"Wait time has to be positive! Using the default 1 second.")
sleep_time = 1
# Start headless driver and access user page
driver.get(BASE_URL)
# Set implicit wait in case elements are not readily available
driver.implicitly_wait(5)
if login(driver, mail, password):
# If login was successful, renew ads
renew_ads(driver, sleep_time=sleep_time,
quiet=args.quiet, random_wait=args.random_wait)
driver.quit()
if __name__ == "__main__":
main()