5
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.

web漫画の最新話が投稿されるとLINEに通知する【python3】

Posted at

はじめに

web漫画は最新話の投稿日が不定期なため、いつ投稿されるか定期的に確認しなければならない。
面倒なので、最新話がアップされたらLINEに通知するプログラムを作成しました。

環境

OS:macOS Mojave 10.14
言語:Python3.5.1

手順

1.プログラムからLINEに通知
2.スクレピング
3.定期実行

1.プログラムからLINEに通知

まず、こちらのサイトからLINENotifyのアクセストークンを発行します。
発行したアクセストークンをコピーして以下のコードを追加します。

lineNotify.py
import requests
from config import config
# ! python3
# -*- coding: utf-8 -*-

# LINEに更新を通知する
def lineNotify(text):
    line_notify_token = 'アクセストークン'
    line_notify_api = 'https://notify-api.line.me/api/notify'
    payload = {'message': text}
    headers = {'Authorization': 'Bearer ' + line_notify_token}
    requests.post(line_notify_api, data=payload, headers=headers)

lineNotify('text')

pythonでのLINEの通知はこちらが参考になりました。

2.スクレイピング

今回はワンパンマンのサイトをスクレイピングします。
まず、抽出する情報を見てみましょう。

抽出する情報

chromeを利用されている方ならデベロッパーツールを利用してリソースをみてください。
すると、aタグに最新話のurlが埋め込まれているのがわかります。
トップページには3話分掲載されているので、下記のようなurlが3個あるかと思います。
https://tonarinoyj.jp/episode/10834108156636232181

プログラム

今回、プログラムの作成はこちらの記事が大変参考になりました。
この記事同様に、プログラムの流れは、
1.最新話のurl取得
2.最新話かどうか判定
3.情報をLINEに通知する

最新話が掲載されている場合、下記のように通知

[139話]ワンパンマン
https://tonarinoyj.jp/episode/10834108156636232181

最新話が掲載されていない場合、下記のように通知

最新話はアップされていません。
https://tonarinoyj.jp/episode/10834108156636232181

スクレイピングプログラム

extract_web.py
# ! python3
# -*- coding: utf-8 -*-

# web漫画の最新話がアップされると通知するシステム

from bs4 import BeautifulSoup
import urllib.request as url_req
from config import config, update_recent_article
from line_notification import lineNotify

first_view = url_req.urlopen(config['web_info']['url']).read()
soup = BeautifulSoup(first_view, "lxml")


def extract_pick_up(soup=soup):
    """
    htmlを詠み込み、最新記事を抜粋
    """
    episodes = soup.find_all("ul", class_="test-readable_product-list series-episode-list ")
    return episodes[0:4]

def extract_url(column):
    """
    エピソードのurlを取得
    """
    episode_html = BeautifulSoup(str(column), "lxml")
    url = episode_html.find("a").get("href")
    return url


def extract_title(column):
    """
    エピソードのタイトルを取得する
    """
    episode_html = BeautifulSoup(str(column), "lxml")
    title = episode_html.find("h4").string
    return title

def extract_update_article(columns):
    """
    更新された記事を抽出
    更新前の最新の記事のindexはconfig.iniで管理
    """
    # 最新の記事番号を取得
    recent_article = config['web_info']['recent_article']
    # 更新された記事のurlを取得
    article_list = []
    update_start = False

    # 最新話の話のurlを取得
    url = extract_url(columns)
    # 記事番号取得
    article_num = url.replace('https://tonarinoyj.jp/episode/', "")

    # タイトル取得
    title = extract_title(columns)

    # 前回取得した最新話かどうか判定
    if not recent_article == article_num:
        article_list.append([title, url])
        recent_article = article_num

    # config更新
    update_recent_article(recent_article)
    if article_list == []:
        return [["最新話はアップされていません", "https://tonarinoyj.jp/episode/13932016480028985383"]]
    return article_list



if __name__ == "__main__":
    episodes = extract_pick_up()
    url = extract_url(episodes[0])
    article_list = extract_update_article(episodes)
    lineNotify(article_list[0])

このプログラムを実行することで、ワンパンマンの最新話が掲載されたかどうかを
LINEに通知します。

LINE通知プログラム

line_notification.py
import requests
from config import config
# ! python3
# -*- coding: utf-8 -*-

# LINEに更新を通知する
def lineNotify(article_list):
    line_notify_token = config['line_info']['line_notify_token']
    line_notify_api = config['line_info']['line_notify_api']
    payload = {'message': article_list}
    headers = {'Authorization': 'Bearer ' + line_notify_token}
    requests.post(line_notify_api, data=payload, headers=headers)

1.で述べたプログラムに少し改良を加えています。
トークンなどは、設定のiniファイルで管理しています。

設定プログラム

config.py
import configparser
import re

config = configparser.ConfigParser()
config.read('config.ini')

# recent_articleの記事番号を更新
def update_recent_article(article_num):
    with open('config.ini', 'r') as f:
        lines = f.readlines()
    with open('config.ini', 'w') as f:
        for line in lines:
            if re.match(r'(recent_article = )', line):
                f.write("recent_article = {}".format(article_num))
                continue
            f.write(line)
config.ini
[line_info]
line_notify_token = 'アクセストークン'
line_notify_api = https://notify-api.line.me/api/notify

[web_info]
url = https://tonarinoyj.jp/episode/13932016480028985383
# 前回取得した番号
recent_article = 10834108156636232181

extract_web.pyで最新話と過去の話のurlを比較した際、
差分があった場合、過去のurlの番号を最新話の番号に上書きします。

3.定期実行

定期実行はcronを利用するとすぐできます。
詳しくはこちらの記事を参考にしてください。

参考

5
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
5
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?