LoginSignup
4
4

More than 5 years have passed since last update.

blogの更新を監視して結果をtwitterに投げたり、interviewsの回答を催促したり

Last updated at Posted at 2013-08-16

はい。
twiiterでの活動・発言とblog更新が連動して無い方をこっそりと更新を見逃さないようという目的で

kanshi.py
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import urllib2
import lxml.html
from urllib import urlencode
from oauth2 import Client, Consumer, Token
import oat
#from prettyprint import pp, pp_str
#import re


blog = 'http://hoge.com'
# IE8のフリをする
user_agent = 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)'
req = urllib2.Request(blog, None, {'User-Agent': user_agent})
res = urllib2.urlopen(req)
#対象サイトのaタグ要素を取り出す
html = lxml.html.fromstring(res.read())
title = html.xpath('//a')[1].text.encode('utf-8')

#サイトの構成によって最新記事の表示位置が異なるのでチェック用
#    for i in title:
#        print i.text
# 

read = open('/xxxx/xxx.txt')
log = read.read()
read.close

if title != log:
    twit = "@hoge 何か更新があったようです。" + title
    oat.client.request('https://api.twitter.com/1.1/statuses/update.json', 'POST', urlencode({'status': twit}))
    f = open('/xxxx/xxx.txt', 'w')
    f.write(title)
    f.close
else:
    pass

はい。
まぁ、わざわざこんなのよりも更新チェックするアプリとか世にあふれていると思うんですがアレでアレ。
しかも以前にinterviewsの回答状況監視のを大分使い回しになりました。
更新監視の対象が増えたらsqlite3とかでログを残すようにしたほうがいいのかな。
かなり大雑把なのでアレです。

インタビューズ

インタビューズの回答をtwitterで催促するbot

saisoku.py
#!/usr/bin/python
# -*- coding:UTF-8 -*-

import urllib2
import lxml.html
import sys
import string
from urllib import urlencode
from oauth2 import Client, Consumer, Token


consumer_key = "xxxx"
consumer_secret = "xxxxxx"
user_key = "xxx-xxxx"
user_secret = "xxxxx"
client = Client(Consumer(consumer_key,consumer_secret), Token(user_key,user_secret))

# urlを指定する
url = 'http://theinterviews.jp/催促したいユーザー'
# IE8のフリをする
user_agent = 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)'

req = urllib2.Request(url, None, {'User-Agent':user_agent})
res = urllib2.urlopen(req)

html = lxml.html.fromstring(res.read().decode('utf-8'))
check = html.text_content().count(u'回答率: 100.00%')


twit =  "@hoge どうやら回答率が100%ではないようです。いますぐ回答しに行きましょう!! " 

if check == 0:
    client.request('https://api.twitter.com/1.1/statuses/update.json', 'POST', urlencode({'status':twit}))
else:
    pass

html取得に関してはこちらから情報を頂きました。有難うございます。
http://d.hatena.ne.jp/stog/20100607/1275926301


私が書いた部分はまだまだ動けばいいんだよ状態です。
メイン?のユーザーとメンバーとで"回答率"と":"の間に半角スペースのアリナシの違いをついて非常に簡易に回答率の状況を判定してます。
回答率100%未満の際に催促だけして100%でも褒めたりすることはしません。
cronでぶん回すなどの話は省略になります。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4