Ejemplo n.º 1
0
def topic_words(topics_id):

    if access_public_topic(topics_id):
        results = topic_word_counts(TOOL_API_KEY,
                                    topics_id,
                                    snapshots_id=None,
                                    timespans_id=None,
                                    foci_id=None,
                                    q=None)
    elif is_user_logged_in():
        results = topic_word_counts(user_mediacloud_key(), topics_id)[:200]
    else:
        return jsonify({'status': 'Error', 'message': 'Invalid attempt'})

    totals = [
    ]  # important so that these get reset on the client when they aren't requested
    logger.info(request.args)
    if (is_user_logged_in()) and ('withTotals' in request.args) and (
            request.args['withTotals'] == "true"):
        # handle requests to return these results
        # and also data to compare it to for the whole topic focus
        totals = topic_word_counts(user_mediacloud_key(),
                                   topics_id,
                                   timespans_id=None,
                                   q=None)
    response = {'list': results, 'totals': totals}
    return jsonify(response)
Ejemplo n.º 2
0
def _topic_summary(topics_id):
    if access_public_topic(topics_id):
        local_mc = mc
    elif is_user_logged_in():
        local_mc = user_admin_mediacloud_client()
    else:
        return jsonify({'status': 'Error', 'message': 'Invalid attempt'})
    topic = local_mc.topic(topics_id)
    # add in snapshot and latest snapshot job status
    topic['snapshots'] = {
        'list': local_mc.topicSnapshotList(topics_id),
        'jobStatus': mc.topicSnapshotGenerateStatus(topics_id)['job_states']    # need to know if one is running
    }
    # add in spider job status
    topic['spiderJobs'] = local_mc.topicSpiderStatus(topics_id)['job_states']
    if is_user_logged_in():
        _add_user_favorite_flag_to_topics([topic])

    '''
    # add in story counts, overall seed and spidered
    feedTotal = topic_story_count(local_mc, topics_id) # with q - but not passed in for summary
    total = topic_story_count(local_mc, topics_id, timespans_id=None, q=None)  # spidered count.. how?
    spidered = total - seedTotal
    topic['seedStories'] = seedTotal
    topic['spideredStories'] = spidered
    topic['totaltories'] = total
    '''
    return topic
Ejemplo n.º 3
0
def topic_words(topics_id):
    sample_size = request.args['sample_size'] if 'sample_size' in request.args else WORD_COUNT_SAMPLE_SIZE

    if access_public_topic(topics_id):
        results = apicache.topic_word_counts(TOOL_API_KEY, topics_id, sample_size=sample_size,
                                             snapshots_id=None, timespans_id=None, foci_id=None, q=None)
    elif is_user_logged_in():
        # grab the top words, respecting all the filters
        results = apicache.topic_word_counts(user_mediacloud_key(), topics_id, sample_size=sample_size)
    else:
        return jsonify({'status': 'Error', 'message': 'Invalid attempt'})

    totals = []  # important so that these get reset on the client when they aren't requested
    logger.debug(request.args)
    if (is_user_logged_in()) and ('withTotals' in request.args) and (request.args['withTotals'] == "true"):
        # return along with the results for the overall timespan, to facilitate comparison
        snapshots_id, timespans_id, foci_id, q = filters_from_args(request.args)
        overall_timespan = _find_overall_timespan(topics_id, snapshots_id)
        totals = apicache.topic_word_counts(user_mediacloud_key(), topics_id, sample_size=sample_size,
                                            timespans_id=overall_timespan['timespans_id'], foci_id=None, q=None)

    response = {
        'list': results[:WORD_COUNT_UI_NUM_WORDS],
        'totals': totals[:WORD_COUNT_UI_NUM_WORDS],
        'sample_size': str(sample_size)
    }
    return jsonify(response)
