Before we go on, please take a look REVERSE PYTHON. You can find more articles like this with UI UX design and if you liked it please share on social media or with your friends.
Currently I am interested in cryptocurrency, so I decided to create Cryptocurrency API to use it in React. Well, we need to crawl and update data continuously and avoid from long request timeout.
Installation and Configuration
Let's start with creating new project named cryptocurrencytracking and inside your project create app named trackingAPI
django-admin startproject cryptocurrencytracking
cd cryptocurrencytracking
django-admin startapp trackingAPI
and install REST Framework:
pip install djangorestframework
Once installation completed, open your settings.py and update INSTALLED_APPS.
So, I stated before we need to handle long term requests. Celery is the best choice for doing background task processing in the Python/Django ecosystem. It has a simple and clear API, and it integrates beautifully with Django. So, we are using Celery to handle the time-consuming tasks by passing them to queue to be executed in the background and always keep the server ready to respond to new requests.
To install celery run following command:
pip install Celery
Celery requires a solution to send and receive messages; usually this comes in the form of a separate service called a message broker. We will be configuring celery to use the RabbitMQ messaging system, as it provides robust, stable performance and interacts well with celery.
We can install RabbitMQ through Ubuntu’s repositories by following command:
We are going to crawl website named Coinranking and if you visit the site you can see the field names there.
Crawling Cryptocurrency Data
We will use BeautifulSoup to crawl cryptocurrency values in given URL.
Beautiful Soup is a Python library for pulling data out of HTML and XML files. It works with your favorite parser to provide idiomatic ways of navigating, searching, and modifying the parse tree. It commonly saves programmers hours or days of work. Run the following command in your terminal to install beautifulsoup:
pip install beautifulsoup4
Now, create new file named tasks.py inside our app trackingAPI.
# tasks.py
fromtimeimportsleepfromceleryimportshared_taskfrombs4importBeautifulSoupfromurllib.requestimporturlopen,Requestfrom.modelsimportCryptocurrency@shared_task# do some heavy stuff
defcrawl_currency():print('Crawling data and creating objects in database ..')req=Request('https://coinranking.com',headers={'User-Agent':'Mozilla/5.0'})html=urlopen(req).read()bs=BeautifulSoup(html,'html.parser')# Find first 5 table rows
rows=bs.find('tbody',class_="table__body").find_all('tr',class_="table__row")[0:5]forrowinrows:cryptocurrency=row.find('span',class_="profile__name").get_text().strip().replace('\n','')values=row.find_all('div',class_="valuta")price=values[0].get_text().strip().replace('\n','')market_cap=values[1].get_text().strip().replace('\n','')change=row.find('div',class_="change").find('span').get_text().strip().replace('\n','')print({'cryptocurrency':cryptocurrency,'price':price,'market_cap':market_cap,'change':change})# Create object in database from crawled data
Cryptocurrency.objects.create(cryptocurrency=cryptocurrency,price=price,market_cap=market_cap,change=change)# Sleep 3 seconds to avoid any errors
sleep(3)
@shared_task will create the independent instance of the task for each app, making task reusable. This makes the @shared_task decorator useful for libraries and reusable apps, since they will not have access to the app of the user.
As you see we are crawling our data and cleaning it from useless characters, then creating new object in database.
Once data crawled we need contentiously update these objects.
#tasks.py
@shared_taskdefupdate_currency():print('Updating data ..')req=Request('https://coinranking.com',headers={'User-Agent':'Mozilla/5.0'})html=urlopen(req).read()bs=BeautifulSoup(html,'html.parser')rows=bs.find('tbody',class_="table__body").find_all('tr',class_="table__row")[0:5]forrowinrows:cryptocurrency=row.find('span',class_="profile__name").get_text().strip().replace('\n','')values=row.find_all('div',class_="valuta")price=values[0].get_text().strip().replace('\n','')market_cap=values[1].get_text().strip().replace('\n','')change=row.find('div',class_="change").find('span').get_text().strip().replace('\n','')print({'cryptocurrency':cryptocurrency,'price':price,'market_cap':market_cap,'change':change})data={'cryptocurrency':cryptocurrency,'price':price,'market_cap':market_cap,'change':change}Cryptocurrency.objects.filter(cryptocurrency=cryptocurrency).update(**data)sleep(3)# Run this function if database is empty
ifnotCryptocurrency.objects.all():crawl_currency()whileTrue:sleep(15)update_currency()
As you see, we are crawling data every 15 seconds and updating our objects.
If you want to see the result start celery in terminal:
celery -A cryptocurrencytracking worker -l info
and go check your admin to see created objects.
Building API
Alright! Now our objects are updating and we need to create API using REST Framework.
Now, create serializers.py inside our app.
Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into JSON, XML or other content types. Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data.
Next step is building API views, so open views.py:
#views.py
fromdjango.shortcutsimportrenderfromrest_frameworkimportgenericsfrom.modelsimportCryptocurrencyfrom.serializersimportCryptocurrencySerializerclassListCryptocurrencyView(generics.ListAPIView):"""
Provides a get method handler.
"""queryset=Cryptocurrency.objects.all()serializer_class=CryptocurrencySerializer