Problem
Bot Public transport of Ekaterinburg currently has hundreds of active users who every day send him a geo-positions. We decided to collect some statistics and see which areas of the city are the most active. For get it required to upload all the geotags from message history.
Stack
- python
- vk api
So, how do it?
First of all, need a method to get the message history. When reading messages, you should consider the following points:
- the maximum number of messages that can be read at a time should not exceed 200
- the message read request must be made no more than once every 0.3 seconds
Note: if you do not comply with these restrictions, the bot can block. For full confidence, I set the data acquisition time to 0.5 seconds.
To get the history you need to use the method vkapi.messages.getHistory. In my case, run in a loop, getting 200 messages and then in a nested loop for check each message existence of a geo-position.
After we learned to take all the geo-positions from the history of messages of a particular user, we need to apply this method to all the dialogs. Here the same restrictions apply:
- request frequency not more than 0.3 seconds
- maximum number of dialogs at once - 200
The principle is that need to take user_id from each dialog. For read the dialogs, call the vkapi.messages.getDialogs method. The next step is to call the method getAllGeoFromHistory (vkapi, userID) for each user_id.
After uploading all the geo-positions, I spread them on the map OpenStreetMap.
This code can be modified and applied for other purposes. For example, it can be useful to search for specific phrases in messages or search phone numbers in history.
The source code can be found here.
How use it?
- Add token to .token.json
- Install python3 and vk api
- Run geoget.py
- The geodata will be recorded to file data.json