-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctftime_script.py
82 lines (69 loc) · 2.96 KB
/
ctftime_script.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
import requests,json, re
from datetime import datetime,timezone,timedelta
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.cron import CronTrigger
from dotenv import load_dotenv
import os
load_dotenv()
interval_days=8
def filter_chars(text):
input=str(text)
special_chars="',!/()[+].{}:-_=>—@$£€₹¥’"
return "".join([c for c in input if(c.isprintable() and (c.isalnum() or c.isspace() or (c in special_chars)))])
# return "".join([c for c in input if(c.isalnum() or c.isspace() or (c in special_chars))])
def get_msg(ctf,i,starttime):
return f"""
{filter_chars(i)}. **[{filter_chars(ctf['title'])}]({filter_chars(ctf['url'])})**
- **Format:** {filter_chars(ctf['format'])} , **Weight:** {filter_chars(ctf['weight'])}
- **Start:** {filter_chars(starttime)}
- **Prizes: check on [CTFtime]({filter_chars(ctf['ctftime_url'])})**
- **Description:** {filter_chars(ctf['description'][:100])}..."""
def get_embed(ctf,i,starttime):
return {
# 'title': f"""**[{filter_chars(ctf['title'])}]({filter_chars(ctf['url'])})**""",
'title': filter_chars(ctf['title']),
'url': filter_chars(ctf['url']),
'description': f"""
- **Format:** {filter_chars(ctf['format'])} , **Weight:** {filter_chars(ctf['weight'])}
- **Start:** {filter_chars(starttime)}
- **Prizes: check on [CTFtime]({filter_chars(ctf['ctftime_url'])})**
- **Description:** {filter_chars(ctf['description'][:100])}..."""
,'thumbnail': {
'url': ctf['logo'],
}}
def ctftime_job():
url=f'https://ctftime.org/api/v1/events/?limit={interval_days}' # 1 CTF daily max
data=requests.get(url,headers={'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'})
ctfs=json.loads(data.text)
embeds=[]
i=1
ctf_events_message = f"""**Upcoming CTFs(weekly):**"""
for ctf in ctfs:
start=datetime.fromisoformat(ctf["start"])
if(ctf["onsite"]==False and start-datetime.now(timezone.utc)<timedelta(days=interval_days)):
#and ctf['restrictions']=='Open'/'Academic'
start+=timedelta(hours=+5.5) # IST
starttime=start.strftime("%a %d %b %I:%M %p IST")
msg=get_msg(ctf,i,starttime)
embeds.append(get_embed(ctf,i,starttime))
ctf_events_message+=msg
i+=1
webhook_url = os.getenv("WEBHOOK_URL")
headers = {'Content-Type': 'application/json'}
webhook_data = {
'username': 'CTFtime bot',
# 'content': ctf_events_message,
'content': '**Upcoming CTFs(weekly):**',
'embeds': embeds
}
response = requests.post(webhook_url, data=json.dumps(webhook_data), headers=headers)
# print(ctf_events_message)
# print(ctfs)
# Just use cronjob or anacron [persists over reboots]
ctftime_job()
# When not using cronjob
# scheduler = BlockingScheduler()
## every Friday at 7 PM
# scheduler.add_job(ctftime_job, trigger=CronTrigger(hour=19, day_of_week=4)) # 6=Sunday
# scheduler.add_job(ctftime_job, trigger=CronTrigger(hour=17, minute=38, day_of_week=6))
# scheduler.start()