Updates – Here is part 2 of this project
Introduction
Sweden is one of the most beautiful countries to live in, filled with nature, sustainability, awesome summer, those delicious maple pecans, kanelbulle, and various sorts of opportunities you can imagine. One other amazing thing about Sweden is that it is one of the few non-English speaking countries you can get away from without knowing Swedish and just speaking English because virtually everyone speaks good English. Despite the good that follows in this Scandinavian country, it has one major flaw – Housing shortage and excessive queuing time.
The housing market in Sweden is not that friendly to anyone. One will expect a country filled with so much economic power, technological power, and more relaxed immigration policies to foreigners should not have problems getting housing accommodation, especially in the big cities. Finding an apartment in a big city like Stockholm is an especially challenging process because of how the system works.
Renting an Apartment
Renting an apartment in Stockholm usually comes in two main options:
- First-Hand Apartment
- Second-Hand Apartment
First-Hand Apartment
First-Hand Apartments are a big deal, and when you have it, you have peace of mind, and no one will kick you anytime soon.
A first-hand contract (förstahandskontrakt) implies that the contract is in your name and signed between you and the owner or lessor of the building. In order to get an apartment with a first-hand contract, you must first register in the housing queue and pay an annual fee. You will usually have to wait in a queue for several years before getting an apartment. Most people who get an apartment have a queue time between seven and eleven years, but it can go quicker or slower than this.
Second-Hand Apartment
In second-hand apartments, you sublet from a first-hand apartment owner.
A second-hand contract (andrahandskontrakt), is the type of contract where there is a signed agreement between the tenant and the person who has a first-hand contract or owns the apartment. In other words, it’s a person that has a lease on the apartment (not the landlord) that rents it out to the tenant.
Second-hand apartments are more easily to get than first-hand apartments, but if you still hoping for a quick first-hand contract, then there is a third option called:
- Lottery First-Hand Apartment
Lottery First-Hand Apartment
These are first-hand apartments that are released to the population randomly, and you only get them by luck and not based on your queuing time. These sorts of apartments can be applied to on websites like https://wahlinfastigheter.se/.
Challenge
These lottery apartments usually come at a catch, at least those from wahlinfastigheter.se. You need to check them out frequently, and for Wahlin Fastigheter, their apartments are only released on certain days and certain times of the day. Hence, it would be best if you always kept checking, and when you found one, there is no guarantee you will get it, remember it’s a lottery. Nevertheless the odds of getting these lottery apartments are low; there is still a better chance if you applied to many than missing out entirely.
Objective
This project aimed to build a notification service that sends alerts of new apartments via SMS and emails to users to apply instantly.
System Design
Three main requirements were considered for this project and are listed below:
- Be cloud-native.
- Supports SMS and Email notifications.
- It runs at certain times and on certain days.
- Measure apartment travelling distance to a referenced location
Based on these requirements, I built this project around the AWS Serverless Infrastructure and Google API.
System Architecture
Web Scrapping
The software infrastructure is built around the Python programming language, as seen in the software section. The popular web scraping framework BeautifulSoup is used as the scraping library. The code works like every other web scraping software by breaking down the HTML pages of the lottery
washlinfastigheter website and extracting out the apartments section from the page.
The code analyzes the HTML content of the apartment page and then extracts out the apartments section, which is located within the class “blocks-wrapper carousel”, and then parse out the content. This scrapping design can also be used for other apartments providers.
Google Distance Matrix
The Apartments information are saved in the Apartment class, allowing one to give each apartment a set of unique attributes. For every discovered apartment, a travel time and distance are calculated from the apartment location to Stockholm Vasastan; this allows the user to see how far it will take to travel from this future apartment to Stockholm centre. The Google Distance Matrix API is leverage in getting this time and distance value, so a Google API key is required to use this functionality.
AWS Architecture
AWS Lambda and AWS EventBridge
The software is optimized to run on AWS lambda serverless infrastructure, which is cost-effective and highly scalable. The AWS Lambda function is kickstarted by rules defined in the AWS Eventbridge rules. The rules currently configured is shown below:
The Eventbridge rule is configured to trigger the AWS lambda function every weekday between 11:30 to 13:30 UTC+2 with an interval of 10 mins between. The reason for this is because, according to the lottery site, new apartments will be released between 11:30 to 13:30 UTC+2 every weekday, and it’s best we trigger our scrapper to scrap the site during those hours, which will also help in saving costs.
Amazon DynamoDB
AWS DynamoDB is a flexible NoSQL database and will make you fall in love working with databases. The DynamoDB database comes into the picture because the Eventbridge triggers the lambda every 10mins interval, and there are cases where the lottery apartment is still showing an already sent apartment. We don’t want to send the user the old apartments again. Whenever an apartment is discovered, a special unique ID is generated for the apartment. The unique ID is generated from the MD5 hash of the apartment URL link and the apartment title combination. This unique ID is used as the primary key in saving that apartment to the database. An apartment is registered as new or unique if its unique ID doesn’t currently exist in the dynamo DB database as at that point in time, such apartment is sent to the user and then added to the database. Only-apartments that are not found in the database are sent to the users.
Because most of the apartments information are saved in the DynamoDB, it is possible in the future to perform some machine learning to determine what sort of answers to uses in the apartment submission form and automate the whole apartment application process – but this is beyond the scope of this project.
Amazon SNS
SNS is a managed messaging service based on the Publish/Subscribe protocol. Amazon SNS is used in delivering the apartment’s contents to users that have subscribed to the service. Two types of notifications are currently being delivered: Email notification and SMS notification. The Email notification contains an itemized list of all apartments that have been found, as shown below.
The SMS notification sample is shown below:
Software
The code is ready to be fully deployed to any AWS Lambda infrastructure. When combined with a subscribed AWS Topic, you can build a notification mailing list of new apartments directly to your personal email instead of having to check the website for new apartments.
Find below the notification code –
Conclusion
Getting a first-hand contract apartment in Sweden doesn’t come easy with the long queuing lines, especially in the big cities; the fastest way to get such apartments is through the lottery option that various housing agencies offer.
This project tries to give you a head start in the lottery applications by letting you know when new apartments are released and allowing you to apply as much as possible. It’s better to apply for many than to apply for little; who knows, maybe one will click.