Esempio n. 1
0
def watcher(event, send_sms, log):
    print "Debug: Watcher started ..."
    try:
        ## Full code for main parent
        if event['type'] == 'send_sms':
            data = event['data']
            res, payloadArr = load_data(data)
            if not res:
              raise Exception

            log(data['id'], len(payloadArr) - 1)              # The last one is the sentinel
            print "Payload array size", len(payloadArr) - 1

            for payload in payloadArr:
                send_sms(payload)
        elif event['type'] == 'external_setup':
            data = event['data']
            result = work_external_data(data)
            print "DEBUG, external event, loaded = "+str(result)
            if not result:
                updateAction(data['id'], 'Data Load Failed', event.get('oid'))
        elif event['type'] == 'update_action':
            data = event['data']
            updateAction(data['id'], data['action'], data.get('oid'))

        return True
    except Exception:
        cLogger.exception("handler recovered over the following")
        return False
    finally:
        pass     # cleanup
Esempio n. 2
0
def work_external_data(event):
    try:
        oid = event['oid']
        url = 'http://45.55.72.208/wadi/query?id=' + str(oid)
        r = requests.get(url)
        print "Inside work external"
        if r.status_code == 200:
            rdata = r.json()

            filename = "res_" + str(event['id']) + ".csv"
            filename_full = './data/temp/' + filename

            if 'query' in rdata:
                query = rdata['query']
                cursor = db.cursor()
                cursor.execute(query)
                print "Got query" + query
                save_to_file(cursor, filename_full, ['Phone', 'Language'])
            elif 'pipeline' in rdata and 'options' in rdata:
                res, headers = execute_pipeline(rdata['pipeline'],
                                                rdata['options'])
                update_job_status(oid, customer_count=len(
                    res))  # Notify the API about the total number of customers
                save_to_file(res, filename_full, headers)
            else:
                raise Exception

            print "Updating action"
            upload_file(filename, filename_full)
            if event.get('repeat', 'Once') == 'No Send':
                action = 'Done'
            else:
                action = 'Data Loaded'
            updateAction(event['id'], action, oid=oid)
            print "Updating link"
            updateLink(event['id'],
                       'http://jlabs.co/downloadcsv.php?file=' + filename,
                       oid=oid)
            return True
        else:
            updateAction(event['id'], 'Bad Link', oid=oid)
            return False
    except Exception:
        cLogger.exception("crashed with event %s", str(event))
        return False
def work_external_data(event):
    try:
        oid = event['oid']
        url = 'http://45.55.72.208/wadi/query?id='+str(oid)
        r = requests.get(url)
        print "Inside work external"
        if r.status_code == 200:
            rdata = r.json()

            filename = "res_"+str(event['id'])+".csv"
            filename_full = './data/temp/'+filename

            if 'query' in rdata:
                query = rdata['query']
                cursor = db.cursor()
                cursor.execute(query)
                print "Got query"+query
                save_to_file(cursor, filename_full, ['Phone', 'Language'])
            elif 'pipeline' in rdata and 'options' in rdata:
                res, headers = execute_pipeline(rdata['pipeline'], rdata['options'])
                update_job_status(oid, customer_count=len(res))     # Notify the API about the total number of customers
                save_to_file(res, filename_full, headers)
            else:
                raise Exception

            print "Updating action"
            upload_file(filename, filename_full)
            if event.get('repeat', 'Once') == 'No Send':
                action = 'Done'
            else:
                action = 'Data Loaded'
            updateAction(event['id'], action, oid=oid)
            print "Updating link"
            updateLink(event['id'], 'http://jlabs.co/downloadcsv.php?file='+filename, oid=oid)
            return True
        else:
            updateAction(event['id'], 'Bad Link', oid=oid)
            return False
    except Exception:
        cLogger.exception("crashed with event %s", str(event))
        return False
Esempio n. 4
0
def work_external_data(event):
    try:
        url = event['External Link']
        r = requests.get(url)
        print "Inside work external"
        if r.status_code == 200:
            rdata = r.json()

            filename = "res_"+str(event['ID'])+".csv"
            filename_full = './data/temp/'+filename

            if 'query' in rdata:
                query = rdata['query']
                cursor = db.cursor()
                cursor.execute(query)
                print "Got query"+query
                save_to_file(cursor, filename_full, ['Phone', 'Language'])
            elif 'pipeline' in rdata and 'options' in rdata:
                res, headers = execute_pipeline(rdata['pipeline'], rdata['options'])
                save_to_file(res, filename_full, headers)
            else:
                print "Error"
                return

            print "Updating action"
            upload_file(filename, filename_full)
            updateAction(event['ID'], 'Data Loaded')
            print "Updating link"
            updateLink(event['ID'], 'http://jlabs.co/downloadcsv.php?file='+filename)
            return True
        else:
            updateAction(event['ID'], 'Bad Link')
            return False
    except Exception:
        cLogger.exception("crashed with event %s", str(event))
        return False
Esempio n. 5
0
            payload = {}
            if d[1].strip() in "Arabic":
                message_text = sms_dict['ar']
                payload = {'message': message_text,'mobilenumber':d[0].strip("=").strip().replace('+','').replace('-',''), 'mtype': "OL"}
            elif d[1].strip() in "English":
                message_text = sms_dict['en']
                payload = {'message': message_text,'mobilenumber':d[0].strip("=").strip().replace('+','').replace('-',''), 'mtype': "N"}
            payloadArr.append([str(event['ID']), payload])

        # Now the sms_sender is responsible for doing the final
        # update Action saying things are done
        payloadArr.append(['sentinel', {
            'sentinel': {
                'ID': event['ID'],
                'Action': event['Action']
            }
        }])
        return True, payloadArr
    except Exception, e:
        cLogger.exception("with event as %s, data_loader crashed", str(event))
        return False, None
    finally:
        updateAction(event['ID'], 'Processing')
        pass
# -----------------------------------------------------------
if __name__ == '__main__':
    event = {"Campaign":"testing","English":"hahahaha","Arabic":"SubhanAllah"}
    a,b =  load_data(event)
    print a
    print b[9]
Esempio n. 6
0
                sms_dict = sms_dict_ae
            elif 'external' in campaign.lower():
                sms_dict = sms_dict_n

            payload = {}
            if d[1].strip() in "Arabic":
                message_text = sms_dict['ar']
                payload = {'message': message_text,
                           'mobilenumber': d[0].strip("=").strip().replace('+', '').replace('-', ''), 'mtype': "OL"}
            elif d[1].strip() in "English":
                message_text = sms_dict['en']
                payload = {'message': message_text,
                           'mobilenumber': d[0].strip("=").strip().replace('+', '').replace('-', ''), 'mtype': "N"}
            payloadArr.append([event['id'], payload])

        # Now the sms_sender is responsible for doing the final
        # update Action saying things are done
        payloadArr.append(['sentinel', {
            'sentinel': {
                'id': event['id'],
                'action': event['action'],
                'oid': event.get('oid')
            }
        }])
        return True, payloadArr
    except Exception, e:
        cLogger.exception("with event as %s, data_loader crashed", str(event))
        return False, None
    finally:
        updateAction(event['id'], 'Processing', oid=event.get('oid'))