I want to create a view function inside views.py file, which run on a particular interval of time, without depending on a request object is that possible in django I am doing a simple project on crawling the web data using bs4, request and django, up to now, I am able on crawling the data and present it to my django views.py .
Crawled data from the different website follow the following format
news_title = 'were-these-remote-wild-islands'
news_url = 'http://bbc.co.uk/travel/see-the-dark-side-of-climate-change'
and my view function has the following line of code
from .bbc import bbc_crawler
from .models import News
def collect_data(request):
'''
aggregrate all the news from each
news portal
'''
allnews = []
#return dict obj {'title':'climate change', 'url':'http://bbc.co.uk'}, {'title':'t', 'url':'http://url.com'}
allnews.append(bbc_crawler())
for news in allnews:
for eachnews,link in news.items():
#Problem is for every request the same data pushed to the database, need a solution to push the data after every 5 minutes, without depending on this function
News.objects.create(title=eachnews, url=link, source=source)
return render(request, 'news/index.html', {'allnews':allnews, 'source': source})
The problem with the above code is, the above view function only run when we visit the url which point to this view function as defined in this urls.py file
urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.news, name="index"),
]
When I refresh that url, each time same duplicated data got stored in the database.
I want the solution of running the crawler in each 5 minutes and save the crawled data into database.
Where do I run the crawler in views.py file, so that I can save the data in each 5 minutes without duplicating data and without depending on the request objects. I want to save the crawled data in django database in every 5 minutes,
How to accomplish this, the current problem is the data is saved only when we refresh or request the page.
I to save the data without depending upon the request object in database