<commit_before>from gevent.monkey import patch_all
patch_all()

import random
import urlparse
import json

from datetime import datetime, timedelta

import pystache
import twilio.twiml

import urllib2

from flask import (abort, after_this_request, Flask, request, render_template,
                   url_for)
from flask_cache import Cache
from flask_jsonpify import jsonify
from raven.contrib.flask import Sentry
from twilio import TwilioRestException

from models import db, aggregate_stats, log_call, call_count
from political_data import PoliticalData
from cache_handler import CacheHandler
from fftf_leaderboard import FFTFLeaderboard
from access_control_decorator import crossdomain


app = Flask(__name__)

app.config.from_object('config.ConfigProduction')

cache = Cache(app, config={'CACHE_TYPE': 'simple'})
sentry = Sentry(app)

db.init_app(app)

# Optional Redis cache, for caching Google spreadsheet campaign overrides
cache_handler = CacheHandler(app.config['REDIS_URL'])

# FFTF Leaderboard handler. Only used if FFTF Leadboard params are passed in
leaderboard = FFTFLeaderboard(app.debug, app.config['FFTF_LB_ASYNC_POOL_SIZE'],
    app.config['FFTF_CALL_LOG_API_KEY'])

call_methods = ['GET', 'POST']

data = PoliticalData(cache_handler, app.debug)


def make_cache_key(*args, **kwargs):
    path = request.path
    args = str(hash(frozenset(request.args.items())))

    return (path + args).encode('utf-8')


def play_or_say(resp_or_gather, msg_template, **kwds):
    # take twilio response and play or say a mesage
    # can use mustache templates to render keword arguments
    msg = pystache.render(msg_template, kwds)

    if msg.startswith('http'):
        resp_or_gather.play(msg)
    elif msg:
        resp_or_gather.say(msg)


def full_url_for(route, **kwds):
    return urlparse.urljoin(app.config['APPLICATION_ROOT'],
                            url_for(route, **kwds))


def parse_params(r):

    params = {
        'userPhone': r.values.get('userPhone'),
        'campaignId': r.values.get('campaignId', 'default'),
        'zipcode': r.values.get('zipcode', None),
        'repIds': r.values.getlist('repIds'),

        # only used for campaigns of infinite_loop
        'saved_zipcode': r.values.get('saved_zipcode', None),

        'ip_address': r.values.get('ip_address', None),

        # optional values for Fight for the Future Leaderboards
        # if present, these add extra logging functionality in call_complete
        'fftfCampaign': r.values.get('fftfCampaign'),
        'fftfReferer': r.values.get('fftfReferer'),
        'fftfSession': r.values.get('fftfSession')
    }

    # lookup campaign by ID
    campaign = data.get_campaign(params['campaignId'])

    if not campaign:
        return None, None

    # add repIds to the parameter set, if spec. by the campaign
    if campaign.get('repIds', None):
        if isinstance(campaign['repIds'], basestring):
            params['repIds'] = [campaign['repIds']]
        else:
            params['repIds'] = campaign['repIds']

    # get representative's id by zip code
    if params['zipcode']:
        params['repIds'] = data.locate_member_ids(
            params['zipcode'], campaign)

        if campaign.get('infinite_loop', False) == True:
            print "Saving zipcode for the future lol"
            params['saved_zipcode'] = params['zipcode']

        # delete the zipcode, since the repIds are in a particular order and
        # will be passed around from endpoint to endpoint hereafter anyway.
        del params['zipcode']

    if params['ip_address'] == None:
        params['ip_address'] = r.headers.get('x-forwarded-for', r.remote_addr)

    if 'random_choice' in campaign:
        # pick a random choice among a selected set of members
        params['repIds'] = [random.choice(campaign['random_choice'])]

    print params<commit_msg>try try try to fix the remote ip address<commit_after>from gevent.monkey import patch_all
patch_all()

import random
import urlparse
import json

from datetime import datetime, timedelta

import pystache
import twilio.twiml

import urllib2

