Ejemplo n.º 1
0
def build_credentials_woutSession(email):
    oauth2_tokens = {}
    user = db.find_one('user', {'email': email})

    resp = requests.get(TOKEN_INFO_URI.format(user['ga_accesstoken']))
    if ('error' in resp.json().keys()):
        data = [('client_id', CLIENT_ID.strip()),
                ('client_secret', CLIENT_SECRET.strip()),
                ('refresh_token', user['ga_refreshtoken']),
                ('grant_type', 'refresh_token')]
        resp = requests.post(ACCESS_TOKEN_URI, data).json()
        print(resp)
        db.find_and_modify('user',
                           query={'email': email},
                           ga_accesstoken=resp['access_token'])
        oauth2_tokens['access_token'] = resp['access_token']
    else:
        oauth2_tokens['access_token'] = user['ga_accesstoken']

    oauth2_tokens['refresh_token'] = user['ga_refreshtoken']

    return google.oauth2.credentials.Credentials(
        oauth2_tokens['access_token'],
        refresh_token=oauth2_tokens['refresh_token'],
        client_id=CLIENT_ID,
        client_secret=CLIENT_SECRET,
        token_uri=ACCESS_TOKEN_URI)
Ejemplo n.º 2
0
def google_gaconnectauth_redirect():
    req_state = flask.request.args.get('state', default=None, type=None)
    if req_state != flask.session[AUTH_STATE_KEY]:
        response = flask.make_response('Invalid state parameter', 401)
        return response

    session = OAuth2Session(CLIENT_ID,
                            CLIENT_SECRET,
                            scope=GA_CONNECT_AUTHORIZATION_SCOPE,
                            state=flask.session[AUTH_STATE_KEY],
                            redirect_uri=GA_CONNECTAUTH_REDIRECT_URI)
    try:
        oauth2_tokens = session.fetch_access_token(
            ACCESS_TOKEN_URI, authorization_response=flask.request.url)
    except:
        return flask.redirect(BASE_URI, code=302)

    # Obtain current analytics account email
    user = db.find_one('user', {'email': flask.session['email']})
    if (user['ga_accesstoken']):
        #        resp = requests.get(TOKEN_INFO_URI.format(user['ga_accesstoken'])).json()
        #        if ('error' in resp.keys()):
        #            data = [('client_id', CLIENT_ID.strip()),
        #                    ('client_secret', CLIENT_SECRET.strip()),
        #                    ('refresh_token', user['ga_refreshtoken']),
        #                    ('grant_type', 'refresh_token')]
        #            resp = requests.post(ACCESS_TOKEN_URI, data).json()
        #            resp = requests.get(TOKEN_INFO_URI.format(resp['access_token'])).json()
        #        current_analyticsemail = resp['email']
        current_analyticsemail = user['ga_email']

        #        # Obtain new analytics account email and
        #        user_info = get_user_info()
        #        new_analyticsemail = user_info['email']
        resp = requests.get(
            TOKEN_INFO_URI.format(oauth2_tokens['access_token'])).json()
        new_analyticsemail = resp['email']
        # Compare them
        if (current_analyticsemail != new_analyticsemail):
            # If emails are not same, remove old datasources
            db.DATABASE['datasource'].remove({'email': user['email']})
            db.DATABASE['notification'].remove({'email': user['email']})
    else:
        resp = requests.get(
            TOKEN_INFO_URI.format(oauth2_tokens['access_token'])).json()
        new_analyticsemail = resp['email']
    flask.session[AUTH_TOKEN_KEY] = oauth2_tokens
    db.find_and_modify(collection='user',
                       query={'_id': user['_id']},
                       ga_accesstoken=oauth2_tokens['access_token'],
                       ga_refreshtoken=oauth2_tokens['refresh_token'],
                       ga_email=new_analyticsemail)
    flask.session['ga_accesstoken'] = oauth2_tokens['access_token']
    #    viewId = google_analytics.get_first_profile_id()
    #    db.find_and_modify(collection='user', query={'email': flask.session['email']}, viewId=viewId)

    #User Segmentation
    #    segmentationOfAnalytics.segmentationOfAnalytics(flask.session['email'])
    return flask.redirect(BASE_URI, code=302)
Ejemplo n.º 3
0
def google_adwconnectauth_redirect():
    req_state = flask.request.args.get('state', default=None, type=None)
    if req_state != flask.session[AUTH_STATE_KEY]:
        response = flask.make_response('Invalid state parameter', 401)
        return response

    session = OAuth2Session(CLIENT_ID,
                            CLIENT_SECRET,
                            scope=ADW_CONNECT_AUTHORIZATION_SCOPE,
                            state=flask.session[AUTH_STATE_KEY],
                            redirect_uri=ADW_CONNECTAUTH_REDIRECT_URI)
    try:
        oauth2_tokens = session.fetch_access_token(
            ACCESS_TOKEN_URI, authorization_response=flask.request.url)
    except:
        return flask.redirect(BASE_URI, code=302)

    flask.session[AUTH_TOKEN_KEY] = oauth2_tokens
    db.find_and_modify(collection='user',
                       query={'email': flask.session['email']},
                       adw_accesstoken=oauth2_tokens['access_token'],
                       adw_refreshtoken=oauth2_tokens['refresh_token'])
    return flask.redirect(BASE_URI, code=302)
Ejemplo n.º 4
0
def check_tokens(user):
    filename = 'check_tokens.log'
    if os.path.exists(filename):
        append_write = 'a'  # append if already exists
    else:
        append_write = 'w'  # make a new file if not
    f = open(filename, append_write)
    try:
        #Check Slack Tokens
        slack_token = user['sl_accesstoken']
        slackActivity = True
        gaActivity = True
        if slack_token:
            client = WebClient(token=slack_token)
            try:
                client.auth_test()
                #Put token into session cache
                if not flask.session.get('sl_accesstoken'):
                    flask.session['sl_accesstoken'] = user['sl_accesstoken']
            except Exception as err:
                print('Slack Token Error:', str(err))
                if 'token_revoked' in str(err) or 'account_inactive' in str(
                        err):
                    db.find_and_modify('user',
                                       query={'_id': user['_id']},
                                       sl_accesstoken='',
                                       sl_teamid='',
                                       sl_userid='')
                if flask.session.get('sl_accesstoken'):
                    flask.session['sl_accesstoken'] = ''
                slackActivity = False
        else:
            flask.session['sl_accesstoken'] = ''
        #
        #Check Google Analytics Tokens
        if user['ga_accesstoken']:
            try:
                build_credentials_woutSession(user['email'])
                if not flask.session.get('ga_accesstoken'):
                    flask.session['ga_accesstoken'] = user['ga_accesstoken']
            except Exception as err:
                print('Google Analytics Token Error:', str(err))
                if 'access_token' in str(err):
                    db.find_and_modify('user',
                                       query={'_id': user['_id']},
                                       ga_accesstoken='',
                                       ga_refreshtoken='')
                if flask.session.get('ga_accesstoken'):
                    flask.session['ga_accesstoken'] = ''
                gaActivity = False
        else:
            flask.session['ga_accesstoken'] = ''
        f.write(
            'INFO - Email: {} - Slack Response: {} - GA Response: {}\n'.format(
                user['email'], slackActivity, gaActivity))
        flask.session['TOKENCHECKED'] = 'Checked'
        return True
    except Exception as ex:
        f.write('ERROR - Email: {} - Error: {}\n'.format(
            user['email'], str(ex)))
        return False
