If you write an article on your blog, do you ever want to find out if it's bookmarked? I have. That's why I made a guy who checks until a new bookmark is added and then alerts me when a new bookmark is added. Now you don't have to reload your browser. You did it!
However, notify is limited to Ubuntu, so Mac people should rewrite it appropriately.
python
# -*- coding: utf-8 -*-
import os
import sys
import urllib
import time
import datetime
from bs4 import BeautifulSoup
def user_loop():
    url = sys.argv[1].replace('http://', 'http://b.hatena.ne.jp/entry/')
    print "Checking ->", url
    previous = None
    while 1:
        html = urllib.urlopen(url).read().decode('utf-8')
        soup = BeautifulSoup(html)
        user = int(soup.find('ul', {'class': 'users'}).find('span').text)
        if previous is None:
            previous = user
            continue
        if previous != user:
            os.system('notify-send -u critical "Bookmarked" "%d users"' % user)
        previous = user
        time.sleep(600)
def check_loop():
    url = sys.argv[1].replace('http://', 'http://b.hatena.ne.jp/entry/')
    print "Polling -> ", url
    while 1:
        html = urllib.urlopen(url).read().decode('utf-8')
        if html.find(u'This page has not been bookmarked yet.') == -1:
            title = "The target page has been bookmarked."
            os.system('notify-send -u critical "%s"' % title)
            return
        title = "It doesn't seem to be bookmarked yet."
        popup = "[%s]<br />" % (datetime.datetime.now().isoformat())
        popup += "Try again after 5 minutes."
        os.system('notify-send -u low "%s" "%s"' % (title, popup))
        time.sleep(300)
def command():
    if len(sys.argv) == 1:
        print "usage: poll.py (url)"
        sys.exit(1)
    check_loop()
    user_loop()
if __name__ == '__main__':
    command()
Recommended Posts