Last active
January 6, 2019 10:33
-
-
Save kohnakagawa/887812fee35da5cd17ec339dd75d3716 to your computer and use it in GitHub Desktop.
cfp timesの新着情報を取得して投稿するためのスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import requests | |
import sqlite3 | |
import json | |
import slackweb | |
import datetime | |
import sys | |
import os | |
import configparser | |
import argparse | |
from contextlib import closing | |
CFPTIME_ROOT_URL = "https://www.cfptime.org/api/cfps/" | |
DBNAME = "conflist.sqlite3" | |
def norm_datetime(tstr): | |
d = datetime.datetime.strptime(tstr, "%Y-%m-%dT%H:%M:%SZ") | |
return d.strftime("%Y-%m-%d %H:%M:%S") | |
def make_attachments(json_data, message): | |
return [ | |
{ | |
"color": "#36a64f", | |
"pretext": message, | |
"title": item["name"], | |
"title_link": item["website"], | |
"text": "CFP Deadline: {}\nConference Start: {}".format(norm_datetime(item["cfp_deadline"]), | |
norm_datetime(item["conf_start_date"])), | |
"fields": [ | |
{ | |
"title": "CFP details", | |
"value": item["cfp_details"], | |
"short": "true" | |
} | |
] | |
} for item in json_data] | |
def make_database(): | |
with closing(sqlite3.connect(DBNAME)) as conn: | |
c = conn.cursor() | |
create_table = ''' create table conferences (id int, name text, website text, | |
cfp_deadline text, conf_start_date text, cfp_details text)''' | |
c.execute(create_table) | |
conn.commit() | |
def make_conf_dict(query_result): | |
return { | |
"id": query_result[0], | |
"name": query_result[1], | |
"website": query_result[2], | |
"cfp_deadline": query_result[3], | |
"conf_start_date": query_result[4], | |
"cfp_details": query_result[5] | |
} | |
def search_close_deaeline_confs(cur_time): | |
beg = cur_time + datetime.timedelta(weeks=2) | |
end = cur_time + datetime.timedelta(weeks=6) | |
with closing(sqlite3.connect(DBNAME)) as conn: | |
c = conn.cursor() | |
select_confs = '''select id, name, website, cfp_deadline, conf_start_date, cfp_details | |
from conferences where cfp_deadline between '%s' and '%s' | |
''' % (beg.strftime("%Y-%m-%d %H:%M:%S"), end.strftime("%Y-%m-%d %H:%M:%S")) | |
c.execute(select_confs) | |
result = c.fetchall() | |
confs = [make_conf_dict(item) for item in result] | |
return confs | |
def append_cfpinfo(json_data): | |
non_recorded_confs = [] | |
with closing(sqlite3.connect(DBNAME)) as conn: | |
c = conn.cursor() | |
for item in json_data: | |
c.execute("select id from conferences where id=?", (item["id"],)) | |
result = c.fetchone() | |
if not result: | |
c.execute("insert into conferences values (?, ?, ?, ?, ?, ?)", | |
(item["id"], item["name"], item["website"], item["cfp_deadline"], | |
item["conf_start_date"], item["cfp_details"])) | |
non_recorded_confs.append(item) | |
conn.commit() | |
return non_recorded_confs | |
def fetch_new_records(): | |
params = {'country': "JP"} | |
headers = {"accept": "application/json"} | |
response = requests.get(CFPTIME_ROOT_URL, params=params, headers=headers) | |
if response.status_code != requests.codes.ok: | |
slack.notify(text="Something went wrong in {}".format(sys.argv[0])) | |
sys.exit(1) | |
res_json = response.json() | |
return res_json | |
def load_params(): | |
parser = configparser.ConfigParser() | |
parser.read("./config.ini", "UTF-8") | |
slack_url = parser.get("settings", "slack_url") | |
slack_url = slack_url.replace('"', '') | |
return {"slack_url": slack_url} | |
def _main(): | |
params = _load_params() | |
slack_url = params["slack_url"] | |
slack = slackweb.Slack(url=slack_url) | |
if not os.path.exists(DBNAME): | |
make_database() | |
parser = argparse.ArgumentParser(description="") | |
parser.add_argument("-f", "--fetch", action="store_true", help="fetch data from cfptime.org") | |
parser.add_argument("-r", "--remind", action="store_true", help="remind cfp deadline") | |
args = parser.parse_args() | |
if args.fetch: | |
res_json = fetch_new_records() | |
non_recorded_confs = append_cfpinfo(res_json) | |
if non_recorded_confs: | |
attachments = make_attachments(non_recorded_confs, "New CFP information") | |
slack.notify(attachments=attachments) | |
if args.remind: | |
cur_time = datetime.datetime.now() | |
confs = search_close_deaeline_confs(cur_time) | |
if confs: | |
attachments = make_attachments(confs, "CFP reminder") | |
slack.notify(attachments=attachments) | |
if __name__ == "__main__": | |
_main() |
アラート出す機能も追加
# 最新情報をfetchし、新規に追加されたCFP情報をSlackに通知
$ python3 notify-cfp-info.py -f
# デッドラインが近づいているCFPの情報を表示
$ python3 notify-cfp-info.py -r
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
締切1週間前のカンファレンスがあった場合にアラートを出す機能とかつけたい