Ejemplo n.º 5
0
def google_loginauth_redirect():
    req_state = flask.request.args.get('state', default=None, type=None)
    if req_state != flask.session[AUTH_STATE_KEY]:
        response = flask.make_response('Invalid state parameter', 401)
        return response

    session = OAuth2Session(CLIENT_ID,
                            CLIENT_SECRET,
                            scope=LOGIN_AUTHORIZATION_SCOPE,
                            state=flask.session[AUTH_STATE_KEY],
                            redirect_uri=LOGINAUTH_REDIRECT_URI)

    oauth2_tokens = session.fetch_access_token(
        ACCESS_TOKEN_URI, authorization_response=flask.request.url)

    flask.session[AUTH_TOKEN_KEY] = oauth2_tokens
    user_info = get_user_info()
    flask.session['email'] = user_info['email']
    #    new_user = User(name=user_info['name'], email=flask.session['email'], password="")
    try:
        name = user_info['name']
    except:
        name = ""
    try:
        firstname = user_info['given_name']
    except:
        firstname = ""
    try:
        lastname = user_info['family_name']
    except:
        lastname = ""
    new_user = User(name=name,
                    firstname=firstname,
                    lastname=lastname,
                    email=flask.session['email'],
                    password="")
    new_user.insert()
    user = db.find_one('user', {'email': user_info['email']})
    ip_addr = flask.request.environ.get('HTTP_X_REAL_IP',
                                        flask.request.remote_addr)
    if 'tz_offset' in user.keys():
        if not user['tz_offset']:
            if ip_addr:
                url = 'https://ipinfo.io/' + ip_addr + '/json'
                res = urlopen(url)
                data = load(res)
                tz = data['timezone']
                pst = pytz.timezone(tz)
                now = datetime.now()
                days = pst.utcoffset(now).days
                if days < 0:
                    tz_offset = (pst.utcoffset(now).days * 24 * 60 * 60 +
                                 pst.utcoffset(now).seconds) // 3600
                else:
                    tz_offset = pst.utcoffset(now).seconds // 3600
                db.find_and_modify('user',
                                   query={'_id': user['_id']},
                                   tz_offset=tz_offset)
            else:
                tz_offset = 0
                db.find_and_modify('user',
                                   query={'_id': user['_id']},
                                   tz_offset=tz_offset)
    else:
        if ip_addr:
            url = 'https://ipinfo.io/' + ip_addr + '/json'
            res = urlopen(url)
            data = load(res)
            tz = data['timezone']
            pst = pytz.timezone(tz)
            now = datetime.now()
            days = pst.utcoffset(now).days
            if days < 0:
                tz_offset = (pst.utcoffset(now).days * 24 * 60 * 60 +
                             pst.utcoffset(now).seconds) // 3600
            else:
                tz_offset = pst.utcoffset(now).seconds // 3600
            db.find_and_modify('user',
                               query={'_id': user['_id']},
                               tz_offset=tz_offset)
        else:
            tz_offset = 0
            db.find_and_modify('user',
                               query={'_id': user['_id']},
                               tz_offset=tz_offset)

    resp = check_tokens(user)
    if resp:
        flask.session['sl_accesstoken'] = user['sl_accesstoken']
        flask.session['ga_accesstoken'] = user['ga_accesstoken']

    return flask.redirect(BASE_URI, code=302)
Ejemplo n.º 6
0
def segmentationOfAnalytics(email):
    db.init()
    mservice = google_analytics.build_management_api_v3_woutSession(email)
    rservice = google_analytics.build_reporting_api_v4_woutSession(email)
    NoU_array = []
    dS_array = []
    counter = 0
    start_time = time.time()
    accounts = mservice.management().accounts().list().execute()
    for acc in accounts.get('items'):
        time.sleep(1)
        accountId = acc.get('id')
        accountName = acc.get('name')
        webproperties = mservice.management().webproperties().list(
            accountId=accountId).execute()
        for webproperty in webproperties.get('items'):
            time.sleep(1)
            propertyId = webproperty.get('id')
            propertyName = webproperty.get('name')
            views = mservice.management().profiles().list(
                accountId=accountId, webPropertyId=propertyId).execute()
            for view in views.get('items'):
                time.sleep(1)
                viewId = view.get('id')
                viewName = view.get('name')
                try:
                    numberOfUsers = getNumberofUsers(rservice, viewId)
                except Exception as ex:
                    if "sufficient permissions" in str(ex):
                        continue
                    else:
                        raise ex
                counter += 1
                print("Requests Number: %s" % counter)
                if counter == 9:  #Limit is 10 requests
                    print("Approached to Limit")
                    stop_time = time.time()
                    sleeptime = 11 - (stop_time - start_time)
                    if sleeptime > 0:
                        print("Waiting for %s seconds" % sleeptime)
                        time.sleep(sleeptime)  #10 requests per 10 seconds
                    counter = 0
                    start_time = time.time()
                NoU_array += [numberOfUsers]
                dS_array += [{
                    "accountID": accountId,
                    "accountName": accountName,
                    "propertyID": propertyId,
                    "propertyName": propertyName,
                    "viewID": viewId,
                    "viewName": viewName
                }]
#                print("Number Of Users: %s"%numberOfUsers)
#                print("Segments: %s"%segments)
    maxNoU = max(NoU_array)
    index = NoU_array.index(maxNoU)
    selectedsegment = segmentOf(maxNoU)
    segmentationSource = dS_array[index]
    #    print("Selected Segments: %s"%selectedsegment)
    db.find_and_modify("user",
                       query={"email": email},
                       segment=selectedsegment,
                       segmentationSource=segmentationSource)
