Skip to content

Instantly share code, notes, and snippets.

@nakazye
Last active August 29, 2015 14:12
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save nakazye/5d0c89022df5f991fe12 to your computer and use it in GitHub Desktop.
hatebu2get.py
import time
import urllib.request
import xml.etree.ElementTree as ET
from requests_oauthlib import OAuth1Session
# RSSフィードのURI
feeduri = "http://nakazye.hatenablog.com/feed"
# はてブカウント用ののURI
htbcounturi = "http://api.b.st-hatena.com/entry.count?url="
# link引っ張るためのxPath
xpath = "{http://www.w3.org/2005/Atom}entry/{http://www.w3.org/2005/Atom}link"
# hatena Consumer Key
htbck = "xxxxxxxxx"
# hatena Consumer Secret
htbcs = "xxxxxxxxx"
# hatena Access Token
htbat = "xxxxxxxxx"
# hatena Accesss Token Secert
htbas = "xxxxxxxxx"
# はてなにログイン
hatena = OAuth1Session(htbck, htbcs, htbat, htbas)
# 1分間隔で実行
while True:
# RSSフィードからブログのURIリストを取得する
data = ET.fromstring(urllib.request.urlopen(feeduri).read())
for node in data.findall(xpath):
target = node.attrib["href"]
# b'3' みたいなデータが取れる(byteをそのまま文字列にする方法あるだろうけど判別できてるしコレで)
bukuma = str(urllib.request.urlopen(htbcounturi + target).read())
if bukuma == "b'1'":
# はてブにポストする
params = {"url": target}
req = hatena.post("http://api.b.hatena.ne.jp/1/my/bookmark",
params=params)
print('はてブしたよ!', target, req)
time.sleep(60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment