forked from 3clypse/milanuncios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenew.py
156 lines (125 loc) · 4.57 KB
/
renew.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import datetime
import os
import random
import re
import time
import requests
from dotenv import load_dotenv, find_dotenv
from fake_useragent import UserAgent
__author__ = "3clypse"
__copyright__ = "Copyright 2017"
__credits__ = ["3clypse", "jesuGMZ"]
__license__ = "GPL"
__version__ = "2.0"
load_dotenv(find_dotenv())
USER_AGENT = UserAgent()
DEBUG_MODE = True if os.environ.get('DEBUG') == 'True' else False
URL = {
'login': 'https://www.milanuncios.com/cmd/',
'advertisements_list': 'http://www.milanuncios.com/mis-anuncios/',
'advertisement_values': 'http://www.milanuncios.com/renovar/',
'renew': 'http://www.milanuncios.com/renovado/'
}
PAYLOAD = {
'login': {
'comando': 'login',
'email': os.environ.get('EMAIL'),
'contra': os.environ.get('PASSWORD'),
'rememberme': 's'
},
'advertisement_values': {
'id': None
},
'renew': {
'comando': 'renovar',
'a': None,
't': None,
'u': None,
'id': None
}
}
RENEW_RESPONSE = {
'renovado': 'Anuncio renovado.',
'pronto': 'Aún es pronto, debes esperar 24h entre cada renovación.',
'error': 'Error renovando el anuncio.'
}
HEADER = {'User-Agent': str(USER_AGENT.chrome)}
def time_stamped(fname="", fmt='%d/%m/%Y | %H:%M:%S'):
return datetime.datetime.now().strftime(fmt).format(fname=fname)
def login():
response = requests.post(URL['login'], data=PAYLOAD['login'],
headers=HEADER)
if DEBUG_MODE:
print(PAYLOAD['login'])
print(HEADER)
print(response.text)
return response.cookies
def get_advertisements_id(cookie):
response = requests.get(URL['advertisements_list'], cookies=cookie,
headers=HEADER)
return re.findall(r"(?<=\?idanuncio=)(\d{9})(?=&)",
response.text.encode('utf-8'))
def wait_until():
rnd = random.randint(3, 10)
print('[' + time_stamped() + '] Esperando ' + str(rnd) +
' segundos para renovar.')
time.sleep(rnd)
def get_advertisement_values(cookie, advertisement_id):
PAYLOAD['advertisement_values']['id'] = advertisement_id
response = requests.get(
URL['advertisement_values'],
params=PAYLOAD['advertisement_values'],
headers=HEADER,
cookies=cookie)
obfuscated_code = re.findall(r"(?<=unescape\(')(.+?)(?=')", response.text)
try:
obfuscated_code = obfuscated_code[1].decode('unicode-escape')
except IndexError:
print('Error al acceder al índice. Cerrando el programa...')
exit()
short_hashes = re.findall("[a-z0-9]{32}", obfuscated_code)
long_hash = re.findall("[a-z0-9]{96}", obfuscated_code)
return (short_hashes[1].encode('utf-8'), short_hashes[0].encode('utf-8'),
long_hash[0].encode('utf-8'))
def renew(cookie, advertisement_values, advertisement_id):
PAYLOAD['renew']['a'] = advertisement_values[0]
PAYLOAD['renew']['u'] = advertisement_values[1]
PAYLOAD['renew']['t'] = advertisement_values[2]
PAYLOAD['renew']['id'] = advertisement_id
response = requests.get(URL['renew'], PAYLOAD['renew'], cookies=cookie,
headers=HEADER)
return response.text
def main():
cookie = login()
if not cookie.values():
print('[' + time_stamped() + '] No se pudo iniciar sesión. Comprueba'
' las credenciales.')
else:
advertisements_id = get_advertisements_id(cookie)
number_advertisements = len(advertisements_id)
if number_advertisements == 0:
print('[' + time_stamped() + '] No tienes anuncios.')
else:
print('[' + time_stamped() + '] %d anuncios obtenidos:'
% number_advertisements)
for advertisement_id in advertisements_id:
if not DEBUG_MODE and number_advertisements > 1:
wait_until()
values = get_advertisement_values(cookie, advertisement_id)
if DEBUG_MODE:
print(values)
response = renew(cookie, values, advertisement_id)
print('[' + time_stamped() + '] Anuncio con referencia %s'
% advertisement_id + ' - ' +
RENEW_RESPONSE.get(response, 'error'))
def header():
print("============================================")
print("By: %s" % __credits__)
print(__copyright__)
print("============================================")
if __name__ == "__main__":
header()
main()