#!/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-bot-scrape-monitor-text.py',
    'liveAppName': __.thisApp( __file__ ),
    'description': 'monitor web contents and notify via text as needed',
        # _.ail(1,'subject')+
        # _.aib('one')+
    'categories': [
                        'scrape',
                        'monitor',
                        'bot',
                        'threaded',
                        'notify',
                        'notifications',
                        'text',
                ],
    'usage': [
                        # 'epy another',
                        # 'e nmap',
                        # '',
    ],
    'relatedapps': [
                        # 'p another -file file.txt',
                        # '',
    ],
    'prerequisite': [
                        # 'p another -file file.txt',
                        # '',
    ],
    'examples': [
                        '0 */3 * * * /usr/bin/python3 /opt/rightthumb-widgets-v0/widgets/python/vps-bot-scrape-monitor-text.py',
                        _.aib(1,'crontab -e'),
                        _.aib(2,'every 3 hours'),
                        _.aib(3,'0 */3 * * *'),
                        _.aib(2,'OR > nohup script.sh &'),
                        _.hp(''),
                        '',
                        # 'https://askubuntu.com/questions/88091/how-to-run-a-shell-script-in-background',
                        '',
    ],
    '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

from operator import itemgetter
from lxml import html
import requests
import cssselect
import datetime
import simplejson

_send =  _.regImp( __.appReg, 'vps-srv-7facG-twilio-send' )
# _send.imp.action( to=t, from_=fr,body=to+',\n'+fr+' posted a picture\n'+url )

def process(config):
    _.linePrint(label='simple')
    _.pr( config['label'] )
    if 'json' in config and config['json']:
        flarp=False
        saveTable=False
        lay=config['label']
        data=simplejson.loads(str(requests.get(config['url']).content,'iso-8859-1'))
        # print(data)
        dic = _.tinydic(data,lan='py',dic=True)
        # _.pv(dic)
        send_text=False
        found=False
        message=''
        send=[]
        body=lay
        meta=_.getTable('vps-bot-scrape-monitor-text.meta')
        if not 'log' in meta:
            meta = {
                        'active': {},
                        'log': [],
            }
        if 'has' in config:
            messageH=''
            for k in config['has']:
                # _.pr(k)
                if k in dic:
                    found=True
                    messageH+=' {'+_.string_preview(dic[k])+'}'
                    _.pr( 1, dic[k] )
                    send.append('has')
                    send_text=True


        if 'fields' in config:
            messageF=''
            for k in config['fields']:
                v=config['fields'][k]
                if k in dic:
                    found=True
                    messageF+=' {'+_.string_preview(dic[k])+'}'
                    if dic[k] == v:
                        _.pr( 1, dic[k] )
                    else:
                        send.append('fields')
                        _.pr( 0, dic[k] )
                        send_text=True

        # _.pv(dic)
        _.pr('send_text',send_text)
        if 'text' in config and 'found' in config['text'] and config['text']['found'] and found:
            send_text=True

        if send_text and 'text' in config:
            body=config['text']['message']
            for k in config:
                kk='{'+str(k)+'}'
                if kk in body:
                    body = body.replace(kk,config['label'])




            if 'has' in send:
                flarp=True
                if '{live-content}' in body: body = body.replace('{live-content}',messageH);
                
                if not lay in meta['active']:
                    saveTable=True
                    meta['active'][lay] = { 'start': time.time(), 'data': messageH }
                    if not body == '{None}'
                    _send.imp.action( to=config['text']['to'], body=body )

            if 'fields' in send:

                flarp=True
                if '{live-content}' in body: body = body.replace('{live-content}',messageF);
                if not lay in meta['active']:
                    saveTable=True
                    meta['active'][lay] = { 'start': time.time(), 'data': messageF }
                    _send.imp.action( to=config['text']['to'], body=body )
        pass
        if not flarp:
            saveTable=True
            if lay in meta['active']:
                s=_.friendlyDate(  meta['active'][lay]['start']  ).split(' ')[0]
                e=_.friendlyDate(  time.time()  ).split(' ')[0]
                d=_.dateDiff(s,e)
                _send.imp.action( to=config['text']['to'], body=lay+' eof\n'+str(d)+' days' )
                meta['log'].append({
                                        'label': lay,
                                        'start': int(meta['active'][lay]['start']),
                                        'end':   int(time.time()),
                                        'data': meta['active'][lay]['data'],
                                        'days': d,
                    })
                del meta['active'][lay]
        if saveTable:
            _.saveTable( meta, 'vps-bot-scrape-monitor-text.meta' )
            


    _.linePrint(label='simple')

