From e7eda34aa63f44dd7d3f616c72815fdb7af13387 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Wed, 23 Dec 2020 23:49:05 +0300 Subject: [PATCH] init --- LICENSE | 21 +++++++++++++++++ randomword_grabber.py | 55 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 LICENSE create mode 100644 randomword_grabber.py diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9ac059c --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Alexander Popov + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/randomword_grabber.py b/randomword_grabber.py new file mode 100644 index 0000000..69e39f4 --- /dev/null +++ b/randomword_grabber.py @@ -0,0 +1,55 @@ +from bs4 import BeautifulSoup +import requests +import json +from time import sleep + + +def load_words(): + with open('./words.json', encoding='utf-8') as file: + words = json.load(file, encoding='utf-8') + + return(words) + + +def parse(): + r = requests.get('https://randomword.com/') + data = r.text + soup = BeautifulSoup(data, 'html.parser') + word = soup.find('div', {'id': 'random_word'}).text + word_definition = soup.find('div', {'id': 'random_word_definition'}).text + + new_word = {'word': word, 'definition': + [word_definition.replace(' ', '')]} + + return(new_word) + + +def main(words_data): + while(True): + sleep(0.5) + new_word = parse() + + print(new_word) + + words_data[new_word['word']] = new_word['definition'] + + return(words_data) + +__author__ = 'Alexander Popov' +__license__ = "Public Domain" +__version__ = "1.0.0" +__email__ = "iiiypuk@fastmail.fm" + +if __name__ == '__main__': + words = {} + + words = load_words() + try: + while(True): + words = main(words) + except KeyboardInterrupt: + with open('./words.json', 'w', encoding='utf-8') as file: + file.write( + json.dumps(words, indent=4, ensure_ascii=False)) + + print('Saved %d words.' % len(words))