Ejemplo n.º 4
0
def _topic_summary(topics_id):
    if access_public_topic(topics_id):
        local_mc = mc
    elif is_user_logged_in():
        local_mc = user_mediacloud_client()
    else:
        return jsonify({'status': 'Error', 'message': 'Invalid attempt'})
    topic = local_mc.topic(topics_id)
    # add in snapshot list (with version numbers, by date)
    topic['snapshots'] = _topic_snapshot_list(topic)
    if is_user_logged_in():
        add_user_favorite_flag_to_topics([topic])
    return topic
Ejemplo n.º 5
0
def map_files(topics_id):
    files = {'wordMap': 'unsupported', 'linkMap': 'not rendered'}

    if access_public_topic(topics_id) or is_user_logged_in():
        snapshots_id, timespans_id, foci_id, q = filters_from_args(
            request.args)
        map_type = MAP_TYPES[0]  # no linkMaps yet
        status = None
        prefix = _get_file_prefix(map_type, topics_id, timespans_id)
        lock_filename = prefix + ".lock"
        rendered_filename = prefix + ".gexf"
        # check if rendered file is there
        is_rendered = os.path.isfile(os.path.join(DATA_DIR, rendered_filename))
        #logger.warn(os.path.join(DATA_DIR,rendered_filename))
        #logger.warn(is_rendered)
        if is_rendered:
            status = 'rendered'
        else:
            is_generating = os.path.isfile(
                os.path.join(DATA_DIR, lock_filename))
            if not is_generating:
                _start_generating_map_file(map_type, topics_id, timespans_id)
            status = 'generating'
        files[map_type] = status
        return jsonify(files)
    else:
        return jsonify({'status': 'Error', 'message': 'Invalid attempt'})
Ejemplo n.º 6
0
def public_topics_list():
    public_topics = sorted_public_topic_list()
    if is_user_logged_in():
        public_topics = _add_user_favorite_flag_to_topics(public_topics)
    # for t in public_topics_list:
    # t['detailInfo'] = get_topic_info_per_snapshot_timespan(t['topics_id'])
    return jsonify({"topics": public_topics})
Ejemplo n.º 7
0
def topic_tag_coverage(topics_id, tags_id):
    '''
    Useful for seeing how many stories in the topic are tagged with a specific tag
    '''
    if isinstance(tags_id, list):  # doesn't repect duck-typing, but quick fix
        tags_id_str = "({})".format(" ".join([str(tid) for tid in tags_id]))
    else:
        tags_id_str = str(tags_id)
    # respect any query filter the user has set
    query_with_tag = add_to_user_query(
        "tags_id_stories:{}".format(tags_id_str))
    # now get the counts
    if access_public_topic(topics_id):
        total = topic_story_count(TOOL_API_KEY, topics_id)
        tagged = topic_story_count(
            TOOL_API_KEY, topics_id,
            q=query_with_tag)  # force a count with just the query
    elif is_user_logged_in():
        total = topic_story_count(user_mediacloud_key(), topics_id)
        tagged = topic_story_count(
            user_mediacloud_key(), topics_id,
            q=query_with_tag)  # force a count with just the query
    else:
        return None
    return {'counts': {'count': tagged['count'], 'total': total['count']}}
Ejemplo n.º 8
0
def _mc_client(admin=False):
    # return the user's client handler, or a tool one if not logged in
    if is_user_logged_in():
        client_to_use = user_mediacloud_client() if not admin else user_admin_mediacloud_client()
    else:
        client_to_use = mc
    return client_to_use
Ejemplo n.º 9
0
def map_files(topics_id):
    files = { 
        'wordMap': 'unsupported',
        'linkMap': 'not rendered'
    }

    if access_public_topic(topics_id) or is_user_logged_in():
        snapshots_id, timespans_id, foci_id, q = filters_from_args(request.args)
        map_type = MAP_TYPES[0]  # no linkMaps yet
        prefix = _get_file_prefix(map_type, topics_id, timespans_id)
        lock_filename = prefix+".lock"
        rendered_filename = prefix+".gexf"
        # check if rendered file is there
        is_rendered = os.path.isfile(os.path.join(DATA_DIR, rendered_filename))
        # logger.warn(os.path.join(DATA_DIR,rendered_filename))
        # logger.warn(is_rendered)
        if is_rendered:
            status = 'rendered'
        else:
            lockfile_path = os.path.join(DATA_DIR, lock_filename)
            is_generating = os.path.isfile(lockfile_path)
            if not is_generating:
                status = 'starting'
                _start_generating_map_file(map_type, topics_id, timespans_id)
            else:
                status = 'generating'
        files[map_type] = status
        return jsonify(files)
    else:
        return jsonify({'status': 'Error', 'message': 'Invalid attempt'})
Ejemplo n.º 10
0
def mc_client(admin=False):
    # return the user's client handler, or a tool one if not logged in
    if is_user_logged_in():
        client_to_use = user_mediacloud_client() if not admin else user_admin_mediacloud_client()
    else:
        client_to_use = mc
    return client_to_use
Ejemplo n.º 11
0
def _topic_snapshot_list(topic):
    if access_public_topic(topic['topics_id']):
        local_mc = mc
        api_key = TOOL_API_KEY
    elif is_user_logged_in():
        local_mc = user_mediacloud_client()
        api_key = user_mediacloud_key()
    else:
        return {}  # prob something smarter we can do here
    snapshots = local_mc.topicSnapshotList(topic['topics_id'])
    snapshots = sorted(snapshots, key=itemgetter('snapshots_id'))
    # add in any missing version numbers
    for idx in range(0, len(snapshots)):
        if snapshots[idx]['note'] in [None, '']:
            snapshots[idx]['note'] = idx + ARRAY_BASE_ONE
    # seed_query story count
    topic['seed_query_story_count'] = _topic_seed_story_count(topic)
    # add foci_count for display
    snapshots = _add_snapshot_foci_count(api_key, topic['topics_id'],
                                         snapshots)
    snapshots = sorted(snapshots, key=lambda d: d['snapshot_date'])
    # extra stuff
    snapshot_status = mc.topicSnapshotGenerateStatus(
        topic['topics_id'])['job_states']  # need to know if one is running
    latest = snapshots[-1] if len(snapshots) > 0 else None
    return {
        'list': snapshots,
        'jobStatus': snapshot_status,
        'latestVersion': latest['note'] if latest else 1,
    }
Ejemplo n.º 12
0
def public_topics_list():
    public_topics = sorted_public_topic_list()
    if is_user_logged_in():
        public_topics = _add_user_favorite_flag_to_topics(public_topics)
    # for t in public_topics_list:
        # t['detailInfo'] = get_topic_info_per_snapshot_timespan(t['topics_id'])
    return jsonify({"topics": public_topics})
Ejemplo n.º 13
0
def topic_filter_cascade_list():
    public_topics = sorted_public_topic_list()

    # for t in sorted_public_topics:
    #    t['detailInfo'] = get_topic_info_per_snapshot_timespan(t['topics_id'])

    # check if user had favorites or personal
    user_topics = []
    favorited_topics = []
    results = {'link_ids': []}
    if is_user_logged_in():
        user_mc = user_admin_mediacloud_client()
        link_id = request.args.get('linkId')
        results = user_mc.topicList(link_id=link_id)
        user_topics = results['topics']
        favorite_topic_ids = db.get_users_lists(user_name(), 'favorite'
                                                'Topics')
        # mark all the public topics as favorite or not
        for t in public_topics:
            t['isFavorite'] = t['topics_id'] in favorite_topic_ids
        # mark all the user's topics as favorite or not
        for t in user_topics:
            t['isFavorite'] = t['topics_id'] in favorite_topic_ids
        # fill in the list of favorite topics (need to hit server because they might no be in the other results)
        favorited_topics = [user_mc.topic(tid) for tid in favorite_topic_ids]
        for t in favorited_topics:
            t['isFavorite'] = True
    return jsonify({
        'topics': {
            'favorite': favorited_topics,
            'personal': user_topics,
            'public': public_topics
        },
        'link_ids': results['link_ids']
    })
