# Path: services/es.py
# def do_search(idx, typ, query, start, num, date_ranges, exclude_distributions,
#               exclude_article_types, selected_pillars, return_source=False, sort_order='_score'):
#     """Returns ElasticSearch search results.
# 
#     Fetch all documents matching the query and return a list of
#     elasticsearch results.
# 
#     This method accepts boolean queries in the Elasticsearch query string
#     syntax (see Elasticsearch reference).
# 
#     Parameters:
#         idx : str
#             The name of the elasticsearch index
#         typ : str
#             The type of document requested
#         query : str
#             A query string in the Elasticsearch query string language
#         start : int
#             An integer representing the index of the first result to be
#             retrieved
#         num : int
#             The total number of results to be retrieved
#         date_ranges : list(dict)
#             A list of dictionaries containg the upper and lower dates of the
#             requested date ranges
#         exclude_distributions : list
#             A list of strings respresenting distributions that should be
#             excluded from the search
#         exclude_article_types : list
#             A list of strings representing article types that should be
#             excluded from the search
#         selected_pillars : list
#             A list of string representing pillars that should be included into
#             the search. Each pillar is linked to a list of newspapers.
#         return_source : boolean, optional
#             A boolean indicating whether the _source of ES documents should be
#             returned or a smaller selection of document fields. The smaller set
#             of document fields (stored in _ES_RETURN_FIELDS) is the default
#         sort_order: string, optional
#             The sort order for this query. Syntax is fieldname:order, multiple
#             sort orders can be separated by commas. Note that if the sort_order
#             doesn't contain _score, no scores will be returned.
# 
#     Returns:
#         validity : boolean
#             A boolean indicating whether the input query string is valid.
#         results : list
#             A list of elasticsearch results or a message explaining why the
#             input query string is invalid.
#     """
#     q = create_query(query, date_ranges, exclude_distributions,
#                      exclude_article_types, selected_pillars)
# 
#     valid_q = indices.IndicesClient(_es()).validate_query(index=idx,
#                                                           doc_type=typ,
#                                                           body=q,
#                                                           explain=True)
# 
#     if valid_q.get('valid'):
#         if return_source:
#             # for each document return the _source field that contains all
#             # document fields (no fields parameter in the ES call)
#             return True, _es().search(index=idx, doc_type=typ, body=q,
#                                       from_=start, size=num, sort=sort_order)
#         else:
#             # for each document return the fields listed in_ES_RETURN_FIELDS
#             return True, _es().search(index=idx, doc_type=typ, body=q,
#                                       fields=_ES_RETURN_FIELDS, from_=start,
#                                       size=num, sort=sort_order)
#     return False, valid_q.get('explanations')[0].get('error')


import base64
import os
import logging
import json
import csv
import zipfile
from celery import shared_task
from math import ceil
from time import time, localtime, strftime
from sys import exc_info, stderr
from dicttoxml import dicttoxml
from django.conf import settings
from django.core.mail.message import EmailMessage
from django.http import HttpResponse
from services.es import do_search


logger = logging.getLogger(__name__)


@shared_task
def write_newspaper_classification(classification_json):
    with open(os.path.join(settings.PROJECT_PARENT, 'newspapers.txt'), 'wb') as out:
        out.write(classification_json)


def zipquerydata(*args):
    t1 = time()

    logger.debug(__name__)

    logger.debug("\n%s\n" % __name__)
    logger.debug("%d args\n" % len(args))
    for arg in args:
        logger.debug("arg: %s\n" % arg)

    if len(args) == 0:
        print "no parameters specified\n"
        return
    elif len(args) > 2:
        print "too many parameters specified\n"
        return
    else:
        arg_base64 = args[0]
        arg_str = base64.b64decode(arg_base64)

# Path: services/es.py
# def do_search(idx, typ, query, start, num, date_ranges, exclude_distributions,
#               exclude_article_types, selected_pillars, return_source=False, sort_order='_score'):
#     """Returns ElasticSearch search results.
# 
#     Fetch all documents matching the query and return a list of
#     elasticsearch results.
# 
#     This method accepts boolean queries in the Elasticsearch query string
#     syntax (see Elasticsearch reference).
# 
#     Parameters:
#         idx : str
#             The name of the elasticsearch index
#         typ : str
#             The type of document requested
#         query : str
#             A query string in the Elasticsearch query string language
#         start : int
#             An integer representing the index of the first result to be
#             retrieved
#         num : int
#             The total number of results to be retrieved
#         date_ranges : list(dict)
#             A list of dictionaries containg the upper and lower dates of the
#             requested date ranges
#         exclude_distributions : list
#             A list of strings respresenting distributions that should be
#             excluded from the search
#         exclude_article_types : list
#             A list of strings representing article types that should be
#             excluded from the search
#         selected_pillars : list
#             A list of string representing pillars that should be included into
#             the search. Each pillar is linked to a list of newspapers.
#         return_source : boolean, optional
#             A boolean indicating whether the _source of ES documents should be
#             returned or a smaller selection of document fields. The smaller set
#             of document fields (stored in _ES_RETURN_FIELDS) is the default
#         sort_order: string, optional
#             The sort order for this query. Syntax is fieldname:order, multiple
#             sort orders can be separated by commas. Note that if the sort_order
#             doesn't contain _score, no scores will be returned.
# 
#     Returns:
#         validity : boolean
#             A boolean indicating whether the input query string is valid.
#         results : list
#             A list of elasticsearch results or a message explaining why the
#             input query string is invalid.
#     """
#     q = create_query(query, date_ranges, exclude_distributions,
#                      exclude_article_types, selected_pillars)
# 
#     valid_q = indices.IndicesClient(_es()).validate_query(index=idx,
#                                                           doc_type=typ,
#                                                           body=q,
#                                                           explain=True)
# 
#     if valid_q.get('valid'):
#         if return_source:
#             # for each document return the _source field that contains all
#             # document fields (no fields parameter in the ES call)
#             return True, _es().search(index=idx, doc_type=typ, body=q,
#                                       from_=start, size=num, sort=sort_order)
#         else:
#             # for each document return the fields listed in_ES_RETURN_FIELDS
#             return True, _es().search(index=idx, doc_type=typ, body=q,
#                                       fields=_ES_RETURN_FIELDS, from_=start,
#                                       size=num, sort=sort_order)
#     return False, valid_q.get('explanations')[0].get('error')


# Path: query/tasks.py
import base64
import os
import logging
import json
import csv
import zipfile
from celery import shared_task
from math import ceil
from time import time, localtime, strftime
from sys import exc_info, stderr
from dicttoxml import dicttoxml
from django.conf import settings
from django.core.mail.message import EmailMessage
from django.http import HttpResponse
from services.es import do_search



logger = logging.getLogger(__name__)


@shared_task
def write_newspaper_classification(classification_json):
    with open(os.path.join(settings.PROJECT_PARENT, 'newspapers.txt'), 'wb') as out:
        out.write(classification_json)


def zipquerydata(*args):
    t1 = time()

    logger.debug(__name__)

    logger.debug("\n%s\n" % __name__)
    logger.debug("%d args\n" % len(args))
    for arg in args:
        logger.debug("arg: %s\n" % arg)

    if len(args) == 0:
        print "no parameters specified\n"
        return
    elif len(args) > 2:
        print "too many parameters specified\n"
        return
    else:
        arg_base64 = args[0]
        arg_str = base64.b64decode(arg_base64)

        print "arg_str: %s\n" % type(arg_str)