Skip to content

Instantly share code, notes, and snippets.

@Proteusiq
Created October 19, 2018 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Proteusiq/c7fa31be6a2719800063bf48c67c03e0 to your computer and use it in GitHub Desktop.
Save Proteusiq/c7fa31be6a2719800063bf48c67c03e0 to your computer and use it in GitHub Desktop.
thread_quoters.py
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