Created
January 12, 2019 06:06
-
-
Save kohnakagawa/463e0336c5bdf2941a37232ba394dea0 to your computer and use it in GitHub Desktop.
annict graphql をつかって、今期アニメ視聴者数Top10をとってきてSlackに通知するためのスクリプト
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/python3 | |
import requests | |
import slackweb | |
import datetime | |
import sys | |
import configparser | |
def month_to_season(month): | |
if 1 <= month < 4: | |
return "winter" | |
elif 4 <= month < 7: | |
return "spring" | |
elif 7 <= month < 10: | |
return "summer" | |
else: | |
return "autumn" | |
def get_current_qr(): | |
current_time = datetime.datetime.now() | |
year = str(current_time.year) | |
month = current_time.month | |
return "-".join([year, month_to_season(month)]) | |
GRAPH_QL_QUERY = """ | |
query { | |
searchWorks( | |
seasons: ["%s"], | |
orderBy: { field: WATCHERS_COUNT, direction: DESC}, | |
first: 10 | |
) { | |
edges { | |
node { | |
title | |
officialSiteUrl | |
watchersCount | |
reviewsCount | |
image { | |
recommendedImageUrl | |
} | |
reviews( | |
first: 2 | |
orderBy: {field: LIKES_COUNT, direction: DESC}, | |
) { | |
edges { | |
node { | |
body | |
impressionsCount | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
""" % get_current_qr() | |
QUERY = { "query": GRAPH_QL_QUERY } | |
def load_params(fname): | |
parser = configparser.ConfigParser() | |
parser.read(fname, "UTF-8") | |
return { | |
"slack_url": parser.get("settings", "slack_url").replace('"', ''), | |
"annict_url": parser.get("settings", "annict_url").replace('"', ''), | |
"annict_key": parser.get("settings", "annict_key").replace('"', '') | |
} | |
def make_attachments(json_data_raw): | |
return [ | |
{ | |
"color": "#36a64f", | |
"pretext": "視聴者数 第{}位".format(idx + 1), | |
"title": item["node"]["title"], | |
"title_link": item["node"]["officialSiteUrl"], | |
"image_url": item["node"]["image"]["recommendedImageUrl"], | |
"thumb_url": item["node"]["image"]["recommendedImageUrl"], | |
"text": "Annict 視聴者数: {}, レビュワー数: {}".format(item["node"]["watchersCount"], item["node"]["reviewsCount"]), | |
"fields": [ | |
{ | |
"title": "コメント (影響度: {})".format(comment["node"]["impressionsCount"]), | |
"value": comment["node"]["body"], | |
"short": "true" | |
} for comment in item["node"]["reviews"]["edges"] | |
] | |
} for idx, item in enumerate(json_data_raw["searchWorks"]["edges"]) | |
] | |
def main(): | |
params = load_params("./config.ini") | |
slack = slackweb.Slack(url=params["slack_url"]) | |
qb_req = requests.post( | |
params["annict_url"], | |
headers={ | |
"Accept": "application/json", | |
"Authorization": "Bearer {}".format(params["annict_key"]) | |
}, | |
data=QUERY | |
) | |
if qb_req.status_code != requests.codes.ok: | |
slack.notify(text="Something went wrong in {}".format(sys.argv[0])) | |
sys.exit(1) | |
body_json = qb_req.json() | |
json_raw = body_json["data"] | |
attachments = make_attachments(json_raw) | |
slack.notify(attachments=attachments) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment