[PYTHON] A script that keeps looking up until the URL is bookmarked with Hatena Bookmark

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

A script that keeps looking up until the URL is bookmarked with Hatena Bookmark
A script that morphologically parses a specified URL
A model that identifies the guitar with fast.ai
Create a python script to check if the link at the specified URL is valid 2
Create a python script to check if the link at the specified URL is valid
Make a BOT that shortens the URL of Discord
The Blender script (2.8) that erases only the skirt is completed
A memo that I touched the Datastore with python
Script that changes the length of the sound with REAPER
A script that makes it easy to create rich menus with the LINE Messaging API
Workaround for the problem that sys.argv is not passed when executing a Python script with only the file name in Python2.7 on Windows