Ejemplo n.º 7
0
                      enhancedECommerceTracking)
                break
        print(email + ' is finished: ', eCommerceTracking,
              enhancedECommerceTracking)
        counter += 1
        print('%s/%s' % (counter, len(users)))
    except Exception as ex:
        counter += 1
        exusers[email] = ex
        nullECommerceList += [email]

for user in allusers:
    email = user.get('email')
    if user.get('ga_accesstoken'):
        if email in eCommerceList:
            db.find_and_modify('user', {'email': email},
                               eCommerceActivity=True)
            print('%s in eCommerceList' % email)
            logging.info('%s in eCommerceList' % email)
            resp = autoPilotRequestwithExponentialBackoff(contact_id=email)
            if resp.status_code == 200:
                logging.info(
                    "SUCCESS - {} is inserted into ECommerce List for Autopilot"
                    .format(email))
            else:
                logging.info(
                    "ERROR - {} cannot be inserted into ECommerce List for Autopilot - Status Code: {} "
                    .format(email, resp.status_code))
        elif email in nullECommerceList:
            db.find_and_modify('user', {'email': email},
                               eCommerceActivity=None)
            print('%s not GA user anymore' % email)
Ejemplo n.º 8
0
def do_job(tasks_to_accomplish):
    while True:
        try:
            '''
                try to get task from the queue. get_nowait() function will 
                raise queue.Empty exception if the queue is empty. 
                queue(False) function would do the same task also.
            '''
            task = tasks_to_accomplish.get_nowait()
            logging.basicConfig(
                filename="orchestrator.log",
                filemode='a',
                format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                level=logging.INFO)
            db.init()
            db2.init()
            user = db.find_one('user', {'email': task['email']})
            slack_token = user['sl_accesstoken']
            dataSource = db.find_one('datasource',
                                     query={'_id': task['datasourceID']})
            logging.info(
                f"User Email: {user['email']} Data Source ID: {task['datasourceID']} Task Type: {task['type']}"
            )
            if (task['type'] == 'performancechangetracking'):
                performancechangetracking(slack_token, task, dataSource)
                db.find_and_modify('notification',
                                   query={'_id': task['_id']},
                                   lastRunDate=time.time())
            elif (task['type'] == 'shoppingfunnelchangetracking'):
                shoppingfunnelchangetracking(slack_token, task, dataSource)
                db.find_and_modify('notification',
                                   query={'_id': task['_id']},
                                   lastRunDate=time.time())
            elif (task['type'] == 'costprediction'):
                costprediction(slack_token, task, dataSource)
                db.find_and_modify('notification',
                                   query={'_id': task['_id']},
                                   lastRunDate=time.time())
            elif (task['type'] == 'performancegoaltracking'):
                performancegoaltracking(slack_token, task, dataSource)
                db.find_and_modify('notification',
                                   query={'_id': task['_id']},
                                   lastRunDate=time.time())
            elif (task['type'] == 'performancechangealert'):
                performancechangealert(slack_token, task, dataSource)
                db.find_and_modify('notification',
                                   query={'_id': task['_id']},
                                   lastRunDate=time.time())
            elif task['type'] == 'analyticsAudit':
                if slack_token:
                    if "channelID" in dataSource:
                        analyticsAudit(slack_token, task, dataSource)
                    else:
                        analyticsAudit_without_slack(task, dataSource)
                else:
                    analyticsAudit_without_slack(task, dataSource)

        except queue.Empty:
            time.sleep(WAITTIME)
        except Exception as error:
            exc_type, exc_obj, exc_tb = sys.exc_info(
            )  # this is to get error line number and description.
            file_name = [tb[0] for tb in traceback.extract_tb(exc_tb)
                         ]  # to get File Name.
            line_no = [tb[1] for tb in traceback.extract_tb(exc_tb)
                       ]  #to get Line Number
            error_type = exc_type.__name__
            error_message = str(error)
            error_string = f"Error Type: {error_type}, Error Message: {error_message}, File Name : {file_name}, Line no : {line_no}"
            logging.error(
                f"TASK DID NOT RUN --- User Email: {user['email']} Data Source ID: {task['datasourceID']} Task Type: {task['type']} --- {error_string}"
            )
    return True
