-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.py
59 lines (49 loc) · 1.73 KB
/
job.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
from api import api_instagram
from collections import OrderedDict
import sched
import time
from datetime import datetime
from api.api_instagram import insta_send
#
class CommunicationManager:
"""
Post format look like this:
{"event_title":
"datetime": (2023, 7, 4, 10, 30, 0)
"apis": [Instagram, ISEPLife]
"asset": "picture.jpg"
"desc": "Workshop à venir ! Abonnez vous !"
}
"""
def __init__(self):
self.schedule = OrderedDict()
def add(self, post):
"""Add a post in the schedule"""
self.schedule.update(post)
def remove(self, post_key):
"""Remove a post from the schedule"""
self.schedule.pop(post_key)
def run(self):
"""
Post all the posts in due time until the dict is empty. NEEDS TO BE REDONE USING ONLY SCHEDULE LIBRARY
:return: None
"""
while self.schedule:
post = dict(self.schedule[0])
datetime = post["datetime"]
while self.wait_until(datetime) != "Over":
continue
for api in post["apis"]:
if api == "Instagram":
insta_send(post["asset"], post["desc"])
self.schedule.pop(post)
def wait_until(target_datetime):
""" Wait for the due date to unlock the software. NEEDS TO BE REDONE USING ONLY SCHEDULE LIBRARY"""
scheduler = sched.scheduler(time.time, time.sleep)
current_datetime = datetime.now()
time_difference = (target_datetime - current_datetime).total_seconds()
if time_difference <= 0:
print("Time is up!")
return "Over"
scheduler.enter(time_difference, 1, lambda: print("Time is up!"))
scheduler.run()