Buying a car using Python, RPI and Telegram

Some time ago I started looking for a new car. I quickly became bored with browsing the biggest polish offer site – otomoto.pl, so I decided to automate the whole thing to do it for me. Obvious, right?

The idea behind the system is rather easy. I have a Python script, which downloads new offers from otomoto.pl (for a query provided by me). The script is run every 10 mins by CRON job on RPI. It then sends to my all new offers on a Telegram channel. This way I do not have to build a new notification system, and I have immediate info on both my phone and PC.

The first step, naturally, is getting all the offers. I have divided it into 3 functions: getting list of all offers from page, getting details of one offer, getting info from all pages. All the offers will be saved in JSON file, which will be loaded at the beginning of the script. This will make it easy for me check which offers are new, and which are old.

My work begins at otomoto.pl html code analysis. Inspector (Ctrl + Shift + I) and Network view (Ctrl + Shift + E) will come in handy. I quickly realize that all the offers are in “article’ element:

clip_image002[6]

Therefore inside my first function I will navigate by “article” when using Xpath. URL variable holds all the filters from otomoto.pl I want to get e.g. younger than 2016. Next I download all the offers in “article” and check the URL. If it is not present in the file, I get all details and send a notification.

 

To download VIN and a phone number I had to check network traffic, which is generated by the website. Very quickly I discovered under which URL those features are stored. This way I will bypass any ReCaptcha and will not have to use Javascript.

clip_image003

Basing on this, I wrote a function:

 

 

To download VIN or a phone number, you just have to paste https://www.otomoto.pl/ajax/misc/vin/ + offer ID, which is at the end of any URL.

My next function retrieves all the details of an offer under “article”. In here Inspector tool is crucial. I marked red all the elements that are interesting for me:

image

I used Xpath navigation here too, as I find it the easiest. Additionally, I download car’s image from its URL locally on RPI as image.jpeg. This photo will be then send by Telegram to me.

 

Downloading multiple pages requires information about how many there are. To know this I use Inspector to find the correct place in HTML and include this in my function:

 

 

With all those pieces ready, I could start sending everything by Telegram. To do that I had to create a bot. It is a very easy process: first start a new conversation with BotFather. Write /newbot, give your bot a name and save the token you receive.

clip_image005

Using the token, I could write two additional functions. One for sending messages and the other for sending photos.

 

 

With this ready, I could save the script on RPI, which is constantly on. This way I am sure the script will be working 24/7. To make it run on schedule I used CRON job on linux. To configure that I wrote in terminal:

 

And add path to my script:

This will mean that any day, any hour that has minutes like 10, 20, 30 etc. the script will be triggered.

And this is it. This is how I created an automated process, which notifies me of any interesting offes. Example of script in action:

clip_image006

I can now be the first person to message the seller and use it as an advantage!

Below you can find the whole code. You are all welcome to comment too!

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *