Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding:utf-8 -*-
- from peewee import (CharField, DateTimeField, TextField, datetime as peewee_datetime)
- import time
- import datetime
- import requests
- from bs4 import BeautifulSoup as Soup
- class SendMessageController(object):
- def __init__(self, token):
- self._token = token
- def call(self, url, id, text):
- response = requests.get(self._make_url(url, self._token, chat_id=id, text=unicode(text)), verify=False)
- if response.status_code != 200:
- raise Exception('Message doesn\'t send')
- def _make_url(self, url, token, **kwargs):
- data = u'&'.join([u'%s=%s' % (key, value) for key, value in kwargs.items()])
- return url + token + '/sendMessage?' + data
- # Telegram params
- TELEGRAM_BOT_URL = 'https://api.telegram.org/bot'
- TELEGRAM_BOT_TOKEN = ''
- TELEGRAM_ID = 24000000 # id чата или пользователя которому отправлять уведомления
- DB_CONFIG = dict(
- user='',
- password='',
- host='localhost',
- port=5432,
- database=''
- )
- db = PooledPostgresqlExtDatabase(**DB_CONFIG)
- db.commit_select = True
- db.autorollback = True
- class BaseModel(Model):
- class Meta:
- database = db
- class Flat(BaseModel):
- class Meta:
- db_table = 'flat'
- title = CharField()
- url = TextField()
- price = CharField()
- offer_datetime = CharField()
- created = DateTimeField(default=peewee_datetime.datetime.now())
- def execute(**kwargs):
- required_attrs = ("float_price_from", "float_price_to", "float_number_of_rooms_to",
- "private_business", "district_id")
- for attr in required_attrs:
- if attr not in kwargs:
- raise Exception('Missed "%s" argument in "%s"' % (attr, kwargs))
- url = 'http://olx.ua/nedvizhimost/arenda-kvartir/dolgosrochnaya-arenda-kvartir/kiev/?' \
- 'search[filter_float_price:from]=%s&search[filter_float_price:to]=%s&search' \
- '[filter_float_number_of_rooms:to]=%s&search[private_business]=%s&search[district_id]=%s' % \
- (kwargs["float_price_from"], kwargs["float_price_to"], kwargs["float_number_of_rooms_to"],
- kwargs["private_business"], kwargs["district_id"])
- send_message = SendMessageController(TELEGRAM_BOT_TOKEN)
- response = None
- try:
- response = requests.get(url)
- except Exception as e:
- send_message.call(TELEGRAM_BOT_URL, TELEGRAM_ID, e)
- if response.status_code != 200:
- send_message.call(TELEGRAM_BOT_URL, TELEGRAM_ID, 'Unexpected response code=%s' % response.status_code)
- raise Exception('Unexpected response code=%s' % response.status_code)
- result = Soup(response.text, 'html.parser')
- offer_list = result.find_all('td', 'offer')
- if not offer_list:
- raise Exception('Found 0 offers')
- if not Flat.table_exists():
- Flat.create_table()
- for offer in offer_list:
- url = offer.find_all('div', 'space rel')[0].h3.a['href']
- if url.find("#") != -1:
- url = url[:url.find("#")]
- if not Flat.select().where(Flat.url == url):
- time.sleep(0.2)
- # выбираем предложения только по опр району
- district_full = offer.find('p', 'color-9 lheight16 marginbott5').small.span.text.strip()
- if len(district_full.split(' ')) <= 1:
- continue
- district = district_full.split(' ')[1]
- offer_dist_id = 0
- for d in kiev_districts:
- if d.name == district:
- offer_dist_id = d.id
- break
- else:
- offer_dist_id = 0
- if offer_dist_id != kwargs["district_id"]:
- continue
- title = offer.find_all('div', 'space rel')[0].h3.a.strong.text
- price = offer.find('p', 'price').strong.text
- date = offer.find('p', 'color-9 lheight16 marginbott5 x-normal').text
- date = prepare_date(date)
- Flat.create(
- url=url,
- title=title,
- price=price,
- offer_datetime=date
- )
- telegram_text = u'Новое объявление\n%s\nЦена(%s) Дата добавления(%s)\nТекущая дата(%s)Ссылка(%s)' % \
- (title, price, date, datetime.datetime.now(), url)
- send_message.call(TELEGRAM_BOT_URL, TELEGRAM_ID, telegram_text)
- def prepare_date(date):
- parsed_date = date.split()
- return ' '.join(parsed_date)
- class District(object):
- def __init__(self, name, dist_id):
- self.name = name
- self.id = dist_id
- kiev_districts = [
- District(u'Голосеевский', 1),
- District(u'Дарницкий', 3),
- District(u'Деснянский', 5),
- District(u'Днепровский', 7),
- District(u'Оболонский', 9),
- District(u'Печерский', 11),
- District(u'Подольский', 13),
- District(u'Святошинский', 15),
- District(u'Соломенский', 17),
- District(u'Шевченковский', 19),
- ]
- if __name__ == '__main__':
- ("float_price_from", "float_price_to", "float_number_of_rooms_to",
- "private_business", "district_id")
- params = dict(
- float_price_from=4000,
- float_price_to=10000,
- float_number_of_rooms_to=2,
- private_business='private', # business
- district_id=9)
- while True:
- execute(**params)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement