13
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

超A&G+の番組表をapi化する(配信編)

Last updated at Posted at 2019-09-03

前回 → https://qiita.com/taittide/items/7d512492179b2b520c4b

正直前回のjsonを渡すだけなので,簡単ではある。

こちらGitHub → https://github.com/sun-yryr/agqr-program-guide


サブドメインを作ってssl証明書を出す

certbot使ってワイルドカード証明書作ってたら褒められた。やったね!

nginx からプロキシを通す

いつもどおり

location / {
	    proxy_set_header Host                      $host;
        proxy_set_header X-Real-IP           $remote_addr;
        proxy_set_header X-Forwarded-Host    $host;
        proxy_set_header X-Forwarded-Server  $host;
        proxy_set_header X-Forwarded-For     $proxy_add_x_forwarded_for;
        proxy_pass http://localhost:1234;
}

###APIサーバーを立てる

「いつもexpressだけど,pythonだしpythonで簡単にしたいなぁ」

こちら神 → https://qiita.com/Yaruki00/items/efe23a0e91579aec9398

まずインストール

pip3 install falcon
pip3 install gunicorn

ルーティングはall, today, nowの3つなので簡単に3リソース作る。

import falcon
import json
import datetime

all_json = []
not_repeat_json = []

class AgqrAll:
    def on_get(self, req, resp):
        params = req.params
        isRepeat = params.get("isRepeat")
        if isRepeat is None:
            resp.body = json.dumps(not_repeat_json, ensure_ascii=False)
        elif (isRepeat == "True") or (isRepeat == "true"):
            resp.body = json.dumps(all_json, ensure_ascii=False)
        else:
            resp.body = json.dumps(not_repeat_json, ensure_ascii=False)

class AgqrToday:
    def on_get(self, req, resp):
        week = datetime.date.today().weekday()
        params = req.params
        isRepeat = params.get("isRepeat")
        if isRepeat is None:
            resp.body = json.dumps(not_repeat_json[week], ensure_ascii=False)
        elif (isRepeat == "True") or (isRepeat == "true"):
            resp.body = json.dumps(all_json[week], ensure_ascii=False)
        else:
            resp.body = json.dumps(not_repeat_json[week], ensure_ascii=False)

class AgqrNow:
    def on_get(self, req, resp):
        now = datetime.datetime.now()
        week = now.weekday()
        res = all_json[week]
        for i in range(len(res)):
            prog = res[i]
            tmp_dt = datetime.datetime.strptime(prog["to"], "%Y%m%d%H%M")
            if now < tmp_dt:
                resp.body = json.dumps(prog, ensure_ascii=False)
                break

nowだけ毎回の計算量が多くなってしまっているのでなんとかしたいなぁとも思う。(自分は使わないのでまぁ……)

バックグラウンドで動かす

nohup python3 server.py &

見てみる

終わりに

  • ワイルドカード証明書の自動renewやらないとな
  • safariの文字コード自動認識に苦しめられた
  • これでラジオ自動録音を改良できる。
  • トップページにapi利用のなんか書いておこう
13
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?