Ejemplo n.º 14
0
def sorted_public_topic_list():
    # needs to support logged in or not
    if is_user_logged_in():
        local_mc = user_mediacloud_client()
    else:
        local_mc = mc
    public_topics = local_mc.topicList(public=True, limit=51)['topics']
    return sorted(public_topics, key=lambda t: t['name'].lower())
Ejemplo n.º 15
0
def sorted_public_topic_list():
    # needs to support logged in or not
    if is_user_logged_in():
        local_mc = user_mediacloud_client()
    else:
        local_mc = mc
    public_topics_list = local_mc.topicList(public=True)['topics']
    return sorted(public_topics_list, key=lambda t: t['name'].lower())
Ejemplo n.º 16
0
def _topic_summary(topics_id):
    if access_public_topic(topics_id):
        local_mc = mc
    elif is_user_logged_in():
        local_mc = user_admin_mediacloud_client()
    else:
        return jsonify({'status': 'Error', 'message': 'Invalid attempt'})
    topic = local_mc.topic(topics_id)
    # add in snapshot and latest snapshot job status
    topic['snapshots'] = {
        'list': local_mc.topicSnapshotList(topics_id),
        'jobStatus': mc.topicSnapshotGenerateStatus(topics_id)[
            'job_states']  # need to know if one is running
    }
    if is_user_logged_in():
        _add_user_favorite_flag_to_topics([topic])
    return topic
Ejemplo n.º 17
0
def topic_media(topics_id):
    if access_public_topic(topics_id):
        media_list = topic_media_list(TOOL_API_KEY, topics_id, snapshots_id=None, timespans_id=None, foci_id=None, sort=None, limit=None, link_id=None)
    elif is_user_logged_in():
        media_list = topic_media_list(user_mediacloud_key(), topics_id)
    else:
        return jsonify({'status': 'Error', 'message': 'Invalid attempt'})

    return jsonify(media_list)
Ejemplo n.º 18
0
def topic_stories(topics_id):
    if access_public_topic(topics_id):
        stories = topic_story_list(TOOL_API_KEY, topics_id, snapshots_id=None, timespans_id=None, foci_id=None, q=None)
    elif is_user_logged_in():
        stories = topic_story_list(user_mediacloud_key(), topics_id)
    else:
        return jsonify({'status': 'Error', 'message': 'Invalid attempt'})

    return jsonify(stories)
Ejemplo n.º 19
0
def topic_split_story_count(topics_id):
    if access_public_topic(topics_id):
        results = apicache.topic_split_story_counts(TOOL_API_KEY, topics_id, snapshots_id=None, timespans_id=None, foci_id=None,q=None)
    elif is_user_logged_in():
        results = apicache.topic_split_story_counts(user_mediacloud_key(), topics_id)
    else:
        return jsonify({'status': 'Error', 'message': 'Invalid attempt'})

    return jsonify({'results': results})
Ejemplo n.º 20
0
def topic_split_story_count(topics_id):
    if access_public_topic(topics_id):
        results = apicache.topic_split_story_counts(TOOL_API_KEY, topics_id, snapshots_id=None, timespans_id=None, foci_id=None,q=None)
    elif is_user_logged_in():
        results = apicache.topic_split_story_counts(user_mediacloud_key(), topics_id)
    else:
        return jsonify({'status': 'Error', 'message': 'Invalid attempt'})

    return jsonify({'results': results})
