Ejemplo n.º 1
0
def new_collector():
    """
    Route for a project account to create a new STACK collector
    """
    # Redirects an admin back to the homepage b/c nothing is loaded into the session yet
    if g.project is None:
        flash(
            'Please navigate to the New Collector page from your homepage panel.'
        )
        return redirect(url_for('index'))

    form = NewCollectorForm(request.form)

    # On submit, get info which varies by network
    if request.method == 'POST' and form.validate():
        collector_name = form.collector_name.data
        network = form.network.data

        api = None
        languages = None
        locations = None
        start_date = None
        end_date = None

        # TODO - historical for Twitter as well
        collection_type = 'realtime'

        if network == 'twitter':
            api = form.api.data
            oauth_dict = {
                'consumer_key': form.consumer_key.data,
                'consumer_secret': form.consumer_secret.data,
                'access_token': form.access_token.data,
                'access_token_secret': form.access_token_secret.data
            }

            # Optional form values are assigned 'None' if not filled out
            languages = form.languages.data
            if not languages or languages == '':
                languages = None
            else:
                languages = languages.split('\r\n')

            locations = form.locations.data
            if not locations or languages == '':
                locations = None
            else:
                locations = locations.replace('\r\n', ',').split(',')
                if len(locations) % 4 is not 0:
                    flash(
                        'Location coordinates should be entered in pairs of 4. Please try again'
                    )
                    return redirect(url_for('new_collector'))

            terms = form.twitter_terms.data
            if not terms or terms == '':
                terms = None
            else:
                terms = terms.split('\r\n')

        elif network == 'facebook':
            collection_type = form.collection_type.data
            oauth_dict = {
                'client_id': form.client_id.data,
                'client_secret': form.client_secret.data
            }

            # Optional start & end date params
            start_date = form.start_date.data
            if not start_date or start_date == '':
                start_date = None
            else:
                start_date = str(start_date)

            end_date = form.end_date.data
            if not end_date or end_date == '':
                end_date = None
            else:
                end_date = str(end_date)

            terms = form.facebook_terms.data
            terms = terms.split('\r\n')

        # Create collector w/ form data
        db = DB()
        resp = db.set_collector_detail(g.project['project_id'],
                                       collector_name,
                                       network,
                                       collection_type,
                                       oauth_dict,
                                       terms,
                                       api=api,
                                       languages=languages,
                                       location=locations,
                                       start_date=start_date,
                                       end_date=end_date)

        # If successful, redirect to collector page
        if resp['status']:
            project_config_db = db.connection[g.project['project_config_db']]
            coll = project_config_db.config
            try:
                collector = coll.find_one({'collector_name': collector_name})
                collector_id = str(collector['_id'])
                network = str(collector['network'])

                return redirect(
                    url_for('collector',
                            project_name=g.project['project_name'],
                            network=network,
                            collector_id=collector_id))
            except:
                flash(
                    'Collector created, but cannot redirect to collector page!'
                )
                return redirect(
                    url_for('home', project_name=g.project['project_name']))
        else:
            flash(resp['message'])

    return render_template('new_collector.html', form=form)
Ejemplo n.º 2
0
                    end_date = None

                client_id = raw_input('Client ID: ')
                client_secret = raw_input('Client Secret: ')

                api_credentials_dict = {
                    'client_id': client_id,
                    'client_secret': client_secret
                }

            resp = db.set_collector_detail(project_id,
                                           collector_name,
                                           network,
                                           collection_type,
                                           api_credentials_dict,
                                           terms_list,
                                           api=api,
                                           languages=languages,
                                           location=locations,
                                           start_date=start_date,
                                           end_date=end_date)

            print json.dumps(resp, indent=1)

        elif method == 'update_collector_detail':
            """
            Calls db.update_collector_detail
            Can only update a single collector param at a time

            FOR TERMS - must provide term and collection status (1 or 0)
            FOR API AUTH CREDS - must provide full list, even if updating one
Ejemplo n.º 3
0
                start_date = raw_input('Start Date: ')
                end_date = raw_input('End Date: ')

                # TODO - start and end date reqs for historical
                if start_date == 'none':
                    start_date = None
                if end_date == 'none':
                    end_date = None

                client_id = raw_input('Client ID: ')
                client_secret = raw_input('Client Secret: ')

                api_credentials_dict = {'client_id': client_id, 'client_secret': client_secret}

            resp = db.set_collector_detail(project_id, collector_name, network, collection_type, api_credentials_dict,
                                           terms_list, api=api, languages=languages, location=locations,
                                           start_date=start_date, end_date=end_date)

            print json.dumps(resp, indent=1)

        elif method == 'update_collector_detail':
            """
            Calls db.update_collector_detail
            Can only update a single collector param at a time

            FOR TERMS - must provide term and collection status (1 or 0)
            FOR API AUTH CREDS - must provide full list, even if updating one
            """
            update_params_list = [
                'collector_name',
                'api',