Ejemplo n.º 1
0
def update_collector(collector_id):
    """
    Used to update a collector details form the front-end

    TODO - Terms & start / end dates
    """
    db = DB()
    resp = db.get_collector_detail(g.project['project_id'], collector_id)
    collector = resp['collector']

    # First, populate the main form w/ info
    form_params = {'collector_name': collector['collector_name']}

    if collector['network'] == 'twitter':
        form_params['api'] = collector['api']
        form_params['consumer_key'] = collector['api_auth']['consumer_key']
        form_params['consumer_secret'] = collector['api_auth'][
            'consumer_secret']
        form_params['access_token'] = collector['api_auth']['access_token']
        form_params['access_token_secret'] = collector['api_auth'][
            'access_token_secret']

        if collector['languages']:
            languages = '\r\n'.join(collector['languages'])
            form_params['languages'] = languages
        if collector['location']:
            loc_string = ''
            r = 1
            c = 1
            for loc in collector['location']:
                if c == 1 and r > 1:
                    loc_string = loc_string + '\r\n' + loc + ','
                else:
                    if c != 4:
                        loc_string = loc_string + loc + ','
                    else:
                        loc_string = loc_string + loc

                if c == 4:
                    c = 1
                    r += 1
                else:
                    c += 1

            form_params['locations'] = loc_string

    elif collector['network'] == 'facebook':
        form_params['collection_type'] = collector['collection_type']
        form_params['client_id'] = collector['api_auth']['client_id']
        form_params['client_secret'] = collector['api_auth']['client_secret']

        # TODO - start & end dates

    form = UpdateCollectorForm(**form_params)

    # Next, create a form for each term -- if no terms, one form is needed & it's empty
    terms = collector['terms_list']
    terms_forms = []
    if terms:
        for term in terms:
            # Load form & set defaults
            form_params = {
                'term': term['term'],
                'collect': int(term['collect']),
            }
            tform = UpdateCollectorTermsForm(prefx=term['term'], **form_params)
            terms_forms.append(tform)

    # Finally, update on submission
    if request.method == 'POST':
        # Loads in the data from the form & sets initial param dict
        form_data = request.get_json()
        params = {}

        if form_data['collector_name'] != collector['collector_name']:
            params['collector_name'] = form_data['collector_name']

        # Twitter data
        if collector['network'] == 'twitter':
            if form_data['api'] != collector['api']:
                params['api'] = form_data['api']
            # If one auth param is updated, assume all are
            if form_data['consumer_key'] != collector['api_auth'][
                    'consumer_key']:
                params['api_auth'] = {
                    'consumer_key': form_data['consumer_key'],
                    'consumer_secret': form_data['consumer_secret'],
                    'access_token': form_data['access_token'],
                    'access_token_secret': form_data['access_token_secret']
                }

            languages = form_data['languages']
            if not languages or languages == '':
                languages = None
            else:
                languages = languages.split('\r\n')

            if languages != collector['languages']:
                params['languages'] = languages

            locations = form_data['locations']
            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('update_collector', collector_id=collector_id))

            if locations != collector['location']:
                params['location'] = locations

        # Facebook Data
        elif collector['network'] == 'facebook':
            if form_data['collection_type'] != collector['collection_type']:
                params['collection_type'] = form.collection_type.data
            if form_data['client_id'] != collector['api_auth']['client_id']:
                params['api_auth'] = {
                    'client_id': form_data['client_id'],
                    'client_secret': form_data['client_secret']
                }

            # TODO - start and end dates

        # Final terms dict
        params['terms_list'] = []

        # Term type value
        if collector['network'] == 'twitter':
            if collector['api'] == 'follow':
                term_type = 'handle'
            else:
                term_type = 'term'
        else:
            term_type = 'page'

        # New terms (if any)
        terms = form_data['new_terms']
        if terms and terms != '':
            terms = terms.split('\r\n')
            for t in terms:
                params['terms_list'].append({
                    'term': t,
                    'collect': 1,
                    'type': term_type,
                    'id': None
                })

        # Updated terms (if any)
        current_terms = form_data['terms']
        while current_terms:
            params['terms_list'].append({
                'term': current_terms.pop(0),
                'collect': int(current_terms.pop(0)),
                'type': term_type,
                'id': None
            })

        # Now, try updating
        resp = db.update_collector_detail(g.project['project_id'],
                                          collector_id, **params)
        if resp['status']:
            flash('Collector updated successfully!')
            return redirect(
                url_for('collector',
                        project_name=g.project['project_name'],
                        network=collector['network'],
                        collector_id=collector_id))
        else:
            flash(resp['message'])
            return redirect(
                url_for('update_collector', collector_id=collector_id))

    return render_template('update_collector.html',
                           collector=collector,
                           form=form,
                           terms_forms=terms_forms)
Ejemplo n.º 2
0
                    if end_date == 'none':
                        params['end_date'] = None
                    else:
                        params['end_date'] = end_date

                elif update_param == 'auth':
                    client_id = raw_input('Client ID: ')
                    client_secret = raw_input('Client Secret: ')

                    api_credentials_dict = {
                        'client_id': client_id,
                        'client_secret': client_secret
                    }
                    params['api_auth'] = api_credentials_dict

            resp = db.update_collector_detail(project_id, collector_id,
                                              **params)
            print json.dumps(resp, indent=1)

    elif wrapper == 'controller' and method in controller_processes:
        """
        python __main__.py controller collect|process|insert start|stop|restart project_id {collector_id|network}

        WHERE

        collector_id - optional, only needed for a collection controller
        network - optional, needed for processor or inserter controllers
        """
        project_id = sys.argv[4]

        if method == 'collect':
            collector_id = sys.argv[5]
Ejemplo n.º 3
0
                    if end_date == 'none':
                        params['end_date'] = None
                    else:
                        params['end_date'] = end_date

                elif update_param == 'auth':
                    client_id = raw_input('Client ID: ')
                    client_secret = raw_input('Client Secret: ')

                    api_credentials_dict = {
                        'client_id' : client_id,
                        'client_secret': client_secret
                    }
                    params['api_auth'] = api_credentials_dict

            resp = db.update_collector_detail(project_id, collector_id, **params)
            print json.dumps(resp, indent=1)

    elif wrapper == 'controller' and method in controller_processes:
        """
        python __main__.py controller collect|process|insert start|stop|restart project_id {collector_id|network}

        WHERE

        collector_id - optional, only needed for a collection controller
        network - optional, needed for processor or inserter controllers
        """
        project_id = sys.argv[4]

        if method == 'collect':
            collector_id = sys.argv[5]