Example #1
0
def search_json():
    query_params = dict(request.args)
    # Early return for bad params
    if "fq" not in query_params:
        return json.dumps(None)
    query_params["fq"] = " AND ".join(
        [urllib.unquote(fq).decode('utf8') for fq in query_params["fq"]]
    )
    # Write the query to cache
    write_query_to_cache(query_params["fq"], queries)
    # Get the path to the full json path
    original_json_full_path = searchInst.search(**query_params)
    # original_json_full_path
    if original_json_full_path is not None:
        # Reformulate the JSON into whatever we're expecting on the other end
        reduced_json_full_path = reduce_json.reduce_json(
            input_path=original_json_full_path,
            output_dir=app.config["ARTICLE_SEARCH_ROOT"],
            replace=False
        )
        reduced_json_relative_path = reduced_json_full_path.replace(app.static_folder + "/", "")
        return app.send_static_file(reduced_json_relative_path)
    else:
        return json.dumps(None)
# coding=utf-8
__author__ = "kohlmannj"

import sys
import os
from nyt_api.reduce_json import reduce_json

# Get the input file
try:
    input_path = sys.argv[1]
except IndexError:
    print "No input path specified."
    input_path = None

# Get the output directory
try:
    # Try to get an output directory specified by a command line argument
    output_dir = sys.argv[2]
except IndexError:
    # Use the current directory
    print "Outputting reduced JSON file to the current directory."
    output_dir = os.getcwd()

result = reduce_json(input_path=input_path, output_dir=output_dir, replace=True, try_to_eliminate_duplicates=True)

print "\n" + result