Ejemplo n.º 21
0
def topic_words(topics_id):
    sample_size = request.args[
        'sample_size'] if 'sample_size' in request.args else WORD_COUNT_SAMPLE_SIZE

    if access_public_topic(topics_id):
        results = apicache.topic_word_counts(TOOL_API_KEY,
                                             topics_id,
                                             sample_size=sample_size,
                                             snapshots_id=None,
                                             timespans_id=None,
                                             foci_id=None,
                                             q=None)
    elif is_user_logged_in():
        # grab the top words, respecting all the filters
        results = apicache.topic_word_counts(user_mediacloud_key(),
                                             topics_id,
                                             sample_size=sample_size)
    else:
        return jsonify({'status': 'Error', 'message': 'Invalid attempt'})

    totals = [
    ]  # important so that these get reset on the client when they aren't requested
    logger.debug(request.args)
    if (is_user_logged_in()) and ('withTotals' in request.args) and (
            request.args['withTotals'] == "true"):
        # return along with the results for the overall timespan, to facilitate comparison
        snapshots_id, timespans_id, foci_id, q = filters_from_args(
            request.args)
        overall_timespan = _find_overall_timespan(topics_id, snapshots_id)
        totals = apicache.topic_word_counts(
            user_mediacloud_key(),
            topics_id,
            sample_size=sample_size,
            timespans_id=overall_timespan['timespans_id'],
            foci_id=None,
            q=None)

    response = {
        'list': results[:WORD_COUNT_UI_NUM_WORDS],
        'totals': totals[:WORD_COUNT_UI_NUM_WORDS],
        'sample_size': str(sample_size)
    }
    return jsonify(response)
Ejemplo n.º 22
0
def topic_list():
    if not is_user_logged_in():
        topics = sorted_public_topic_list()
        return jsonify({'topics': {'public': topics}})
    else:
        user_mc = user_admin_mediacloud_client()
        link_id = request.args.get('linkId')
        topics = user_mc.topicList(link_id=link_id)
        _add_user_favorite_flag_to_topics(topics['topics'])
        return jsonify({'topics': {'personal': topics}})
Ejemplo n.º 23
0
def story_counts(topics_id):
    if access_public_topic(topics_id):
        local_key = TOOL_API_KEY
    elif is_user_logged_in():
        local_key = user_mediacloud_key()
    else:
        return jsonify({'status': 'Error', 'message': 'Invalid attempt'})
    total = apicache.topic_story_count(local_key, topics_id, timespans_id=None, snapshots_id=None, q=None, foci_id=None)
    filtered = apicache.topic_story_count(local_key, topics_id)
    return jsonify({'counts': {'count': filtered['count'], 'total': total['count']}})
Ejemplo n.º 24
0
def _public_safe_topic_story_count(topics_id, q):
    if access_public_topic(topics_id):
        total = topic_story_count(TOOL_API_KEY, topics_id, q=add_to_user_query(None))
        matching = topic_story_count(TOOL_API_KEY, topics_id, q=add_to_user_query(q))  # force a count with just the query
    elif is_user_logged_in():
        total = topic_story_count(user_mediacloud_key(), topics_id, q=add_to_user_query(None))
        matching = topic_story_count(user_mediacloud_key(), topics_id, q=add_to_user_query(q))  # force a count with just the query
    else:
        return jsonify({'status': 'Error', 'message': 'Invalid attempt'})
    return jsonify({'counts': {'count': matching['count'], 'total': total['count']}})
Ejemplo n.º 25
0
def story_counts(topics_id):
    if access_public_topic(topics_id):
        local_key = TOOL_API_KEY
    elif is_user_logged_in():
        local_key = user_mediacloud_key()
    else:
        return jsonify({'status': 'Error', 'message': 'Invalid attempt'})
    total = topic_story_count(local_key, topics_id, timespans_id=None, q=None)
    filtered = topic_story_count(local_key, topics_id)  # force a count with just the query
    return jsonify({'counts': {'count': filtered['count'], 'total': total['count']}})
