-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfra-workshop-twitter-bot.py
More file actions
92 lines (76 loc) · 3.63 KB
/
infra-workshop-twitter-bot.py
File metadata and controls
92 lines (76 loc) · 3.63 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
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
#!/usr/bin/python3
# vim: set fileencoding=utf-8 :
# require `export PYTHONIOENCODING=utf-8`
import os
import requests
import json
from datetime import datetime, timedelta, timezone
from subprocess import call
import post_twitter
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DEBUG = True
def log(msg):
if DEBUG:
f = open(BASE_DIR + "/run.log","a")
f.write(str(msg) + "\n")
f.close()
JST = timezone(timedelta(hours=+9), 'JST')
POST_SUMMERY_TIME = datetime.now(JST).replace(hour=7, minute=0, second=0, microsecond=0)
def post_reservation_by_at(message,time="now"):
command = "/bin/echo python3 \"" + BASE_DIR + "/post_twitter.py \'" + message + "\'\" | /usr/bin/at " + time
command = command.replace('\n','\\\\n')
log("----")
log(command)
ret = call(command, shell=True)
log(ret)
return ret
def main():
log("main start")
sdt = datetime.now(JST)
sdt = sdt.replace(hour=0, minute=0, second=0, microsecond=0)
sdt = sdt.strftime('%Y/%m/%dT%H:%MZ')
edt = (datetime.now(JST) + timedelta(days=1))
edt = edt.replace(hour=23, minute=59, second=59, microsecond=0)
edt = edt.strftime('%Y/%m/%dT%H:%MZ')
API_URI = 'https://wp.infra-workshop.tech/?rest_route=/tribe/events/v1/events'
url = API_URI + "&start_date=" + sdt + "&end_date=" + edt
log(url)
response = requests.get(url)
if response.status_code != 200:
log("error : " + str(response.status_code))
return
wss = json.loads(response.text)
next_post = datetime.now(JST).replace(hour=21, minute=0, second=0, microsecond=0)
if not "events" in wss:
log('No events found.')
for event in wss["events"]:
title = event["title"]
url = event["url"]
esd = event["start_date_details"]
eed = event["end_date_details"]
day = esd["year"] + "/" + esd["month"] + "/" + esd["day"]
day_s = "(" + esd["month"] + "/" + esd["day"] + ")"
start = esd["hour"] + ":" + esd["minutes"]
end = eed["hour"] + ":" + eed["minutes"]
log(start + "-" + end + "\n" + title)
if day == datetime.now(JST).date().strftime('%Y/%m/%d'):
end_datetime = datetime(tzinfo=JST, year=int(eed["year"]),month=int(eed["month"]),day=int(eed["day"]) ,hour=int(eed["hour"]), minute=int(eed["minutes"]),)
if next_post < end_datetime:
next_post = end_datetime
log("Today Workshop")
start_datetime = datetime(tzinfo=JST, year=int(esd["year"]),month=int(esd["month"]),day=int(esd["day"]) ,hour=int(esd["hour"]), minute=int(esd["minutes"]),)
if start_datetime > POST_SUMMERY_TIME:
word = "今日" + day_s + "の #インフラ勉強会 は...\n" + title + "\n" + start + " - " + end + "\n" + url
post_reservation_by_at(word, POST_SUMMERY_TIME.strftime("%H%M"))
word = "今日" + day_s + "の勉強会がそろそろ始まるよ!\n" + title + "\n" + start + " - " + end + "\n" + url + "\n#インフラ勉強会"
post_reservation_by_at(word, start + " - 30min")
word = "勉強会が始まるよ! 本日のお題は...\n" + title + "\n" + url + "\n#インフラ勉強会"
post_reservation_by_at(word, start)
if day == (datetime.now(JST) + timedelta(days=1)).date().strftime('%Y/%m/%d'):
log("Next Workshop")
next_post_time = next_post.strftime('%H:%M')
word = "#インフラ勉強会 、次回" + day_s + "は...\n" + title + "\n" + start + " - " + end + "\n" + url
post_reservation_by_at(word, next_post_time)
log("end main")
if __name__ == '__main__':
main()