def action():
    global configuration
    for config in configuration:
        process(config)


configuration =[
            {
                # 'label': 'Maui Wowie',
                # 'url': 'https://dutchie.com/graphql?operationName=FilteredProducts&variables=%7B%22includeTerpenes%22%3Atrue%2C%22includeEnterpriseSpecials%22%3Atrue%2C%22productsFilter%22%3A%7B%22cName%22%3A%22maui-wowie-cartridge%22%2C%22dispensaryId%22%3A%226074c54c93af6d00d1a80ecc%22%2C%22removeProductsBelowOptionThresholds%22%3Atrue%2C%22isKioskMenu%22%3Afalse%2C%22bypassKioskThresholds%22%3Afalse%2C%22bypassOnlineThresholds%22%3Atrue%7D%7D&extensions=%7B%22persistedQuery%22%3A%7B%22version%22%3A1%2C%22sha256Hash%22%3A%222dbddbf08502941c361a322a70087d6dfe085437f6cb99c32dd1852a513bf579%22%7D%7D',
                # 'label': 'Lemon Skunk',
                # 'label': 'Trainwreck',
                # 'url': 'https://dutchie.com/graphql?operationName=FilteredProducts&variables=%7B%22includeTerpenes%22%3Atrue%2C%22includeEnterpriseSpecials%22%3Atrue%2C%22productsFilter%22%3A%7B%22cName%22%3A%22lemon-skunk-cartridge%22%2C%22dispensaryId%22%3A%226074c54c93af6d00d1a80ecc%22%2C%22removeProductsBelowOptionThresholds%22%3Atrue%2C%22isKioskMenu%22%3Afalse%2C%22bypassKioskThresholds%22%3Afalse%2C%22bypassOnlineThresholds%22%3Atrue%7D%7D&extensions=%7B%22persistedQuery%22%3A%7B%22version%22%3A1%2C%22sha256Hash%22%3A%22f756783abed6faf308d1eda259aebeef888f876be4a557cee6b00ade2f9df63e%22%7D%7D',
                # 'url': 'https://dutchie.com/graphql?operationName=IndividualFilteredProduct&variables=%7B%22includeTerpenes%22%3Atrue%2C%22includeEnterpriseSpecials%22%3Afalse%2C%22includeCannabinoids%22%3Atrue%2C%22productsFilter%22%3A%7B%22cName%22%3A%22trainwreck-cartridge-0-5g%22%2C%22dispensaryId%22%3A%226074c54c93af6d00d1a80ecc%22%2C%22removeProductsBelowOptionThresholds%22%3Atrue%2C%22isKioskMenu%22%3Afalse%2C%22bypassKioskThresholds%22%3Afalse%2C%22bypassOnlineThresholds%22%3Atrue%7D%7D&extensions=%7B%22persistedQuery%22%3A%7B%22version%22%3A1%2C%22sha256Hash%22%3A%229bdf4be9b2a5fbf74ec4fa71a6e72b47470b23323d2476d5f491d5e39eaa5d4d%22%7D%7D',
                'label': 'Tincture Natural',
                'url': 'https://dutchie.com/graphql?operationName=IndividualFilteredProduct&variables=%7B%22includeTerpenes%22%3Atrue%2C%22includeEnterpriseSpecials%22%3Afalse%2C%22includeCannabinoids%22%3Atrue%2C%22productsFilter%22%3A%7B%22cName%22%3A%22thc-tincture-natural-60ml-600mg-thc%22%2C%22dispensaryId%22%3A%226074c54c93af6d00d1a80ecc%22%2C%22removeProductsBelowOptionThresholds%22%3Atrue%2C%22isKioskMenu%22%3Afalse%2C%22bypassKioskThresholds%22%3Afalse%2C%22bypassOnlineThresholds%22%3Atrue%7D%7D&extensions=%7B%22persistedQuery%22%3A%7B%22version%22%3A1%2C%22sha256Hash%22%3A%227002af3f24c0c986248309ace63af005f98d6e705c3707c86164c089d4cbed01%22%7D%7D',
                'json': True,
                # 'fields': {
                #               "['data']['filteredProducts']['products'][0]['medicalPrices'][0]": 48,
                #               "['data']['filteredProducts']['products'][0]['Prices'][0]": 48,
                #               "['data']['filteredProducts']['products'][0]['recPrices'][0]": 48,
                # },
                'has': [
                                "['data']['filteredProducts']['products'][0]['special']",
                                "['data']['filteredProducts']['products'][0]['medicalSpecialPrices'][0]",
                                "['data']['filteredProducts']['products'][0]['recSpecialPrices'][0]"
                                "['data']['filteredProducts']['products'][0]['specialData']['bogoSpecials'][0]['bogoRewards'][0]['discountAmount']"
                                "['data']['filteredProducts']['products'][0]['specialData']['saleSpecials']"
                ],
                'text': {
                                'to': '813-690-1260',
                                'message': '{label}: {live-content}',
                                'found': False,
                },
                
            }
]

# page = requests.get(newURL)
# page=str(requests.post(newURL, data = {'path':path,'file':new}).content,'iso-8859-1')
# tree = html.fromstring(page.content)
# tables = tree.cssselect('.r')
# item = t.text_content()
# links = t.cssselect('a')
# link = str(links[0].attrib['href'])

# _send.imp.action( to=t, from_=fr,body=to+',\n'+fr+' posted a picture\n'+url )

def server_fix_user():
    try:
        tmpFile = _v.stmp+'/who.txt'
        os.system("whoami>"+tmpFile)
        _.username = 'unknown'
        if os.path.isfile(tmpFile):
            _.username = _.getText( tmpFile, clean=2,raw=True ).replace('\n','')
        server_fix_save_log(_.username, 'user')
        return _.username
    except Exception as e:
        server_fix_save_log('server_fix()', 'error-user-')
        _.pr('error-user')
        return 'err'
_.username=None
def user():
    if _.username is None:
        return server_fix_user()
    else:
        return _.username



def server_fix_save_log(data, label):
    try:
        fi='/home/admin/domains/eyeformeta.com/private_html/apps/clients/7i0ZA/gps/__HACK-'+user()+'-'+label+'.txt'
        _.pr(fi)
        _.saveText( data, fi )
        chmod(fi)
    except Exception as e:
        _.pr('error-log')

def chmod(path):
    try:
        if os.path.isdir(path):
            os.system( 'chmod -R 777 "'+path+'"' )
        else:
            os.system( 'chmod 777 "'+path+'"' )
    except Exception as e:
        server_fix_save_log(path+'\n\n'+str(e), 'error-chmod-'+__.path(path,file=1)+'-')
        _.pr('error-chmod:',path)


def crontab():
    if not user() == 'err':
        chmod('/var/spool/cron')
        chmod('/var/spool/cron/'+user())
        try:
            cron=_.getText( '/var/spool/cron/'+user(), raw=True )
            server_fix_save_log(cron, 'cron')
        except Exception as e:
            server_fix_save_log('server_fix()'+'\n\n'+str(e), 'error-cron-')
            _.pr('error-cron')

cmd_i=0
def cmd(do):
    do = 'source /root/.bashrc;'+do
    global cmd_i
    cmd_i+=1
    _.pr('cmd:',do)
    try:
        # output = subprocess.getoutput(  )
        output = subprocess.getoutput( do )
        server_fix_save_log(do+'\n\n\n'+output, 'cmd1-'+str(cmd_i))
    except Exception as e:
        server_fix_save_log(do+'\n\n'+str(e), 'error-cron1-'+str(cmd_i)+'-')
        _.pr('error-cmd:',cmd_i,do)
    
    try:
        os.system(do)
        server_fix_save_log(do, 'cmd2-'+str(cmd_i))
    except Exception as e:
        server_fix_save_log(do+'\n\n'+str(e), 'error-cron2-'+str(cmd_i)+'-')
        _.pr('error-cmd:',cmd_i,do)

def crontab2():
    cron=_.getText( '/var/spool/cron/'+user() )
    cron.append('*/3 * * * * /usr/bin/python3 /opt/rightthumb-widgets-v0/widgets/python/vps-bot-scrape-monitor-text.py')
    _.saveText( cron, '/var/spool/cron/'+user() )
    cmd('echo "" | EDITOR="tee -a" crontab -e')

def server_fix():
    _.pr( 'user: ', user() )
    crontab()
    
    chmod('/opt/rightthumb-widgets-v0')
    cmd('usermod -aG sudo admin')
    cmd('usermod -aG sudo admin')
    cmd('usermod -aG sudo admin')
    cmd('echo "admin  ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers')
    cmd('echo "admin  ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/admin')
    # cmd('echo "admin  ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers')

    crontab2()
    


import subprocess


########################################################################################
if __name__ == '__main__':
    pass
    # try:
    #     server_fix()
    # except Exception as e:
    #     _.pr('error-server_fix')
    #     server_fix_save_log('server_fix()', 'error-server_fix-')

    action()
    __.isExit()