Ejemplo n.º 26
0
def topic_media(topics_id):
    if access_public_topic(topics_id):
        media_list = apicache.topic_media_list(TOOL_API_KEY, topics_id, snapshots_id=None, timespans_id=None,
                                               foci_id=None, sort=None, limit=None, link_id=None)
    elif is_user_logged_in():
        media_list = apicache.topic_media_list(user_mediacloud_key(), topics_id)
    else:
        return jsonify({'status': 'Error', 'message': 'Invalid attempt'})

    return jsonify(media_list)
Ejemplo n.º 27
0
def _topic_summary(topics_id):
    local_mc = user_mediacloud_client()
    topic = local_mc.topic(topics_id)
    # add in snapshot list (with version numbers, by date)
    topic['snapshots'] = _topic_snapshot_list(topic)
    # add in fake topic_seed_query for web/mediacloud platform
    if topic_has_seed_query(topic):
        p = platform_for_web_seed_query(topic)
        topic['topic_seed_queries'].append(p)
    if is_user_logged_in():
        add_user_favorite_flag_to_topics([topic])
    return topic
Ejemplo n.º 28
0
def _public_safe_topic_story_count(topics_id, q):
    if access_public_topic(topics_id):
        total = apicache.topic_story_count(TOOL_API_KEY, topics_id, q=apicache.add_to_user_query(None))
        # force a count with just the query
        matching = apicache.topic_story_count(TOOL_API_KEY, topics_id, q=apicache.add_to_user_query(q))
    elif is_user_logged_in():
        total = apicache.topic_story_count(user_mediacloud_key(), topics_id, q=apicache.add_to_user_query(None))
        # force a count with just the query
        matching = apicache.topic_story_count(user_mediacloud_key(), topics_id, q=apicache.add_to_user_query(q))
    else:
        return jsonify({'status': 'Error', 'message': 'Invalid attempt'})
    return jsonify({'counts': {'count': matching['count'], 'total': total['count']}})
Ejemplo n.º 29
0
def api_explorer_collection_set(tag_sets_id):
    '''
    Return a list of all the (public only or public and private, depending on user role) collections in a tag set.  Not cached because this can change, and load time isn't terrible.
    :param tag_sets_id: the tag set to query for public collections
    :return: dict of info and list of collections in
    '''
    info = []
    if is_user_logged_in() and user_has_auth_role(ROLE_MEDIA_EDIT) == True:
        info = _tag_set_with_private_collections(tag_sets_id)
    else:
        info = _tag_set_with_public_collections(tag_sets_id)

    #_add_user_favorite_flag_to_collections(info['collections'])
    return jsonify(info)
Ejemplo n.º 30
0
def topic_sentence_count(topics_id):
    if access_public_topic(topics_id):
        response = topic_sentence_counts(TOOL_API_KEY,
                                         topics_id,
                                         snapshots_id=None,
                                         timespans_id=None,
                                         foci_id=None,
                                         q=None)
    elif is_user_logged_in():
        response = topic_sentence_counts(user_mediacloud_key(), topics_id)
    else:
        return jsonify({'status': 'Error', 'message': 'Invalid attempt'})

    return jsonify(response)
Ejemplo n.º 31
0
def api_explorer_collection_set(tag_sets_id):
    """
    Return a list of all the (public only or public and private, depending on user role) collections in a tag set.
    Not cached because this can change, and load time isn't terrible.
    :param tag_sets_id: the tag set to query for public collections
    :return: dict of info and list of collections in
    """
    if is_user_logged_in() and user_has_auth_role(ROLE_MEDIA_EDIT) is True:
        info = _tag_set_with_private_collections(tag_sets_id)
    else:
        info = _tag_set_with_public_collections(tag_sets_id)

    # add_user_favorite_flag_to_collections(info['collections'])
    return jsonify(info)
