from django.core.management.base import BaseCommand, CommandError
from catalog.models import News, Publisher, Topic, audioTime
from django.db.models import Q
import os
import json
import datetime
from django.utils import timezone
import dateutil.parser
import itertools
from datetime import date

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


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

	def handle(self, *args, **options):

		if True:
			#update radio stations recotding time
			print("Recording Time:")
			recordingsDir = "/home/mario/virtualHDD/m3/recordings/"
			audioTime.objects.all().delete()
			publishers  = Publisher.objects.all().filter(type="audio")
			for p in publishers:
				files =  [files for r, d, files in os.walk(recordingsDir+p.shortName)]
				files =  list(itertools.chain.from_iterable(files))
				minutes = len(files)
				sortedFiles = sorted([(f[:f.find(".flac")]) for f in files if f.find(".flac")>0 and f.count(".")==1])
				print(p.shortName+": "+ str(minutes))
				if len(sortedFiles)>2:
					print("timestamp: " + sortedFiles[0]) 
					print("len timestamp: " + str(len(sortedFiles[0]))) 

					since = datetime.datetime.utcfromtimestamp(int(sortedFiles[0]))
					audioTime.objects.update_or_create(publisher=p, defaults={'minutes': minutes, "startDate": since},)
		#load news
		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")
			minYear = 0
			lastDate = datetime.datetime(1950,1,1)

			if news.count()>0:
				minYear = news[0].date.year
				lastDate = news[0].date
			yearList = [ int(y) for y in os.listdir('.')]

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

						if fileDate >= lastDate.date():
							with open(f) as data_file:
								try:
									print(f)
									data = json.load(data_file)
									for d in data:
										newsDate = dateutil.parser.parse(d['date'])
										if News.objects.all().filter(Q(publisher=publisher.id)&Q(title=d['title'])&Q(date__gte=newsDate)).count() == 0:

											if d['title'] == None:
												d['title'] = "Sin Titulo"
											if len(d['title']) >= 512:
												d['title'] = d['title'][:500]
											print  (p + " "+  str(newsDate) +  ": " + d['title'])
											news = News()
											news.publisher = publisher
											news.title = d['title']
											news.text = d['text']
											news.url = d['url']
											news.date = newsDate
											news.save()

											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)

								except ValueError as e:
									print (e)
									print ("Error: " + os.getcwd()+"/"+f)
									print (type(d['topic']))
									
								data_file.close()						

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


