from django.core.management.base import BaseCommand, CommandError
#from polls.models import Question as Poll
import  json
from pprint import pprint
from catalog.models import Publisher


class Command(BaseCommand):
    help = 'Update database'

    def add_arguments(self, parser):
        parser.add_argument('filename', nargs=1, type=str)

    def handle(self, *args, **options):
	Publisher.objects.all().delete()
	print options['filename']

	with open(options['filename'][0]) as data_file:    
		data = json.load(data_file)
		for i in data:
			print i['nombre']
			publisher = Publisher()
			publisher.name = i['nombre']
			crawler = i['crawler']
			publisher.shortName = crawler[crawler.find("/")+1:]
			publisher.crawler = crawler
			publisher.url = i['url']
			publisher.type = "texto"
			publisher.save()


		
