Created
October 19, 2018 16:56
-
-
Save Proteusiq/c7fa31be6a2719800063bf48c67c03e0 to your computer and use it in GitHub Desktop.
thread_quoters.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from concurrent.futures import ThreadPoolExecutor | |
import random,time | |
from bs4 import BeautifulSoup as bs | |
import requests | |
URL = 'http://quotesondesign.com/wp-json/posts' | |
def quote_stream(sleep=5): | |
''' | |
Quoter streamer | |
''' | |
param = dict(page=random.randint(1, 1000)) | |
quo = requests.get(URL, params=param) | |
if quo.ok: | |
data = quo.json() | |
author = data[0]['title'].strip() | |
content = bs(data[0]['content'], 'html5lib').text.strip() | |
print(f'{content}\n-{author}\n') | |
time.sleep(sleep) | |
print(f'slept {sleep} seconds\n') | |
else: | |
print('Connection Issues :(') | |
def main(workers=3): | |
with ThreadPoolExecutor(max_workers=workers) as executor: | |
_ = [executor.submit(quote_stream) for i in range(workers)] | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment