This commit is contained in:
Administrator
2022-10-03 16:32:43 +03:00
commit 149c251bfa
15 changed files with 315 additions and 0 deletions

0
helpers/__init__.py Normal file
View File

31
helpers/jokes.py Normal file
View File

@@ -0,0 +1,31 @@
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:
print(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 poll_jokes():
while True:
print("start fetching jokes")
fetch_jokes()
print("finished fetching jokes")
sleep(60 * 60 * 24)

22
helpers/mongo.py Normal file
View File

@@ -0,0 +1,22 @@
from functools import cached_property
import pymongo
import settings
class Mongo:
def __init__(self):
url = f"mongodb://{settings.MONGO_USER}:{settings.MONGO_PASSWORD}@{settings.MONGO_HOST}:27017/"
self.client = pymongo.MongoClient(url)
self.database = self.client.get_database("b-jokes")
def __getitem__(self, item):
return self.database.get_collection(item)
@cached_property
def jokes_collection(self):
return self["jokes"]
mongo = Mongo()