def post(self):
     url = self.get_argument('url')
     # Fetch word dictionary with frequency
     word_dict = build_word_dict(url)
     # Get public key
     public_key = get_keys("public_key")
     new_dict_array = []
     with self.make_session() as session:
         # Iterate through all the word in the word_dict , create the word hash and
         # check if a word hash exists in the database
         for word, quantity in word_dict:
             new_dict = {'text': word, 'size': quantity}
             new_dict_array.append(new_dict)
             salted_hash = get_salted_hash(word)
             word_element = yield as_future(
                 session.query(Word).filter(
                     Word.word_hash == salted_hash).first)
             if not word_element:
                 # Create a word object and add to session
                 encrypted_word = encrypt_data(word, public_key)
                 session.add(
                     Word(word_hash=salted_hash,
                          word=encrypted_word,
                          count=quantity))
             else:
                 # update the word object with the frequency
                 word_element.count += quantity
             session.commit()
     self.render("base.html", word_array=json_encode(new_dict_array))
def create_word(text: str):
    """
    Creates a new word in the word database if it doesn't exist yet (case insensitive)

    :param text: The text for the new word
    :returns: The created word if successful
    """
    lower_text = text.lower()
    existing_word = Word.query.filter(
        func.lower(Word.text) == lower_text).first()

    if existing_word is not None:
        return existing_word

    word = Word(text=text)
    db.session.add(word)

    db.session.commit()
    return word
Exemple #3
0
db_songs = {}
for song in storage.all(Song).values():
    db_songs[song.title] = song
if db_songs.get(input_song) is None:
    input_genre = input('Genre: ')
    image = input('image_url: ')
    lyrics = input('lyrics: ')
    song = Song()
    song.artist = input_artist
    song.title = input_song
    song.lyrics = lyrics
    song.genre = input_genre
    song.image_url = image
else:
    song = db_songs[input_song]
words = input('words: ')
word_list = map(str, words.strip('[]').split(','))
db_words = {}
for word in storage.all(Word).values():
    db_words[word.text] = word
for item in word_list:
    if db_words.get(item) is None:
        word = Word()
        word.text = item
        word.save()
        song.words.append(word)
    else:
        song.words.append(db_words[item])
models.storage.new(song)
song.save()
Exemple #4
0
 def create_word(self, word):
     self.create_word_dict(word)
     print("--Instantiating new word--")
     self.word = Word(self.word_dict)