-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
108 lines (73 loc) · 2.87 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
import json
import os
import requests
import sqlite3
from time import sleep
import credentials
def generate_request_payload(page):
with open(os.path.join(PROJECT_PATH, 'request_payload.json')) as json_file:
data = json.load(json_file)
if page != 1:
data['pagina'] = page
return json.dumps(data)
def make_postings_request(page):
r = requests.post(
f'{BASE_URL}{API_URL}',
headers = {'Content-Type': 'application/json'},
data = generate_request_payload(page)
)
return r.json()
def send_message(url, message):
payload = {
'chat_id' : credentials.CHAT_ID,
'text' : message,
'parse_mode': 'MarkdownV2'
}
r = requests.get(url, params=payload)
response = r.json()
if response['ok'] is not True:
error_code = response['error_code']
error_description = response['description']
send_message(url, f'*ERROR*: {error_code}\. See log for details')
print(error_code)
print(error_description)
print(payload)
print()
def send_listing(title, price, expenses, url):
message = f'*{title}*\n*Alquiler*: {price}\n*Expensas*: {expenses}\n\n[Link]({url})'
send_message(f'https://api.telegram.org/bot{credentials.BOT_TOKEN}/sendMessage', message)
# Constants
PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
BASE_URL = 'https://www.zonaprop.com.ar'
API_URL = '/rplis-api/postings'
if __name__ == '__main__':
conn = sqlite3.connect(os.path.join(PROJECT_PATH, 'database.db'))
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS postings (id text, url text, title text)''')
response = make_postings_request(1)
for page in range(1, int(response['paging']['totalPages']) + 1):
if page == 1:
current_response = response
else:
current_response = make_postings_request(page)
for post in current_response['listPostings']:
# Check if already notified
id = (post['postingId'], )
c.execute('SELECT * FROM postings WHERE id=?', id)
row = c.fetchone()
if row is None:
title = str(post['title']).replace('-', '\-').replace('.', '\.')
price = post['priceOperationTypes'][0]['prices'][0]['amount']
try:
expenses = post['expenses']['amount']
except:
expenses = ''
url = '{}{}'.format(BASE_URL, post['url'])
# Send message
send_listing(title, price, expenses, url)
# Add to notified list
row_to_save = (post['postingId'], post['url'], post['generatedTitle'])
c.execute('INSERT INTO postings VALUES (?, ?, ?)', row_to_save)
conn.commit()
sleep(10)
conn.close()