I made a net news notification app with Python

I made an app that will notify you when a new article is posted on the target net news in the notification center of Mac.

Thing you want to do

① New articles are posted ② Notification center will notify you (the notification will disappear in a few seconds) ③ ー a Click the notification icon to open the corresponding article. ③ ー b Articles that did not open are stocked in the notification window even if the Mac does not notice the notification.

Tools that meet the above specifications are automatically executed when the OS starts. Also, even if the tool terminates abnormally due to a temporary communication failure, etc., it will be restarted.   名称未設定.png

Referenced materials

Creation procedure

Created in all 5 processes.

  1. Repair mdfmonitor ・ Repair location A. URL update detection process B. URL update history information extraction process

A. The URL update detection conditions have been changed as follows. ・ Existing When the header date and contents of the WEB page have changed since the last acquisition ·this time When only the article part of the WEB page has changed since the last acquisition

mdfmonitor.py


    def _check_modify(self, url, o_rbody):
        n_rbody = self._get_text(url)
        if n_rbody == o_rbody:
            return False
        else:
            return True

    def _get_text(self, url):
        try:
            l=[]
            title = urladrs = icon = []
            for line in requests.get(url).iter_lines():
                # get title
                if line.find("<h2>") >= 0:
                    title = re.sub(".*html\">", "", line)
                    title = re.sub("</a>.*", "", title)
                # get url
                if line.find("<h2>") >= 0:
                    urladrs = re.sub(".*href=\"", "", line)
                    urladrs = re.sub("\">.*", "", urladrs)
                # get icon
                if re.search("<img src=.* class=\"pict.*\"", line):
                    icon = re.sub("\s.*<a.*img src=\"", "", line)
                    icon = re.sub("\" width.*", "", icon)

                if title and urladrs and icon:
                    l.append([title, urladrs, icon])
                    title = urladrs = icon = []
            return l
        except requests.exceptions.ConnectionError:
            raise ConnectionError("Monitor can't connect the server of url you added.")

Since the new arrival detection logic depends on the WEB page to be detected, it is inevitably non-general purpose. If the number of detection targets increases, it is better to create an external file for the detection keywords.

B. The URL update history information extraction process has been changed as follows. ・ Existing URL update history information extraction process ·this time Only store the new article name, new article URL, and new article headline image acquired in A above in a multidimensional list.

mdfmoniter.py


    def _diffgen(self):
        l = []
        for new in self.new_rbody:
            if not new in self.old_rbody:
                l.append(new)
        return l
  1. Output processing of MAC notification center call command statement This is a python file that calls mdfmoniter.py in 1 above.

Execution of terminal-notifier After creating the command statement, I wanted to execute it as it is, but when I execute it by automatic startup, terminal-notifier terminates abnormally for some reason. Therefore, the command is not executed here, but is executed by cron.

newsmoniter.py


#!/usr/bin/python
#coding: utf-8

import os
from mdfmonitor import URLModificationMonitor

# create Watcher instnce
monitor = URLModificationMonitor()

# append file to mdfmonitor instance
monitor.add_url("http://blog.esuteru.com")

for mdf in monitor.monitor():
    for title, urladrs, icon in mdf.diff:
        cmd = "/usr/local/bin/terminal-notifier -title new! -message {0} -open {1} -sound Submarine -appIcon {2}".format(title, urladrs, icon)
        f2 = open("/usr/local/bin/news_cmd.sh", "w")
        f2.write(cmd)
        os.chmod("/usr/local/bin/news_cmd.sh", 0777)
        f2.close()

3.newsmoniter.py Life and death monitoring tool The above newsmoniter.py may terminate abnormally due to a temporary communication error, etc., and the process may disappear. This tool is a tool for detecting the disappearance and restarting it.

autorun.sh


#!/bin/sh

while true;
do
    ps -ef | grep newsmoniter.py | grep -v grep
    if [ $? = "1" ]; then
        echo "restart newsmoniter.py"
        /usr/local/bin/newsmoniter.py
    fi
    sleep 10
done
  1. Settings for automatic startup when the OS starts Use the OS standard function and set the above autorun.sh to start when the OS starts.

/Library/StartupItems/Newsmoniter


StartService()
{
    /usr/local/bin/autorun.sh
}
  1. Execution of MAC notification center call command by cron

cron


* * * * * /usr/local/bin/news_cmd.sh > /dev/null 2>&1; rm -f /usr/local/bin/news_cmd.sh > /dev/null 2>&1

What I want to do in the future

Operation check environment

Recommended Posts

I made a net news notification app with Python
I made a fortune with Python.
I made a daemon with Python
I made a character counter with Python
I made a Hex map with Python
I made a roguelike game with Python
I made a simple blackjack with Python
I made a configuration file with Python
I made a neuron simulator with Python
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
[Python] I made a Youtube Downloader with Tkinter.
I made a bin picking game with Python
I made a Mattermost bot with Python (+ Flask)
I made blackjack with python!
I made a python text
I made blackjack with Python.
I made wordcloud with Python.
I made a Twitter BOT with GAE (python) (with a reference)
I made a Christmas tree lighting game with Python
I made a Python3 environment on Ubuntu with direnv.
I made a LINE BOT with Python and Heroku
I made a simple typing game with tkinter in Python
I made a simple book application with python + Flask ~ Introduction ~
I made a puzzle game (like) with Tkinter in Python
I made a simple circuit with Python (AND, OR, NOR, etc.)
I made a package that can compare morphological analyzers with Python
I made a payroll program in Python!
I tried scraping Yahoo News with Python
I drew a heatmap with seaborn [Python]
I made a Nyanko tweet form with Python, Flask and Heroku
I made a lot of files for RDP connection with Python
Daemonize a Python web app with Supervisor
Created a darts trip with python (news)
[Python] I made an image viewer with a simple sorting function.
I tried a functional language with Python
I made a shuffle that can be reset (reverted) with Python
I tried to make a 2channel post notification application with Python
What I did with a Python array
I made a life game with Numpy
I made a stamp generator with GAN
I made a poker game server chat-holdem using websocket with python
After studying Python3, I made a Slackbot
Make a desktop app with Python with Electron
I made a segment tree with python, so I will introduce it
I made a WEB application with Django
Othello app (iOS app) made with Python (Kivy)
I made a tool to automatically browse multiple sites with Selenium (Python)
I tried to discriminate a 6-digit number with a number discrimination application made with python
I made a tool that makes decompression a little easier with CLI (Python3)
I made a module PyNanaco that can charge nanaco credit with python
I made a stamp substitute bot with line
I made a python dictionary file for Neocomplete
I want to make a game with Python
Procedure for creating a LineBot made with Python
I made a simple Bitcoin wallet with pycoin
I made a LINE Bot with Serverless Framework!
I made a random number graph with Numpy
I want to write to a file with Python
I made a Caesar cryptographic program in Python.
I made a Python Qiita API wrapper "qiipy"