Ejemplo n.º 9
0
def authorized(self):
    """
    This is the route/function that the user will be redirected to by
    the provider (e.g. Twitter) after the user has logged into the
    provider's website and authorized your app to access their account.
    """
    if self.redirect_url:
        next_url = self.redirect_url
    elif self.redirect_to:
        next_url = url_for(self.redirect_to)
    else:
        next_url = "/"
    log.debug("next_url = %s", next_url)

    # check for error in request args
    error = request.args.get("error")
    if error:
        error_desc = request.args.get("error_description")
        error_uri = request.args.get("error_uri")
        log.warning(
            "OAuth 2 authorization error: %s description: %s uri: %s",
            error,
            error_desc,
            error_uri,
        )
        oauth_error.send(
            self, error=error, error_description=error_desc, error_uri=error_uri
        )
        return redirect(next_url)

    state_key = "{bp.name}_oauth_state".format(bp=self)
    if state_key not in flask.session:
        # can't validate state, so redirect back to login view
        log.info("state not found, redirecting user to login")
        return redirect(url_for(".login"))

    state = flask.session[state_key]
    log.debug("state = %s", state)
    self.session._state = state
    del flask.session[state_key]

    self.session.redirect_uri = url_for(".authorized", _external=True)

    log.debug("client_id = %s", self.client_id)
    log.debug("client_secret = %s", self.client_secret)
    try:
        token = self.session.fetch_token(
            self.token_url,
            authorization_response=request.url,
            client_secret=self.client_secret,
            **self.token_url_params
        )
        #token['access_token']
        #token['bot']['bot-access-token']
        user = db.find_one(collection='user', query={'email': flask.session['email']})
        db.find_and_modify(collection='user', query={'_id': user['_id']},
                           sl_accesstoken=token['bot']['bot_access_token'], sl_userid=token['user_id'], sl_teamid = token['team_id'])
        flask.session['sl_accesstoken'] = token['bot']['bot_access_token']
    except MissingCodeError as e:
        e.args = (
            e.args[0],
            "The redirect request did not contain the expected parameters. Instead I got: {}".format(
                json.dumps(request.args)
            ),
        )
        raise

    results = oauth_authorized.send(self, token=token) or []
    set_token = True
    for func, ret in results:
        if isinstance(ret, (Response, current_app.response_class)):
            return ret
        if ret == False:
            set_token = False

    if set_token:
        try:
            self.token = token

        except ValueError as error:
            log.warning("OAuth 2 authorization error: %s", str(error))
            oauth_error.send(self, error=error)
            
#    # When the slack connection is completed send notification user to set time
#    headers = {'Content-type': 'application/json', 'Authorization': 'Bearer ' + token['access_token']}
#    data = {
#        "channel": token['incoming_webhook']['channel'],
#        "attachments": [{
#            "text": """Hi, I am HeyBooster, Default notification time is set as 07:00.
#Click "Change" button for changing it.""",
#            "title": "Blog Yazıları",
#            "title_link": "https://blog.boostroas.com/tr/"},
#            {
#                "text": "",
#                "callback_id": "notification_form",
#                "color": "#3AA3E3",
#                "attachment_type": "default",
#                "actions": [
#                    {
#                        "name": "change",
#                        "text": "Change",
#                        "type": "button",
#                        "value": "change"
#                    }]
#            }]}
#    requests.post(URL.format('chat.postMessage'), data=json.dumps(data), headers=headers)
#    modules = db.find("notification", query={'email': google_auth.get_user_info()['email']})
#    lc_tz_offset = datetime.now(timezone.utc).astimezone().utcoffset().seconds // 3600
    data = [('token', token['access_token']),
            ('user', token['user_id'])]
    usr_tz_offset = requests.post(URL.format('users.info'), data).json()['user']['tz_offset'] // 3600
    db.find_and_modify(collection='user', query={'_id': user['_id']},
                           tz_offset=usr_tz_offset)
#    if (7 >= (usr_tz_offset - lc_tz_offset)):
#        default_time = str(7 - (usr_tz_offset - lc_tz_offset)).zfill(2)
#    else:
#        default_time = str(24 + (7 - (usr_tz_offset - lc_tz_offset))).zfill(2)
#    for module in modules:
#        db.find_and_modify("notification", query={'_id': module['_id']}, timeofDay="%s.00" % (default_time))
            
    return redirect(next_url)