Ejemplo n.º 32
0
def story(topics_id, stories_id):
    if is_user_logged_in():
        local_mc = user_mediacloud_client()
        story_topic_info = apicache.topic_story_list(user_mediacloud_key(),
                                                     topics_id,
                                                     stories_id=stories_id)
        story_topic_info = story_topic_info['stories'][0]
        '''
        all_fb_count = []
        more_fb_count = True
        link_id = 0
        while more_fb_count:
            fb_page = local_mc.topicStoryListFacebookData(topics_id, limit=100, link_id=link_id)

            all_fb_count = all_fb_count + fb_page['counts']
            if 'next' in fb_page['link_ids']:
                link_id = fb_page['link_ids']['next']
                more_fb_count = True
            else:
                more_fb_count = False

        for fb_item in all_fb_count:
            if int(fb_item['stories_id']) == int(stories_id):
                story_topic_info['facebook_collection_date'] = fb_item['facebook_api_collect_date']
        '''
    else:
        return jsonify({'status': 'Error', 'message': 'Invalid attempt'})

    try:
        story_info = local_mc.story(
            stories_id)  # add in other fields from regular call
        for k in story_info.keys():
            story_topic_info[k] = story_info[k]
        for tag in story_info['story_tags']:
            if tag['tag_sets_id'] == tag_util.GEO_TAG_SET:
                geonames_id = int(tag['tag'][9:])
                try:
                    tag['geoname'] = _cached_geoname(geonames_id)
                except Exception as e:
                    # query to CLIFF failed :-( handle it gracefully
                    logger.exception(e)
                    tag['geoname'] = {}
    except MCException:
        logger.warning(
            "Story {} wasn't found in a regular story API call, but is it topic {}"
            .format(stories_id, topics_id))
    return jsonify(story_topic_info)
Ejemplo n.º 33
0
def story(topics_id, stories_id):
    if is_user_logged_in():
        local_mc = user_mediacloud_client()
        story_topic_info = apicache.topic_story_list(user_mediacloud_key(), topics_id, stories_id=stories_id)
        story_topic_info = story_topic_info['stories'][0]
        '''
        all_fb_count = []
        more_fb_count = True
        link_id = 0
        while more_fb_count:
            fb_page = local_mc.topicStoryListFacebookData(topics_id, limit=100, link_id=link_id)

            all_fb_count = all_fb_count + fb_page['counts']
            if 'next' in fb_page['link_ids']:
                link_id = fb_page['link_ids']['next']
                more_fb_count = True
            else:
                more_fb_count = False

        for fb_item in all_fb_count:
            if int(fb_item['stories_id']) == int(stories_id):
                story_topic_info['facebook_collection_date'] = fb_item['facebook_api_collect_date']
        '''
    else:
        return jsonify({'status': 'Error', 'message': 'Invalid attempt'})

    try:
        story_info = local_mc.story(stories_id)  # add in other fields from regular call
        for k in story_info.keys():
            story_topic_info[k] = story_info[k]
        for tag in story_info['story_tags']:
            if tag['tag_sets_id'] == tag_util.GEO_TAG_SET:
                geonames_id = int(tag['tag'][9:])
                try:
                    tag['geoname'] = _cached_geoname(geonames_id)
                except Exception as e:
                    # query to CLIFF failed :-( handle it gracefully
                    logger.exception(e)
                    tag['geoname'] = {}
    except MCException:
        logger.warning("Story {} wasn't found in a regular story API call, but is it topic {}".format(
            stories_id, topics_id
        ))
    return jsonify(story_topic_info)
Ejemplo n.º 34
0
def topic_tag_coverage(topics_id, tags_id):
    '''
    Useful for seeing how many stories in the topic are tagged with a specific tag
    '''
    if isinstance(tags_id, list):   # doesn't repect duck-typing, but quick fix
        tags_id_str = "({})".format(" ".join([str(tid) for tid in tags_id]))
    else:
        tags_id_str = str(tags_id)
    # respect any query filter the user has set
    query_with_tag = add_to_user_query("tags_id_stories:{}".format(tags_id_str))
    # now get the counts
    if access_public_topic(topics_id):
        total = topic_story_count(TOOL_API_KEY, topics_id)
        tagged = topic_story_count(TOOL_API_KEY, topics_id, q=query_with_tag)  # force a count with just the query
    elif is_user_logged_in():
        total = topic_story_count(user_mediacloud_key(), topics_id)
        tagged = topic_story_count(user_mediacloud_key(), topics_id, q=query_with_tag)   # force a count with just the query
    else:
        return None
    return {'counts': {'count': tagged['count'], 'total': total['count']}}
Ejemplo n.º 35
0
def get_topic_info_per_snapshot_timespan(topic_id):
    if not is_user_logged_in():
        local_mc = mc
    else:
        local_mc = user_admin_mediacloud_client()
    snapshots = {
        'list': local_mc.topicSnapshotList(topic_id),
    }
    most_recent_running_snapshot = {}
    overall_timespan = {}
    for snp in snapshots['list']:
        if snp['searchable'] == 1 and snp['state'] == "completed":
            most_recent_running_snapshot = snp
            timespans = cached_topic_timespan_list(user_mediacloud_key(), topic_id,
                                                   most_recent_running_snapshot['snapshots_id'])
            for ts in timespans:
                if ts['period'] == "overall":
                    overall_timespan = ts

    return {'snapshot': most_recent_running_snapshot, 'timespan': overall_timespan}
Ejemplo n.º 36
0
def topic_tag_coverage(topics_id, tags_id):
    '''
    Useful for seeing how many stories in the topic are tagged with a specific tag
    '''
    # respect any query filter the user has set
    snapshots_id, timespans_id, foci_id, q = filters_from_args(request.args)
    query_with_tag = add_to_user_query("tags_id_stories:{}".format(tags_id))
    # now get the counts
    if access_public_topic(topics_id):
        total = topic_story_count(TOOL_API_KEY, topics_id)
        tagged = topic_story_count(
            TOOL_API_KEY, topics_id,
            q=query_with_tag)  # force a count with just the query
    elif is_user_logged_in():
        total = topic_story_count(user_mediacloud_key(), topics_id)
        tagged = topic_story_count(
            user_mediacloud_key(), topics_id,
            q=query_with_tag)  # force a count with just the query
    else:
        return None
    return {'counts': {'count': tagged['count'], 'total': total['count']}}
Ejemplo n.º 37
0
def get_topic_info_per_snapshot_timespan(topic_id):
    if not is_user_logged_in():
        local_mc = mc
    else:
        local_mc = user_admin_mediacloud_client()
    snapshots = {
        'list': local_mc.topicSnapshotList(topic_id),
    }
    most_recent_running_snapshot = {}
    overall_timespan = {}
    for snp in snapshots['list']:
        if snp['searchable'] == 1 and snp['state'] == "completed":
            most_recent_running_snapshot = snp
            timespans = cached_topic_timespan_list(
                user_mediacloud_key(), topic_id,
                most_recent_running_snapshot['snapshots_id'])
            for ts in timespans:
                if ts['period'] == "overall":
                    overall_timespan = ts

    return {
        'snapshot': most_recent_running_snapshot,
        'timespan': overall_timespan
    }
Ejemplo n.º 38
0
def access_public_topic(topics_id):
    # check whether logged in here since it is a requirement for public access
    if (not is_user_logged_in()) and (topic_is_public(topics_id)):
        return True
    return False
Ejemplo n.º 39
0
def _api_key():
    api_key = user_mediacloud_key() \
        if is_user_logged_in() else TOOL_API_KEY
    return api_key
Ejemplo n.º 40
0
def api_key():
    return user_mediacloud_key() if is_user_logged_in() else TOOL_API_KEY
Ejemplo n.º 41
0
def access_public_topic(topics_id):
    # check whether logged in here since it is a requirement for public access
    if (not is_user_logged_in()) and (topic_is_public(topics_id)):
        return True
    return False
Ejemplo n.º 42
0
def api_key():
    return user_mediacloud_key() if is_user_logged_in() else TOOL_API_KEY