File size: 2,801 Bytes
447dec7
0277ad1
 
 
 
5663f67
c9e6491
 
 
 
 
 
 
595fcca
0277ad1
 
 
 
 
 
 
447dec7
0277ad1
 
 
 
447dec7
0277ad1
 
 
 
447dec7
0277ad1
 
 
 
447dec7
0277ad1
 
d0f7604
595fcca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc34b75
d0f7604
5663f67
 
447dec7
0277ad1
1f2522a
c9e6491
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from django.shortcuts import render, redirect
from .utils.HebScraper import HebScraper
from .utils.WallmartScraper import WallmartScraper
from .utils.SephoraScraper import SephoraScraper
from .utils.UltaScraper import UltaScraper
from .utils.TargetScraper import TargetScraper
from django_filters.views import FilterView
from django_tables2.views import SingleTableMixin
from .filters import ProductFilter
from .tables import ProductTable
from .models import Product
import csv
from django.http import HttpResponse
import threading
# Create your views here.



def refreshHeb(request):
    hebScraper = HebScraper()
    hebScraper.run()
    return redirect('home')

def refreshWallmart(request):
    wallmartScraper = WallmartScraper()
    wallmartScraper.run()
    return redirect('home')

def refreshSephora(request):
    sephoraScraper = SephoraScraper()
    sephoraScraper.run()
    return redirect('home')

def refreshUlta(request):
    ultaScraper = UltaScraper()
    ultaScraper.run()
    return redirect('home')

def refreshAll(request):
    # create a background task to run all the scrapers in parallel
    hebScraper = HebScraper()   
    wallmartScraper = WallmartScraper()
    sephoraScraper = SephoraScraper()
    targetScraper = TargetScraper()
    ultaScraper = UltaScraper()
    hebThread = threading.Thread(target=hebScraper.run)
    wallmartThread = threading.Thread(target=wallmartScraper.run)
    sephoraThread = threading.Thread(target=sephoraScraper.run)
    targetThread = threading.Thread(target=targetScraper.run)
    ultaThread = threading.Thread(target=ultaScraper.run)
    hebThread.start()
    wallmartThread.start()
    sephoraThread.start()
    targetThread.start()
    ultaThread.start()
    # redirect to home page and pass a message
    return redirect('home')

    
def refreshTarget(request):
    TargetScraper().run()
    return redirect('home')

def home(request):
    return render(request, 'scraper/home.html')

class ProductListView(SingleTableMixin, FilterView):
    table_class = ProductTable
    model = Product
    template_name = "scraper/home.html"
    filterset_fields = {
        'title': ['icontains'],
        'ingredients': ['icontains'],
        'product_id': ['exact'],
        'url': ['exact'],
        'store_name': ['exact'],
    }
    filtered_class = ProductFilter


def download_all_product_csv(request):
    parcels = Product.objects.all()
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="parcels.csv"'
    writer = csv.writer(response)
    writer.writerow(['Product Id', 'Title', 'Ingredients', 'Url', 'Store Name'])
    for parcel in parcels:
        writer.writerow([parcel.product_id, parcel.title, parcel.ingredients, parcel.url, parcel.store_name])
    return response