-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
177 lines (146 loc) · 6.34 KB
/
bot.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# coding: utf8
import discord
import logging
import asyncio
import re
import os
import pickle
import datetime as dt
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s | %(name)s | %(levelname)s | %(message)s'
)
from PIL import Image
from typing import Optional, List
from my_constants import TOKEN, IMG_FOLDER, channel_horoscope
from rtl2_horoscope.scraper.facebook import FacebookScraper
from rtl2_horoscope.parse import parse_horoscope, reformat_horoscope
from rtl2_horoscope.utils import now
#import nest_asyncio
#nest_asyncio.apply()
manual = """
```Help
<@{id}> test -- Récupère la dernière photo de RTL2 (horoscope ou pas)
<@{id}> last -- Donne le dernier horoscope de RTL2
<@{id}> download <URL> -- Télécharge l'image via l'URL donnée en argument et vérifie s'il s'agit de l'horoscope de RTL2
```
"""
TIMESTAMP_FORMAT = "%Y-%m-%d"
class HoroscopeDiscordBot(discord.Client):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.scraper = FacebookScraper()
async def setup_hook(self):
# create the background task and run it in the background
self.loop.create_task(self.job())
async def on_ready(self):
"""Initial check"""
if not os.path.isdir(IMG_FOLDER):
logging.info(f"Création du dossier {IMG_FOLDER}")
os.mkdir(IMG_FOLDER)
logging.info("Bot ready :-)")
logging.info('Logged in as')
logging.info(self.user.name)
logging.info(self.user.id)
logging.info('------')
def is_for_bot(self, message) -> bool:
"""Check if the message is for the bot.
Args:
message: Discord message
"""
return re.match(f"^<@!?{self.user.id}>", message.content)
def command(self, message, cmd: str) -> str:
"""Check if the command match the one found in message.
Args:
message: Discord message
cmd: command people sent
"""
return re.match(f"^<@!?{self.user.id}> {cmd}", message.content)
async def job(self, fetch_interval=300, days=[0,1,2,3,4], hours=[9,10,11,12]):
""" Job to run evey `fetch_interval` seconds,
each day in days, between hours
Args:
fetch_interval (int) : run job every `fetch_interval` seconds
days (list of int): day to fetch horoscope
hours (list of hour) : (whole) hour to fetch horoscope
Example : job(300, [0,1,3,4], [7,8,10,12,13]):
Run the job on Monday, Tuesday, Thursday and Friday, between 7 and 8 hours
and between 10 and 13 hours.
"""
await self.wait_until_ready()
assert max(days) <= 6, "Need number between 0 and 6"
assert min(days) >= 0, "Need number between 0 and 6"
assert max(hours) <= 23, "Need number between 0 and 23"
assert min(hours) >= 0, "Need number between 0 and 23"
while not self.is_closed():
today = now()
while today.weekday() in days and today.hour in hours \
and not await self.fetch_new_horoscope():
# while (it's time to fetch horoscope) AND (the horoscope has not been published yet)
# wait fetch_interval to not spam Twitter
await asyncio.sleep(fetch_interval)
time_to_wait = self.get_time_to_wait(hours).total_seconds()
time_to_wait_message = f"Reprise de l'activité dans {time_to_wait} secondes."
logging.info(time_to_wait_message)
await asyncio.sleep(time_to_wait)
async def on_message(self, message):
"""Handle messages
See help for features.
"""
if message.author == client.user:
return
if self.command(message, "help"):
await self.get_channel(channel_horoscope).send(manual.format(id=self.user.id))
if self.command(message, "download"):
img_href = message.content.split(" ")[-1]
if img_href.startswith("http") and await self.fetch_new_horoscope(img_href=img_href):
time_to_wait = self.get_time_to_wait([10,11,12]).total_seconds()
time_to_wait_message = f"Reprise de l'activité dans {time_to_wait} secondes."
logging.info(time_to_wait_message)
await asyncio.sleep(time_to_wait)
if self.command(message, "last"):
files = sorted(os.listdir(IMG_FOLDER), reverse=True)
if len(files) == 0:
await self.get_channel(channel_horoscope).send("Aucun horoscope en stock :-(")
return
horoscope_img = IMG_FOLDER + "/" + files[0]
await self.parse_and_send_horoscope(horoscope_img)
async def parse_and_send_horoscope(self, filename):
"""Parse the image and send the image and the text found through OCR"""
logging.info("OCR : en cours.")
horoscope_dict = parse_horoscope(filename, threads=1)
horoscope_str = reformat_horoscope(horoscope_dict)
logging.info("OCR : terminé.")
await self.get_channel(channel_horoscope).send(file=discord.File(filename))
await self.get_channel(channel_horoscope).send(horoscope_str)
async def fetch_new_horoscope(self, img_href: Optional[str] = None) -> bool:
"""Get last image from RTL2 social media page, check if it's a new horoscope
and send the file on Discord
Args:
img_href : if not None, download the image from <img_href> url
"""
horoscope = await self.scraper.fetch_new_horoscope(img_href)
if horoscope:
await self.parse_and_send_horoscope(horoscope)
return True
return False
def get_time_to_wait(self, hours):
"""How many time to wait before checking
for a new horoscope ?
"""
today = now()
# Wait until tomorrow
days_to_wait = 1
if today.weekday() == 4:
# it's Friday -> wait two more days
days_to_wait += 2
if today.weekday() == 5:
# it's Saturday -> wait one more day
days_to_wait += 1
next_day = today.replace(hour=hours[0],minute=0,second=0,microsecond=0) + dt.timedelta(days=days_to_wait)
return next_day-today
if __name__ == "__main__":
intents = discord.Intents.default()
intents.message_content = True
client = HoroscopeDiscordBot(intents=intents)
client.run(TOKEN)