def topic_create(): user_mc = user_mediacloud_client() name = request.form['name'] description = request.form['description'] solr_seed_query = request.form['solr_seed_query'] start_date = request.form['start_date'] end_date = request.form['end_date'] optional_args = { 'max_iterations': request.form['max_iterations'] if 'max_iterations' in request.form and request.form['max_iterations'] != 'null' else None, 'max_stories': request.form['max_stories'] if 'max_stories' in request.form and request.form['max_stories'] != 'null' else flask_login.current_user.profile['limits']['max_topic_stories'], } try: topic_result = user_mc.topicCreate(name=name, description=description, solr_seed_query=solr_seed_query, start_date=start_date, end_date=end_date, media_tags_ids=[COLLECTION_US_TOP_ONLINE], # HACK: can't save without one of these in place (for now) **optional_args, )['topics'][0] topics_id = topic_result['topics_id'] logger.info("Created new topic \"{}\" as {}".format(name, topics_id)) # if this includes any of the US-centric collections, add the retweet partisanship subtopic by default # client will either make a empty snapshot, or a spidering one return topic_summary(topics_id) except mediacloud.error.MCException as e: logging.error("Topic creation failed {}".format(name)) logging.exception(e) return json_error_response(e.message, e.status_code) except Exception as e: logging.error("Topic creation failed {}".format(name)) logging.exception(e) return json_error_response(str(e), 500)
def topic_create(): user_mc = user_mediacloud_client() name = request.form['name'] description = request.form['description'] solr_seed_query = request.form['solr_seed_query'] start_date = request.form['start_date'] end_date = request.form['end_date'] optional_args = { 'is_public': request.form['is_public'] if 'is_public' in request.form else None, 'is_logogram': request.form['is_logogram'] if 'is_logogram' in request.form else None, 'ch_monitor_id': request.form['ch_monitor_id'] if len(request.form['ch_monitor_id']) > 0 and request.form['ch_monitor_id'] != 'null' else None, 'max_iterations': request.form['max_iterations'] if 'max_iterations' in request.form else None, 'max_stories': request.form['max_stories'] if 'max_stories' in request.form and request.form['max_stories'] != 'null' else flask_login.current_user.profile['max_topic_stories'], } # parse out any sources and collections to add media_ids_to_add = ids_from_comma_separated_str(request.form['sources[]']) tag_ids_to_add = ids_from_comma_separated_str( request.form['collections[]']) try: topic_result = user_mc.topicCreate(name=name, description=description, solr_seed_query=solr_seed_query, start_date=start_date, end_date=end_date, media_ids=media_ids_to_add, media_tags_ids=tag_ids_to_add, **optional_args)['topics'][0] topics_id = topic_result['topics_id'] logger.info("Created new topic \"{}\" as {}".format(name, topics_id)) # if this includes any of the US-centric collections, add the retweet partisanship subtopic by default if set(tag_ids_to_add).intersection(US_COLLECTIONS): add_retweet_partisanship_to_topic( topic_result['topics_id'], 'Retweet Partisanship', 'Subtopics driven by our analysis of Twitter followers of Trump and Clinton during the 2016 election season. Each media soure is scored based on the ratio of retweets of their stories in those two groups.' ) # client will either make a empty snapshot, or a spidering one return topic_summary(topics_id) except Exception as e: logging.error("Topic creation failed {}".format(name)) logging.exception(e) return json_error_response(str(e), 500) except mediacloud.error.MCException as e: logging.error("Topic creation failed {}".format(name)) logging.exception(e) return json_error_response(e.message, e.status_code)
def topic_update(topics_id): # update the seed query first 5 MUST be filled in) args = { 'name': request.form['name'] if 'name' in request.form else None, 'description': request.form['description'] if 'description' in request.form else None, 'solr_seed_query': request.form['solr_seed_query'] if 'solr_seed_query' in request.form else None, 'start_date': request.form['start_date'] if 'start_date' in request.form else None, 'end_date': request.form['end_date'] if 'end_date' in request.form else None, 'is_public': request.form['is_public'] if 'is_public' in request.form else None, 'is_logogram': request.form['is_logogram'] if 'is_logogram' in request.form else None, 'ch_monitor_id': request.form['ch_monitor_id'] if 'ch_monitor_id' in request.form and request.form['ch_monitor_id'] is not None and request.form['ch_monitor_id'] != 'null' and len(request.form['ch_monitor_id']) > 0 else None, 'max_iterations': request.form['max_iterations'] if 'max_iterations' in request.form else None, 'max_stories': request.form['max_stories'] if 'max_stories' in request.form and request.form['max_stories'] != 'null' else current_user.profile['max_topic_stories'], 'twitter_topics_id': request.form['twitter_topics_id'] if 'twitter_topics_id' in request.form else None } # parse out any sources and collections to add media_ids_to_add = ids_from_comma_separated_str( request.form['sources[]'] if 'sources[]' in request.form else '') tag_ids_to_add = ids_from_comma_separated_str( request.form['collections[]'] if 'collections[]' in request.form else '') # hack to support twitter-only topics if (len(media_ids_to_add) is 0) and (len(tag_ids_to_add) is 0): media_ids_to_add = None tag_ids_to_add = None # update the seed query (the client will start the spider themselves user_mc = user_mediacloud_client() result = user_mc.topicUpdate(topics_id, media_ids=media_ids_to_add, media_tags_ids=tag_ids_to_add, **args) return topic_summary( topics_id) # give them back new data, so they can update the client
def topic_snapshot_generate(topics_id): user_mc = user_mediacloud_client() if 'snapshot_id' in request.form: # generate into the one passed in snapshots_id = request.form[ 'snapshotId'] if 'snapshotId' in request.form else None else: # make a new snapshot new_snapshot = user_mc.topicCreateSnapshot( topics_id, note=_next_snapshot_number(topics_id))['snapshot'] snapshots_id = new_snapshot['snapshots_id'] # and now generate into the existing or the new snapshot job = user_mc.topicGenerateSnapshot(topics_id, snapshots_id=snapshots_id) return topic_summary(topics_id)
def topic_snapshot_spider(topics_id): user_mc = user_mediacloud_client() # kick off a spider, which will also generate a snapshot if 'snapshotId' in request.form: # generate into the one passed in snapshots_id = request.form[ 'snapshotId'] if 'snapshotId' in request.form else None else: # make a new snapshot new_snapshot = user_mc.topicCreateSnapshot( topics_id, note=_next_snapshot_number(topics_id))['snapshot'] snapshots_id = new_snapshot['snapshots_id'] # and now spider into the existinng or the new snapshot job = user_mc.topicSpider(topics_id, snapshots_id=snapshots_id) return topic_summary(topics_id)
def topic_snapshot_spider(topics_id): """ This supports making a new version, or re-using the current one. It also allows for spidering or not. :param topics_id: :return: """ user_mc = user_mediacloud_client() # kick off a spider, which will also generate a snapshot if request.form[ 'snapshotId'] is not None and request.form['snapshotId'] != "null": # generate into the one passed in snapshots_id = request.form[ 'snapshotId'] if 'snapshotId' in request.form else None else: # make a new snapshot new_snapshot = user_mc.topicCreateSnapshot( topics_id, note=_next_snapshot_number(topics_id))['snapshot'] snapshots_id = new_snapshot['snapshots_id'] # and now spider into the existing or the new snapshot if request.form['spider'] == '1': user_mc.topicSpider(topics_id, snapshots_id=snapshots_id) else: user_mc.topicGenerateSnapshot(topics_id, snapshots_id=snapshots_id) return topic_summary(topics_id)
def topic_snapshot_create(topics_id): user_mc = user_mediacloud_client() # make a new snapshot new_snapshot = user_mc.topicCreateSnapshot( topics_id, note=_next_snapshot_number(topics_id))['snapshot'] return topic_summary(topics_id)