Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
規約違反にならない感じでのお知らせツール
import time
import urllib.request
import xml.etree.ElementTree as ET
from requests_oauthlib import OAuth1Session
from datetime import datetime
# RSSフィードのURI
feeduri = "http://nakazye.hatenablog.com/feed"
# はてブカウント用ののURI
htbcounturi = "http://api.b.st-hatena.com/entry.count?url="
# twitter投稿用のURI
twitterpost = "https://api.twitter.com/1.1/statuses/update.json"
# link引っ張るためのxPath
xpath = "{http://www.w3.org/2005/Atom}entry/{http://www.w3.org/2005/Atom}link"
# twitter Consumer Key
twck = "xxxxxxx"
# twitter Consumer Secret
twcs = "xxxxxxx"
# twitter Access Token
twat = "xxxxxxx"
# twitter Accesss Token Secert
twas = "xxxxxxx"
print("start", datetime.now().strftime("%Y/%m/%d %H:%M:%S"))
ignore = []
# Twitter認証用
hatena = OAuth1Session(twck, twcs, twat, twas)
# ガイドラインに従い30分感覚で実行
while True:
# RSSフィードからブログのURIリストを取得する
data = ET.fromstring(urllib.request.urlopen(feeduri).read())
for node in data.findall(xpath):
target = node.attrib["href"]
if target not in ignore:
# b'3' みたいなデータが取れる(byteをそのまま文字列にする方法あるだろうけど判別できてるしコレで)
bukuma = str(urllib.request.urlopen(htbcounturi + target).read())
print('check:', target, bukuma)
if bukuma == "b'1'":
# Twitterにポスト
params = {"status": "@nakazye はてぶついたよ! " + target}
req = hatena.post(twitterpost,
params=params)
print('はてぶついたよ!', target, req, datetime.now().strftime("%Y/%m/%d %H:%M:%S"))
ignore.append(target)
elif bukuma != "b''":
ignore.append(target)
time.sleep(1800)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment