#!/usr/bin/python3

# ## {R2D2919B742E} ##
# ###########################################################################
# What if magic existed?
# What if a place existed where your every thought and dream come to life.
# There is only one catch: it has to be written down.
# Such a place exists, it is called programming.
#    - Scott Taylor Reph, RightThumb.com
# ###########################################################################
# ## {C3P0D40fAe8B} ##
##################################################
import os, sys, time
##################################################
import _rightThumb._construct as __
appDBA = __.clearFocus( __name__, __file__ )
__.appReg = appDBA
def focus( parentApp='', childApp='', reg=True ):
    global appDBA
    f = __.appName( appDBA, parentApp, childApp )
    if reg:
        __.appReg = f
    return f
__.registeredApps.append( focus() )
import _rightThumb._base3 as _
_.load()
##################################################
import _rightThumb._vars as _v
import _rightThumb._string as _str
##################################################

def appSwitches():
    pass

_.autoBackupData = __.setting('receipt-log')
__.releaseAcquiredData = __.setting('receipt-file')
__.myFileLocations_SKIP_VALIDATION = False
__.isRequired_Pipe = False
__.isRequired_Pipe_or_File = False
__.pre_error = False
__.switch_raw = []
# __.switch_raw = [ 'Delim' ]
# __.isRequired_or_List = ['Pipe','Files','Plus']
# __.setting( 'app-switches-raw', [ 'Delim' ] )


_.appInfo[focus()] = {
    'file': 'vps-hoth-7i0ZA-7GnvMI-youtube-dl-listen.py',
    'liveAppName': __.thisApp( __file__ ),
    'description': 'youtube-dl server runs on bespin runs with vps-srv-7i0ZA-7GnvMI-youtube-upload.sh',
    'categories': [
                        'youtube',
                        'download',
                ],
    'usage': [
                        # 'epy another',
                        # 'e nmap',
                        # '',
    ],
    'relatedapps': [
                        # 'p another -file file.txt',
                        # '',
    ],
    'prerequisite': [
                        # 'p another -file file.txt',
                        # '',
    ],
    'examples': [
                        _.hp('p vps-hoth-7i0ZA-7GnvMI-youtube-dl-listen'),
                        '',
    ],
    'columns': [
                       # { 'name': 'name', 'abbreviation': 'n' },
                       # { 'name': '{1}', 'abbreviation': '{0}', 'sort': '{2}' },
    ],
    'aliases': [
                       # 'this',
                       # 'app',
    ],
    'notes': [
                       # {},
    ],
}

_.appData[focus()] = {
        'start': __.startTime,
        'uuid': '',
        'audit': [],
        'pipe': False,
        'data': {
                    'field': {'sent': [], 'received': [] }, # { 'label': '', 'context': [],  }
                    'table': {'sent': [], 'received': [] }, 
        },
    }



def registerSwitches( argvProcessForce=False ):
    global appDBA
    if not __.appReg == appDBA and appDBA in __.appReg:

        if not __name__ == '__main__':
            _.argvProcess = argvProcessForce
        else:
            _.argvProcess = True

        _.load()
        _.appInfo[__.appReg] = _.appInfo[appDBA]
        _.appData[__.appReg] = _.appData[appDBA]
    __.constructRegistration( _.appInfo[__.appReg]['file'],__.appReg )
    appSwitches()

    _.myFileLocation_Print = False
    _.switches.trigger( 'Files', _.myFileLocations, vs=True )
    _.switches.trigger( 'Folder', _.myFolderLocations )
    _.switches.trigger( 'URL', _.urlTrigger )
    _.switches.trigger( 'Ago', _.timeAgo )
    _.switches.trigger( 'Duration', _.timeFuture )
    
    _.defaultScriptTriggers()
    _.switches.process()


if not __name__ == '__main__':
    _.argvProcess = False
else:
    _.argvProcess = True

registerSwitches()


def fieldSet( switchName, switchField, switchValue, theFocus=False ):
    if not type( theFocus ) == bool:
        theFocus = theFocus
    _.switches.fieldSet( switchName, switchField, switchValue, theFocus )


if __name__ == '__main__':
    if not sys.stdin.isatty():
        _.setPipeData( sys.stdin.readlines(), __.appReg, clean=True )


_.postLoad( __file__ )

########################################################################################
# START

spent=[]


count=0

def relevant(path,url=0):
    global result
    global noRel
    result = []
    noRel = []
    result = relevantB(folder,url)
    if url:
        for i,path in enumerate(result):
            path = str(path).replace('/var/www/bespin.eyeformeta.com','https://eyeformeta.com')
            parts = path.split('/')
            parts.reverse()
            file = parts.pop(0)
            parts.reverse()
            path = '/'.join(parts) +'/'+ urllib.parse.quote(file)
            # path = '/'.join(parts) +'/'+ urllib.parse.quote_plus(file)
            result[i] = path
    # _.pr('relevant',result)
    for i,path in enumerate(result):
        _.pr(url,'relevantB',path)
    return result
def relevantB(fo,url=0):
    global noRel
    global result
    global folder
    for r in os.listdir(fo):
        path = fo +os.sep+ r
        path = _str.replaceDuplicate(path,os.sep)
        if os.path.isdir(path):
            relevantB(path)
        else:               
            found = False
            # result.append(path)
            # noRel.append(path)

            for x in '.mp3 .mp4'.lower().split(' '):
                if r.lower().endswith(x):
                    found = True
                    result.append(path)
            if not found:
                if not 'index.htm' in path:
                    noRel.append(path)


    return result

def clearNotRelevant(fo):
    # return None
    global noRel
    if not os.path.isdir(fo):
        return None
    relevant(fo,url=0)
    for r in os.listdir(fo):
        p = fo+os.sep+r
        if os.path.isdir(p):
            clearNotRelevant(p)

    for path in noRel:
        try:
            os.remove(path)
            _.cp(['deleted:',path],'red')
        except Exception as e:
            _.cp(['error:',path,e],'red')

def process(row):
    global folder
    global count
    os.chdir(folder)
    folder += '/'
    folder = _str.replaceDuplicate(folder,'/')
    folder = _str.replaceDuplicate(folder,'/')
    folder = folder.replace('//','/')
    count+=1
    row = row.replace('\t',' ')
    row = _str.replaceDuplicate(row,' ')
    row = _str.replaceDuplicate(row,' ')
    row = _str.cleanBE(row,' ')
    row = _str.cleanBE(row,' ')
    if len(row) > 36:
        uuid = row.split(' ')[0]
        if len(uuid) > 30:
            url = row.split(' ')[1]
            if os.path.isdir(folder+uuid):
                _.saveText('\n'.join( relevant( folder+uuid ) ), folder+uuid+os.sep+'index.htm')
                pass
            if not os.path.isdir(folder+uuid):
                try:

                    if url in complete:
                        u = complete[url]
                        if os.path.isdir(folder+u):
                            _v.mkdir(folder+uuid)
                            do('mkdir -p '+folder+uuid)
                            if not len(relevant(folder+uuid)):
                                do('cd '+folder+uuid)
                                os.chdir(folder+uuid)
                                os.chdir(folder+uuid)
                                if v.format == 'mp3':
                                    do('/usr/local/bin/youtube-dl -x -k --audio-format mp3 '+url)
                                else:
                                    do('/usr/local/bin/youtube-dl --recode-video mp4 -k -f bestvideo+bestaudio/best '+url)
                                _.saveText('\n'.join( relevant( folder+uuid ) ), folder+uuid+os.sep+'index.htm')
                            os.link( folder+u+os.sep+'index.htm', folder+uuid+os.sep+'index.htm' )
                            _.saveText('\n'.join( relevant( folder+uuid ) ), folder+uuid+os.sep+'index.htm')
                    else:
                        complete[url]=uuid
                        _v.mkdir(folder+uuid)
                        do('mkdir -p '+folder+uuid)
                        do('cd '+folder+uuid)
                        os.chdir(folder+uuid)
                        os.chdir(folder+uuid)
                        if v.format == 'mp3':
                            do('/usr/local/bin/youtube-dl -x -k --audio-format mp3 '+url)
                        else:
                            do('/usr/local/bin/youtube-dl --recode-video mp4 -k -f bestvideo+bestaudio/best '+url)
                        _.saveText('\n'.join( relevant( folder+uuid ) ), folder+uuid+os.sep+'index.htm')
                except Exception as e:
                    _.cp(e,'red')
            _.pr('p',count)

def do(a):
    _.pr('do:\t',a)
    os.system(a)

def getFolder(folder):
    global files
    _.pr(folder)
    if os.path.isdir(folder):
        for file in os.listdir(folder):
            path = folder +os.sep+ file
            if os.path.isdir(path):
                getFolder(path)
            elif os.path.isfile(path):
                files.append(path)

def getFiles(folder):
    global files
    files = []
    getFolder(folder)
    return files


v = _.dot()
def action():
    # should be   Single-Task   OR   Imply-Architecture-Functions   OR   CLASSES!!
    global spent
    global video
    global mp3
    global folder
    global files
    global complete

    while True:
        complete={}
        load()
        v.format = 'mp3'
        do('chmod 777 -R '+folder)
        clearNotRelevant(folder)
        do('sh /opt/rightthumb-widgets-v0/widgets/bash/vps-srv/vps-srv-7i0ZA-7GnvMI-youtube-upload.sh')
        getFiles(folder)
        _.saveText('\n'.join( relevant( folder,url=1 ) ), folder+'index.htm')
        for i,row in enumerate( mp3 ):
            if not row in spent:
                spent.append(row)
                process(row)
        v.format = 'video'
        _.saveText('\n'.join( relevant( folder,url=1 ) ), folder+'index.htm')
        complete={}
        for i,row in enumerate( video ):
            if not row in spent:
                spent.append(row)
                process(row)
        _.saveText('\n'.join( relevant( folder,url=1 ) ), folder+'index.htm')
        time.sleep(30)



def load():
    global mp3
    global video
    global folder
    folder = '/var/www/bespin.eyeformeta.com/apps/youtube/dl/'
    mp3 = _.getText('/var/www/bespin.eyeformeta.com/apps/youtube/todo-mp3.txt',raw=True).replace('\r','').split('\n')
    video = _.getText('/var/www/bespin.eyeformeta.com/apps/youtube/todo-movie.txt',raw=True).replace('\r','').split('\n')


import urllib.parse


########################################################################################
if __name__ == '__main__':
    action()
    __.isExit()





