from django.conf import settings
from urllib.parse import urljoin, urlencode
import re
import requests
import requests_cache


requests_cache.install_cache('lomadee_cache')


class ComputerDataImporter(object):

    def __init__(self):
        pass

    def build_api_url(self, **kwargs):
        api_url = urljoin(settings.LOMADEE_API_URL, settings.LOMADEE_APP_TOKEN)

        # Specific path to 'Computer' category
        url = urljoin('{}/'.format(api_url), 'offer/_category/6424')
        kwargs['sourceId'] = settings.LOMADEE_SOURCE_ID
        kwargs['size'] = 100

        return '{}?{}'.format(url, urlencode(kwargs))

    def get_data(self, url=None):
        if not url:
            url = self.build_api_url()

        data = requests.get(url).json()
        if data['requestInfo']['status'] != 'OK':
            return None

        final_data = []
        final_data.extend(data['offers'])
        pagination = data['pagination']

        if pagination['page'] < pagination['totalPage']:
            next_page_data = self.get_data(
                self.build_api_url(page=pagination['page'] + 1)
            )
            final_data.extend(next_page_data)
        return final_data

    def get_valid_data(self):
        all_data = self.get_data()
        valid_data = []
        for data in all_data:
            cpu, ram, disk, _, _, _ = self.get_specs(data['name'])
            if None not in (cpu, disk, ram):
                valid_data.append(data)

        return valid_data

    def get_specs(self, description):
        cpu = self.get_cpu(description)
        ram = self.get_ram(description)
        disk = self.get_disk(description)
        is_macbook = self.is_macbook(description)
        has_gpu = self.has_gpu(description)
        has_ssd = self.has_ssd(description)
        return (cpu, ram, disk, is_macbook, has_gpu, has_ssd)

    def get_cpu(self, description):
        groups = self._get('(i3|i5|i7)', description)
        if groups:
            return groups[0]

    def get_disk(self, description):
        groups = self._get('\s(\d{1})\s?(tb|tera)|\s(\d{3,4})\s?(hd|gb)',
                           description)
        if groups:
            tb, _, gb, _ = groups
            if tb:
                return int(tb) * 1000
            else:
                return int(gb)

    def get_ram(self, description):
        groups = self._get('\s(\d{1,2})\s?(gb|ram|ddr)', description)
        if groups:
            return int(groups[0])

    def is_macbook(self, description):
        return self._has('apple|macbook', description)

    def has_gpu(self, description):
        return self._has('geforce|gtx|dedicad[ao]|nvidea|radeon', description)

    def has_ssd(self, description):
        return self._has('ssd|solid state', description)

    def _has(self, regex, description):
        match = re.search(regex, description.lower())
        if match:
            return True
        else:
            return False

    def _get(self, regex, description):
        match = re.search(regex, description.lower())
        if match:
            return match.groups()
        else:
            return None

@@ -70,16 +70,77 @@
 lencode%0A
+from lomadee import models%0Afrom django.db import transaction%0A
 import r
@@ -1477,16 +1477,31 @@
 isk, ram
+, data%5B'price'%5D
 ):%0A     
@@ -1559,24 +1559,1129 @@
 valid_data%0A%0A
+    def save_data(self):%0A        data = self.get_valid_data()%0A        empty_database = not models.Computer.objects.all().exists()%0A        for computer_data in data:%0A            self.create_object(computer_data, empty_database)%0A%0A    @transaction.atomic%0A    def create_object(self, data, empty_database=True):%0A        if empty_database:%0A            computer = models.Computer()%0A            computer.id = data%5B'id'%5D%0A        else:%0A            computer = models.Computer.objects.get_or_create(id=data%5B'id'%5D)%5B0%5D%0A%0A        specs = self.get_specs(data%5B'name'%5D)%0A        computer.name = data%5B'name'%5D%0A        computer.price = data%5B'price'%5D%0A        computer.thumbnail = data%5B'thumbnail'%5D%0A        computer.link = data%5B'link'%5D%0A        computer.cpu = specs%5B0%5D%0A        computer.ram = specs%5B1%5D%0A        computer.disk = specs%5B2%5D%0A        computer.is_macbook = specs%5B3%5D%0A        computer.has_gpu = specs%5B4%5D%0A        computer.has_ssd = specs%5B5%5D%0A%0A        product = data.get('product', None)%0A        if product:%0A            computer.rating = data%5B'product'%5D%5B'userRating'%5D%5B'rating'%5D%0A%0A        computer.save()%0A        return computer%0A%0A
     def get_
@@ -3833,18 +3833,52 @@
 a%7Cradeon
-',
+%7Cgraphics',%0A                        
  descrip

Add create_object to data_importer