示例#1
0
async def crawl_all():
    fts = [asyncio.ensure_future(crawl()) for _ in range(0, 50)]

    for f in asyncio.as_completed(fts):
        try:
            puzzle = await f
            puzzle = Puzzle(**puzzle)
            session.add(puzzle)
            session.commit()
        except Exception as e:
            pass
示例#2
0
def load_puzzles():
    """Loads in the JSON files listed in the global PUZZLE_PATHS variable to Puzzle models
    :return: A list of Puzzle objects
    """
    puzzles = []
    for path in PUZZLE_PATHS:
        with open(path, 'r') as f:
            puzzle_dict = json.load(f)
            new_puzzle = Puzzle()
            new_puzzle.input_anagrams = puzzle_dict['input_anagrams']
            new_puzzle.sentence_indices = puzzle_dict['sentence_indices']
            new_puzzle.sentence_word_lengths = puzzle_dict[
                'sentence_word_lengths']
            puzzles.append(new_puzzle)
    return puzzles
示例#3
0
def build_puzzle(question, answer, difficulty):
    question = question.upper()
    answer = answer.upper()
    difficulty = int(difficulty)
    print('Cleaning up', file=sys.stderr)
    regex = re.compile('[^a-zA-Z]')
    answer_root = regex.sub('', answer).upper()
    answer_letter_count = len(answer_root)

    print('Preparing', file=sys.stderr)
    min_key_count = 1
    max_key_count = 3

    min_word_length = ceil(4 * (1 + difficulty * 5 / 10))
    max_word_length = min_word_length + ceil(2 * (1 + difficulty * 5 / 10))

    puzzle_words = {'Word': [], 'Keys': [], 'Length': []}
    keys_list = list(answer_root.upper())
    print('making wordlist', file=sys.stderr)
    while keys_list:
        print(len(keys_list), 'remaining:', keys_list)
        key_length = randint(min_key_count, min(len(keys_list), max_key_count))
        word_keys, keys_list = get_random_keys(keys_list, key_length)
        word_length = randint(min_word_length, max_word_length)
        print(word_keys, word_length, keys_list, file=sys.stderr)
        possible_word = get_word_suggestion(word_keys, word_length)
        print(possible_word, file=sys.stderr)
        if not possible_word or possible_word.name in puzzle_words['Word']:
            print('redoing', file=sys.stderr)
            keys_list = word_keys + keys_list
        else:
            puzzle_words['Word'].append(possible_word.name)
            puzzle_words['Keys'].append(word_keys)
            puzzle_words['Length'].append(possible_word.length)
    pw = ','.join(puzzle_words['Word'])
    kw = ';'.join([','.join(k) for k in puzzle_words['Keys']])
    new_puzzle = Puzzle(question=question, answer=answer, words=pw, letter_keys=kw)
    db.session.add(new_puzzle)
    db.session.commit()
    return 'Created Puzzle', new_puzzle.id
示例#4
0
def create_puzzle():
    # Get fields from Form Data
    name_of_puzzle = request.form["name_of_puzzle"]
    picture_of_puzzle = request.files["picture_of_puzzle"]
    picture_of_box = request.files["picture_of_box"]
    number_of_pieces = request.form["number_of_pieces"]
    age_range = request.form["age_range"]
    category = request.form["category"]
    owner_id = request.form["owner_id"]

    if picture_of_box is not None:
        # upload box to cloudinary
        box_upload_result = cloudinary.uploader.upload(picture_of_box)
    else:
        return jsonify("Failed to upload picture of box"), 500

    if picture_of_puzzle is not None:
        # upload puzzle to cloudinary
        puzzle_upload_result = cloudinary.uploader.upload(picture_of_puzzle)
    else:
        return jsonify("Failed to upload picture of puzzle"), 500

    # add to db

    request_body_puzzle = request.get_json()

    newpuzzle = Puzzle(name_of_puzzle=name_of_puzzle,
                       picture_of_puzzle=puzzle_upload_result['secure_url'],
                       picture_of_box=box_upload_result['secure_url'],
                       number_of_pieces=number_of_pieces,
                       age_range=age_range,
                       category=category,
                       owner_id=owner_id,
                       is_available=True)
    db.session.add(newpuzzle)
    db.session.commit()

    return jsonify("successfully added puzzle"), 200