from django.core.management.base import BaseCommand, CommandError
from catalog.models import News, Publisher, Topic
from django.db.models import Q
import os
import json
import datetime
from django.utils import timezone
import dateutil.parser
class Command(BaseCommand):
    help = 'Update database'


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

    def handle(self, *args, **options):
	os.chdir(options['basedir'][0])
        publisherList = os.listdir(options['basedir'][0])
        for p in publisherList:
		print p
		os.chdir(p)
		publisher  = Publisher.objects.all().filter(shortName=p)[0]
		news = News.objects.all().filter(publisher=publisher.id).order_by("-date")
#		print publisher.id
		minYear = 0
		lastDate = datetime.datetime(1950,1,1)
#		print news.count()
		if news.count()>0:
			minYear = news[0].date.year
			lastDate = news[0].date
		yearList = [ int(y) for y in os.listdir('.')]

#		print "LastDate"+str(lastDate)
		print yearList
		for y in yearList:
			if y >=minYear:
				os.chdir(str(y))
				print os.getcwd()
				filesList = os.listdir(".")
				for f in filesList:
#					print "file: " + f
					fileDate = datetime.datetime.strptime(f[:f.find(".")], "%Y-%m-%d").date()

					if fileDate >= lastDate.date():
#						print "fileDate: " + str(fileDate)
						with open(f) as data_file:
							try:
							        data = json.load(data_file)
								for d in data:
#									if d['date'].find("+") >=0:
#										date =d['date'][:d['date'].find("+")].replace("T", " ")	
#										newsDate = datetime.datetime.strptime(date, "%Y-%m-%d %H:%M:%S")
#									else:
#										date = d['date']
#										newsDate = datetime.datetime.strptime(date, "%Y-%m-%d")
#									print d['date']
#									print d['title']
									newsDate = dateutil.parser.parse(d['date'])


#									newsDate = timezone.make_aware(newsDate, timezone.get_current_timezone())

#									print "newsDate:"
#									print  newsDate
									if News.objects.all().filter(Q(publisher=publisher.id)&Q(title=d['title'])&Q(date__gte=newsDate)).count() == 0:
#										print  "no news  match"
										if d['title'] == None:
											d['title'] = "Sin Titulo"
										if len(d['title']) >= 512:
											d['title'] = d['title'][:500]
										print  p + " "+  str(newsDate) +  ": " + d['title']
#										print type(d['topic'])
										news = News()
										news.publisher = publisher
#										print "title"
										news.title = d['title']
#										print "text"
										news.text = d['text']
#										print "url"
										news.url = d['url']
#										print "date"
										news.date = newsDate
										news.save()

#										print "TOPIC: " + str(d['topic'])
#										print type(d['topic'])
										if d['topic'] == "" or d['topic'] == None or d['topic'] ==[]:
											d['topic'] = "Sin Tema"

										if type(d['topic'])=="list":
											for t in d['topic']:
												topic, created = Topic.objects.all().get_or_create(name=t)
												news.topic.add(topic)
										else:
											topic, created = Topic.objects.all().get_or_create(name=d['topic'])
											news.topic.add(topic)
#										print news.date
#										print d['date']
#										#news.save()
#							except (RuntimeError, TypeError, NameError, ValueError) as e:
							except ValueError as e:

								print e 
#								print e.NameError
								print "Error: " + os.getcwd()+"/"+f
								print type(d['topic'])
#								print "TOPIC: " + str(d['topic'])
								
							data_file.close()

						

				os.chdir("..")
		os.chdir("..")
