def search_form():
    query = Query('elasticsearch', 9200)

    param_dict = request.args.to_dict()
    current_query = param_dict['q'].strip().lower() if 'q' in param_dict else ''
    current_cat = param_dict['cat'].strip().lower() if 'cat' in param_dict else ''
    documents = query.get_by_title(current_query)

    for document in documents:
        document["hubs"] = parse_hubs(document["hubs"])

    if current_cat:
        documents = [document for document in documents if current_cat in document['hubs']]

    results_num = len(documents)
    
    DOCUMENTS_PER_PAGE = 10
    pages_num = results_num // DOCUMENTS_PER_PAGE + (results_num % DOCUMENTS_PER_PAGE != 0) 
    current_page_id = int(param_dict['page']) if 'page' in param_dict else 1
    min_doc_id = (current_page_id - 1) * DOCUMENTS_PER_PAGE
    max_doc_id = min(current_page_id * DOCUMENTS_PER_PAGE, results_num)


    full_path = request.full_path.replace('?&', '?')
    
    if 'page' in param_dict:
        full_path = full_path.replace(f'page={param_dict["page"]}', 'page=[PAGE_NUM]')
    else:
        full_path += "&page=[PAGE_NUM]"

    return render_template("search.html", query=current_query, results_num=results_num, pages_num=pages_num, 
                           current_page_id=current_page_id, full_path=full_path, 
                           documents=documents[min_doc_id:max_doc_id])
def view_doc():
    query = Query('elasticsearch', 9200)

    param_dict = request.args.to_dict()
    current_doc = param_dict['id'].strip() if 'id' in param_dict else ''
    
    # try:
    document = query.get_by_id(current_doc)
    # except:
    #     return render_template("view_doc.html", document=None)

    document["doc_id"] = current_doc
    document["hubs"] = parse_hubs(document["hubs"])
    document["text"] = document["text"].replace("\n", "<br>")
    return render_template("view_doc.html", document=document)
예제 #3
0
파일: base.py 프로젝트: codex-bot/itmo
    def __init__(self, state_controller):
        self.sdk = state_controller.sdk
        self.controller = state_controller

        # todo remove this var
        self.queries = Query(self.sdk)

        self.response_phrases = {}
예제 #4
0
def foo():
    search = request.args.get("search")
    artist = request.args.get("artist")
    album = request.args.get("album")
    unique = request.args.get("unique") == "True"
    print("unique=", type(unique))

    query = Query(search, album, artist, unique)
    result = []
    if not query.q:
        return {"result": result}
    print("query=", query.q)
    query_result = query.make_query()
    query_result = list(map((lambda x: vars(x)), query_result))
    result.extend(query_result)
    #         offset += 50
    return {"result": result}
예제 #5
0
파일: parse.py 프로젝트: dbremner/cozy
def parseQuery(text):
    tokens = peekable(_tokenize(text))
    assert tokens.next() == "fields"
    fields = list(_parseVars(tokens))

    field_names = set(f for f, t in fields)

    assumptions = []
    while tokens.peek() == "assume":
        tokens.next()
        assumptions.append(_parseQuery(field_names, [], tokens))

    queries = []
    while tokens.peek() == "query":
        tokens.next()

        # name
        name = tokens.next()

        # vars
        assert tokens.next() == "("
        qvars = list(_parseVars(tokens))
        var_names = set(v for v, t in qvars)
        assert tokens.next() == ")"

        # assumptions
        query_assumptions = []
        while tokens.peek() == "assume":
            tokens.next()
            query_assumptions.append(_parseQuery((), var_names, tokens))

        # body
        pred = _parseQuery(field_names, var_names, tokens)

        # sort?
        sort_field = None
        if tokens.peek() == "sort":
            tokens.next()
            sort_field = tokens.next()

        queries.append(
            Query(name=name,
                  vars=qvars,
                  pred=pred,
                  assumptions=query_assumptions,
                  sort_field=sort_field))

    costmodel = None
    if tokens.peek() == "costmodel":
        tokens.next()
        costmodel = ""
        while tokens.peek() is not _EOF:
            costmodel += tokens.next()

    assert tokens.peek() is _EOF
    return fields, assumptions, queries, costmodel
예제 #6
0
 def main(self):
     url = environ.get(
         'CLOUDAMQP_URL',
         'amqps://*****:*****@woodpecker.rmq.cloudamqp.com/ahsmnsum'
     )
     params = URLParameters(url)
     connection = BlockingConnection(params)
     channel = connection.channel()
     print('start')
     query = Query()
     query.connect()
     while True:
         inmessage = channel.basic_get("received", auto_ack=True)
         sleep(0.2)
         if inmessage[2] is not None:
             print('ok')
             while True:
                 self.data = query.select()
         sleep(0.8)
예제 #7
0
'''

from flask import Flask, request, jsonify, render_template, redirect, url_for, session
from flask_cors import CORS
from pymysql import cursors, connect
from datetime import datetime
from pytz import timezone, utc
from queries import Query
import flask_login
import os

app = Flask(__name__)
app.secret_key = os.urandom(24)
CORS(app)

query = Query()

login_manager = flask_login.LoginManager()

login_manager.init_app(app)

users = query.get_all_users()


class User(flask_login.UserMixin):
    pass


@login_manager.user_loader
def user_loader(username):
    if username not in users:
예제 #8
0
client = discord.Client()
token = token_data['token']
prefix  = config_data['prefix']
invite = config_data['invite']

# global vars
can_redraft = False
can_draft = False
valid_league = False

# Load custom queries
file_path = os.path.abspath(os.getcwd())+"/db/pokemon_draft_league.db"
while not valid_league:
  lname = input("Enter league name to run with Drafty: ")
  q = Query(file_path,lname)
  if q.league == None:
    print("Invalid league!")
  else:
    valid_league = True

@client.event
async def on_ready():
  print('Logged in as {0.user}'.format(client))
  print('To test out Drafty, go here: ' + invite)

@client.event
async def on_message(message):
  # Return if message is sent by bot
  if(message.author == client.user):
    return
예제 #9
0
def main():
    parser = OptionParser()
    parser.add_option("-l", "--list",
                      action="store_true", dest="show_list",
                      help="list all available data sets")
    parser.add_option("-p", "--process",
                      action="store",
                      dest="process_file",
                      metavar="FILE data_source", 
                      nargs=2,
                      help="process file into data base")
    parser.add_option("-o", "--diff-overlap",
                      action="store", 
                      nargs=2,
                      dest="diff_overlap",
                      metavar="[HASH 1] [HASH 2]", 
                      help="find the overlap of 2 data sets")
    parser.add_option("-m", "--diff-minus",
                      action="store", 
                      nargs=2,
                      dest="diff_minus",
                      metavar="[HASH 1] [HASH 2]", 
                      help="find the data in data set 1 but excluded from data set 2")
    parser.add_option("-c", "--diff-combine",
                      action="store", 
                      nargs=2,
                      dest="diff_combine",
                      metavar="[HASH 1] [HASH 2]", 
                      help="find the combined data set from data set 1 and data set 2")
    parser.add_option("-D", "--delete-all",
                      action="store_true", dest="delete_all",
                      help="Delete all data")

    (opts, args) = parser.parse_args()

    if opts.show_list:
        lookup = Cursor(es, "all_data")
        lookup.list()
    # process file
    elif opts.process_file:
        p = Processor(es, opts.process_file[0], opts.process_file[1])
        p.process()
    # show data in set 1 and set 2
    elif opts.diff_overlap:
        # translate key value into data source and version
        lookup = Cursor(es, "all_data")
        (source_1, version_1) = lookup.search_by_key(opts.diff_overlap[0])
        (source_2, version_2) = lookup.search_by_key(opts.diff_overlap[1])
        # search database
        q = Query(es)
        res = q.search_match_field_value_a_and_b(source_1, version_1, source_2, version_2)
        print json.dumps(res, indent=2)
    # show data in set 1 but not in set 2
    elif opts.diff_minus:
        # translate key value into data source and version
        lookup = Cursor(es, "all_data")
        (source_1, version_1) = lookup.search_by_key(opts.diff_minus[0])
        (source_2, version_2) = lookup.search_by_key(opts.diff_minus[1])
        # search database
        q = Query(es)
        res = q.search_match_field_value_a_not_b(source_1, version_1, source_2, version_2)
        print json.dumps(res, indent=2)
    # show data in set 1 or in set 2
    elif opts.diff_combine:
        # translate key value into data source and version
        lookup = Cursor(es, "all_data")
        (source_1, version_1) = lookup.search_by_key(opts.diff_combine[0])
        (source_2, version_2) = lookup.search_by_key(opts.diff_combine[1])
        # search database
        q = Query(es)
        res = q.search_match_field_value_a_or_b(source_1, version_1, source_2, version_2)
        print json.dumps(res, indent=2)
    elif opts.delete_all:
        q = Query(es)
        q.delete_all_data()
    else:
        # ignore other options
        parser.print_help()