41 lines
1.1 KiB
Python
41 lines
1.1 KiB
Python
import logging
|
|
import random
|
|
from time import sleep
|
|
|
|
from requests import get
|
|
|
|
from helpers.mongo import mongo
|
|
|
|
|
|
def fetch_jokes():
|
|
i = 1
|
|
while True:
|
|
info = get(f"https://baneks.ru/{i}")
|
|
if info.status_code == 200:
|
|
logging.info(i)
|
|
content = info.text
|
|
anek = content.split("<p>")[1].split("</p>")[0].replace("<br />", "")
|
|
if '<a href="random">' in anek:
|
|
break
|
|
if mongo.jokes_collection.find_one({"id": i}) is None:
|
|
mongo.jokes_collection.insert_one({
|
|
"id": i,
|
|
"text": anek
|
|
})
|
|
i += 1
|
|
|
|
|
|
def get_random() -> str:
|
|
count_docs = mongo.jokes_collection.count_documents({})
|
|
rnd = random.randrange(1, count_docs + 1)
|
|
anek = mongo.jokes_collection.aggregate([{"$sample": {"size": 1}}]).next()
|
|
return anek['text']
|
|
|
|
|
|
def poll_jokes():
|
|
while True:
|
|
logging.info("start fetching jokes")
|
|
fetch_jokes()
|
|
logging.info("finished fetching jokes")
|
|
sleep(60 * 60 * 24)
|