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.
① 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.
Created in all 5 processes.
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
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
/Library/StartupItems/Newsmoniter
StartService()
{
/usr/local/bin/autorun.sh
}
cron
* * * * * /usr/local/bin/news_cmd.sh > /dev/null 2>&1; rm -f /usr/local/bin/news_cmd.sh > /dev/null 2>&1
Recommended Posts