-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmemebot.py
More file actions
49 lines (41 loc) · 1.3 KB
/
memebot.py
File metadata and controls
49 lines (41 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from os import listdir, remove
from time import sleep
from requests import RequestException
from modules.scraper import Scraper
from modules.telebot import TeleBot
from utils.constants import *
from utils.logger import log
# Spawn fake session
log.info("Loading...")
scr = Scraper()
log.info(f"Loaded {len(scr.read_ids)} IDs...")
# Initialize bot
bot = TeleBot()
# Clean cache
log.info("Removing old cache files...")
# Make sure not to remove ID cache
remove_files = [file for file in listdir(CACHE_PATH) if not file.endswith(".json")]
for file in remove_files:
remove(f"{CACHE_PATH}/{file}")
log.info(f"Removed {len(remove_files)} cached files...")
log.info("Starting bot...")
while True:
# Refresh IDs of people who want memes
try:
bot.update_users()
except RequestException:
log.error("HTTP request exception while updating users...")
sleep(15)
continue
# No need to spend processing power if no one wants our memes :(
if bot.users:
# Download the new posts from each sub
for sub in SUBS:
# Callback to the send function of the bot
scr.download_posts(
subreddit=sub,
img_cb=bot.send_photo,
vid_cb=bot.send_video,
)
# Wait till next iteration
sleep(QUERY_DELAY)