import sys
import argparse
import os
import subprocess
from numpy import array_equal
from datetime import datetime, timedelta

BASEDIR = "/home/mario/"
#-------------------------------------------------------------------------------
def getRecordingDays(initTimestamp, endTimestamp):

	initialDate = datetime.fromtimestamp(initTimestamp) #+ timedelta(hours=6)
	endDate = datetime.fromtimestamp(endTimestamp) #+ timedelta(hours=6)

	print(initialDate)
	print(endDate)

	iDate = [initialDate.day, initialDate.month, initialDate.year]
	eDate = [endDate.day, endDate.month, endDate.year]

	days = 1
	if array_equal(iDate, eDate):
		# Iterate over the same day dir
		return days
	else:
		while not array_equal(iDate, eDate):
			days += 1
			initialDate += timedelta(days=1)
			iDate = [initialDate.day, initialDate.month, initialDate.year]
		return days
#-------------------------------------------------------------------------------
def getAudiosList2Convert(streamPublisher, initTimestamp, endTimestamp, days):

	recordingsPath = BASEDIR+'virtualHDD/m3/recordings/'
	initialDate = datetime.fromtimestamp(initTimestamp) #+ timedelta(hours=6)
	endDate = datetime.fromtimestamp(endTimestamp) #+ timedelta(hours=6)

	audiosList = []

	for i in range(days):

		audiosPath = '{0}/{1}/{2:02d}/{3:02d}/'.format(
			streamPublisher, 
			initialDate.year,
			initialDate.month,
			initialDate.day
		)
		print('Now enter in: {0}'.format(audiosPath))

		print(recordingsPath+audiosPath)
		if os.path.exists(recordingsPath+audiosPath):
			print ("PATH EXIST")
			for file in os.listdir(recordingsPath+audiosPath):
#				print (file)
				audioTimestamp = int(file.split(".")[0])
				if audioTimestamp >= initTimestamp and audioTimestamp <= endTimestamp:
					audiosList.append(recordingsPath+audiosPath+file)
					#audiosList.append((recordingsPath+audiosPath, file))

		# Next day dir
		initialDate += timedelta(days=1)

	return audiosList
#-------------------------------------------------------------------------------
def convertAudios2Mp3(audiosList):

	mp3Path = BASEDIR+'virtualHDD/m3/recordings/mp3/'
	if not os.path.exists(mp3Path):
		print('Mp3 created...')
		os.mkdir(mp3Path)

	for (audioPath, audioName) in audiosList:
		timestamp = audioName.split('.')[0]
		print('Timestamp: {0} - Date format: {1}'.format(timestamp, datetime.fromtimestamp(float(timestamp))))
		mp3Name = timestamp + '.mp3'
		convertionCommand = 'ffmpeg -i "{0}" -qscale:a 20 {1}'.format(audioPath+audioName, mp3Path+mp3Name)
		subprocess.run(convertionCommand, shell=True)

#-------------------------------------------------------------------------------
def getAudioList(streamPublisher, initTimestamp, endTimestamp):
	print("getAudioList(", streamPublisher, ",", initTimestamp, ",", endTimestamp, ")")
	initTimestamp = float(initTimestamp)
	endTimestamp = float(endTimestamp)
	days = getRecordingDays(initTimestamp, endTimestamp)
	print('Recording days: {0}'.format(days))
	audiosList = getAudiosList2Convert(streamPublisher, initTimestamp, endTimestamp, days)
	return audiosList