from flask import (abort, after_this_request, Flask, request, render_template,
                   url_for)
from flask_cache import Cache
from flask_jsonpify import jsonify
from raven.contrib.flask import Sentry
from twilio import TwilioRestException

from models import db, aggregate_stats, log_call, call_count
from political_data import PoliticalData
from cache_handler import CacheHandler
from fftf_leaderboard import FFTFLeaderboard
from access_control_decorator import crossdomain


app = Flask(__name__)

app.config.from_object('config.ConfigProduction')

cache = Cache(app, config={'CACHE_TYPE': 'simple'})
sentry = Sentry(app)

db.init_app(app)

# Optional Redis cache, for caching Google spreadsheet campaign overrides
cache_handler = CacheHandler(app.config['REDIS_URL'])

# FFTF Leaderboard handler. Only used if FFTF Leadboard params are passed in
leaderboard = FFTFLeaderboard(app.debug, app.config['FFTF_LB_ASYNC_POOL_SIZE'],
    app.config['FFTF_CALL_LOG_API_KEY'])

call_methods = ['GET', 'POST']

data = PoliticalData(cache_handler, app.debug)


def make_cache_key(*args, **kwargs):
    path = request.path
    args = str(hash(frozenset(request.args.items())))

    return (path + args).encode('utf-8')


def play_or_say(resp_or_gather, msg_template, **kwds):
    # take twilio response and play or say a mesage
    # can use mustache templates to render keword arguments
    msg = pystache.render(msg_template, kwds)

    if msg.startswith('http'):
        resp_or_gather.play(msg)
    elif msg:
        resp_or_gather.say(msg)


def full_url_for(route, **kwds):
    return urlparse.urljoin(app.config['APPLICATION_ROOT'],
                            url_for(route, **kwds))


def parse_params(r):

    params = {
        'userPhone': r.values.get('userPhone'),
        'campaignId': r.values.get('campaignId', 'default'),
        'zipcode': r.values.get('zipcode', None),
        'repIds': r.values.getlist('repIds'),

        # only used for campaigns of infinite_loop
        'saved_zipcode': r.values.get('saved_zipcode', None),

        'ip_address': r.values.get('ip_address', None),

        # optional values for Fight for the Future Leaderboards
        # if present, these add extra logging functionality in call_complete
        'fftfCampaign': r.values.get('fftfCampaign'),
        'fftfReferer': r.values.get('fftfReferer'),
        'fftfSession': r.values.get('fftfSession')
    }

    # lookup campaign by ID
    campaign = data.get_campaign(params['campaignId'])

    if not campaign:
        return None, None

    # add repIds to the parameter set, if spec. by the campaign
    if campaign.get('repIds', None):
        if isinstance(campaign['repIds'], basestring):
            params['repIds'] = [campaign['repIds']]
        else:
            params['repIds'] = campaign['repIds']

    # get representative's id by zip code
    if params['zipcode']:
        params['repIds'] = data.locate_member_ids(
            params['zipcode'], campaign)

        if campaign.get('infinite_loop', False) == True:
            print "Saving zipcode for the future lol"
            params['saved_zipcode'] = params['zipcode']

        # delete the zipcode, since the repIds are in a particular order and
        # will be passed around from endpoint to endpoint hereafter anyway.
        del params['zipcode']

    if params['ip_address'] == None:
        params['ip_address'] = r.headers.get('x-forwarded-for', r.remote_addr)

        if "," in params['ip_address']:
            ips = params['ip_address'].split(", ")
            params['ip_address'] = ips[len(ips)-1]

    if 'random_choice' in campaign:
        # pick a random choice among a selected set of members
        params['repIds'] = [random.choice(campaign['random_choice'])]

    print params
{'fraction_non_alphanumeric': 0.10739705134722928, 'fraction_numerical': 0.0006354855109303507, 'mean_word_length': 3.5459272097053725, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5838239', 'n_tokens_mistral': 2407, 'n_tokens_neox': 2331, 'n_words': 644}