hatebu2get.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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