crawlerApp/dockerhubCrawler/views.py

65 lines
No EOL
3.2 KiB
Python

from django.shortcuts import render
import requests
from .models import DockerhubCrawler
# from django.db import models (Tried doesn't work:Sami)
import datetime
from datetime import timedelta
# Create your views here.
def HomePage(request):
return render(request, 'home.html', status=200)
def DockerPage(request):
#datatimeobjlast_pushedtemp = models.DateTimeField()
#datetimeobjtag_last_pushed = models.DateTimeField()
datatimeobjlast_pushedtemp = datetime.datetime.now()
datetimeobjtag_last_pushed = datetime.datetime.now()
count = 0
for dhubobj in DockerhubCrawler.objects.all():
resp = requests.get(url=dhubobj.api_url)
data = resp.json()
string_obj = ''
for chunk in data['results']: # looping through the data if updated in greater than prev date time save it
#print(chunk)
if count == 0: # first iteration lets just assign both object values
splits = ((chunk['tag_last_pushed'].split(":")))
#print(splits)
datatimeobjlast_pushedtemp = datetime.datetime(int(splits[0][:4]), int(splits[0][5:7])
,int(splits[0][8:10]), int(splits[0][11:]), int(splits[1][0:3]),
int(splits[2][0:2]), int(splits[2][3:len(splits[2])-1]))
datetimeobjtag_last_pushed = datetime.datetime(int(splits[0][:4]), int(splits[0][5:7])
,int(splits[0][8:10]), int(splits[0][11:]), int(splits[1][0:3]),
int(splits[2][0:2]), int(splits[2][3:len(splits[2])-1]))
string_obj = chunk['tag_last_pushed']
else:
splits = ((chunk['tag_last_pushed'].split(":")))
#print(splits)
#print(splits[0][:4], splits[0][5:7], splits[0][8:10],splits[0][11:] ,splits[1][0:3] , splits[2][0:2], splits[2][3:len(splits[2])-1])
datatimeobjlast_pushedtemp = splits = ((chunk['tag_last_pushed'].split(":")))
datatimeobjlast_pushedtemp = datetime.datetime(int(splits[0][:4]), int(splits[0][5:7])
,int(splits[0][8:10]), int(splits[0][11:]), int(splits[1][0:3]),
int(splits[2][0:2]), int(splits[2][3:len(splits[2])-1]))
if datatimeobjlast_pushedtemp > datetimeobjtag_last_pushed:
datetimeobjtag_last_pushed = datatimeobjlast_pushedtemp
string_obj = chunk['tag_last_pushed']
count = count + 1
# end for here, string_obj should have latest pushed date by now
splits = (string_obj.split(":"))
dhubobj.last_pushed = datetimeobjtag_last_pushed
# here we will notify and send the email
dhubobj.save()
allobjs = DockerhubCrawler.objects.all()
return render(request, 'docker.html', context={'dockerhubobjs': allobjs})