The jonki

呼ばれて飛び出てじょじょじょじょーんき

bottonX: TwitterにRSSから引っ張りだした記事を適当に投稿するボット

前の日記で書いたソースを載せます。
例のごとくPython

bottonXってこんなやつ。
一応、自分のサーバで常時稼働してます。
http://twitter.com/bottonX


#!/usr/bin/python
#-*- coding: utf-8 -*-

#@author Junki OHMURA
#@date 2009/02/09


import re

#simplejson-2.0.7
#python-twitter-0.5
import twitter

#記事取り出しライブラリ
from bottonLib import *

import random
import time

media = [
'[CNET ja] ',
'[CNET en] ',
'[ITpro] ',
'[P2P Today] ',
'[/. Japan] ',
'[ITmedia] ',
'[PC watch] '
]

url = [
'http://feeds.japan.cnet.com/cnet/rss',
'http://news.cnet.com/2547-1_3-0-20.xml',
'http://itpro.nikkeibp.co.jp/rss/ITpro.rdf',
'http://wslash.com/?feed=rss2',
'http://slashdot.jp/slashdotjp.rss',
'http://rss.itmedia.co.jp/rss/1.0/news_bursts.xml',
'http://pc.watch.impress.co.jp/sublink/pc.rdf',
]

while(2>1):
randUrl = random.randint(0,len(media)-1)
btn = Botton()
main_body = btn.output(url[randUrl])
randArticle = random.randint(0, len(main_body)-1)
msg = media[randUrl] + main_body[randArticle].encode('utf-8')
print msg
tapi = twitter.Api(username='hogehoge@hoge.com', password='password')
status = tapi.PostUpdate(msg)
time.sleep(1800) #単位は秒、1800秒=30分



・bottonLib


#!/usr/bin/python -S
# -*- coding: utf-8 -*-

#@author Junki OHMURA
#@date 2009/02/09

import urllib
import BeautifulSoup


class Botton:
def __init__(self):
self.hoge="hoge"

#linkタグからリンク取り出し
def getLink(self, _soup):
_link_list = []
for item in _soup.findAll('item'):
_item_str = item.encode('utf-8')
_link_tag = _item_str.split('')
_link_list.append( _link_tag[1].split('\n')[0] )

return _link_list

def output(self, _url):
soup = BeautifulSoup.BeautifulSoup(urllib.urlopen(_url))
link_list = self.getLink(soup)
l=0
_main_body = []
for item in soup.findAll('item'):
_main_body.append(item.title.string + link_list[l] )
l += 1

return _main_body