Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _sentence_numfound(api_key, keywords, media, start, end):
user_mc = mcapi.MediaCloud(api_key)
query = "%s AND (%s)" % (keywords, app.util.media_to_solr(media))
start = datetime.datetime.strptime(start, '%Y-%m-%d').strftime('%Y-%m-%d')
end = datetime.datetime.strptime(end, '%Y-%m-%d').strftime('%Y-%m-%d')
response = user_mc.sentenceCount(query, solr_filter='', split=True, split_daily=True, split_start_date=start, split_end_date=end)
del response['split']['gap']
del response['split']['start']
del response['split']['end']
date_counts = []
for date, num_found in response['split'].iteritems():
date_counts.append({
"date": date[:10]
, "numFound": num_found
})
date_counts = sorted(date_counts, key=lambda d: datetime.datetime.strptime(d["date"], "%Y-%m-%d"))
return json.dumps(date_counts, separators=(',',':'))
def _get_user_mc(api_key, force_admin=False):
if force_admin or (api_key == app_mc_key):
return mcapi.AdminMediaCloud(api_key)
return mcapi.MediaCloud(api_key)
def main():
# first backup the old file
json_file_path = os.path.join(OUTPUT_DIR, OUTPUT_FILE)
backup_json_path = os.path.join(OUTPUT_DIR, BACKUP_OUTPUT_FILE)
if os.path.exists(json_file_path):
print('Backing up old media.json')
shutil.copyfile(json_file_path,backup_json_path)
# connect to mediacloud
config = ConfigParser.ConfigParser()
config.read( CONFIG_FILE )
mc = mediacloud.api.MediaCloud(config.get('mediacloud','key'))
# page through sources
print " "
print "Fetching Sources:"
sources = []
current = 0
more_rows = True
while more_rows:
print " At "+str(current)
media_list = mc.mediaList(current,ITEMS_PER_PAGE)
public_media_list = []
for media in media_list:
public_tags = [t for t in media['media_source_tags'] if t['show_on_media']==1 or t['show_on_stories']==1]
if len(public_tags)>0:
public_media_list.append(media)
[ sources.append( {'media_id': m['media_id'], 'name': m['name'], 'url': m['url'] } )
def sentences(keywords, media, start, end):
user_mc = mcapi.MediaCloud(flask_login.current_user.get_id())
query = app.util.solr_query(app.util.media_to_solr(media), start, end)
res = user_mc.sentenceList("%s AND (%s)" % (keywords, query), '', 0, 10)
return json.dumps(res, separators=(',',':'))
def _sentence_numfound(api_key, keywords, media, start, end):
user_mc = mcapi.MediaCloud(api_key)
query = "%s AND (%s)" % (keywords, app.util.media_to_solr(media))
start = datetime.datetime.strptime(start, '%Y-%m-%d').strftime('%Y-%m-%d')
end = datetime.datetime.strptime(end, '%Y-%m-%d').strftime('%Y-%m-%d')
response = user_mc.sentenceCount(query, solr_filter='', split=True, split_daily=True, split_start_date=start, split_end_date=end)
del response['split']['gap']
del response['split']['start']
del response['split']['end']
date_counts = []
for date, num_found in response['split'].iteritems():
date_counts.append({
"date": date[:10]
, "numFound": num_found
})
date_counts = sorted(date_counts, key=lambda d: datetime.datetime.strptime(d["date"], "%Y-%m-%d"))
return json.dumps(date_counts, separators=(',',':'))