Skip to content

Instantly share code, notes, and snippets.

@kimihito
Created July 30, 2012 13:25
Show Gist options
  • Save kimihito/3206886 to your computer and use it in GitHub Desktop.
Save kimihito/3206886 to your computer and use it in GitHub Desktop.
コマンドラインから品詞の後にwwwをつけてつぶやくスクリプト
#!/usr/bin/env python
# coding: utf-8
import sys
import tweepy
from bs4 import BeautifulSoup
import urllib
import urllib2
import random
#YahooAPIのID(各自で取得)
YahooAPI = "YOUR_APP_ID"
#Yahoo形態素解析URL
parseurl = "http://jlp.yahooapis.jp/MAService/V1/parse"
#解析するメソッド
def www(sentence, appid=YahooAPI):
sentence = urllib.quote_plus(sentence.encode("utf-8"))
query =\
"%s?appid=%s&sentence=%s"%(parseurl,YahooAPI,sentence)
c = urllib2.urlopen(query)
soup = BeautifulSoup(c.read())
return [w.surface.string for w in soup.ma_result.word_list]
sentence = unicode(sys.argv[1],"utf-8")
result = www(sentence)
words = [word + 'w' * random.randint(1,10) for word in result]
#合計が140文字になるようにする
tweet = "".join(words)[:140]
#twitterのOAuthの設定(各自で取得)
CONSUMER_KEY = "YOUR_CONSUMER_KEY"
CONSUMER_SECRET = "YOUR_SECRET_KEY
ACCESS_TOKEN = "YOUR_ACCESS_TOKEN"
ACCESS_TOKEN_SECRET = "YOUR_ACCESS_TOKEN_SECRET
auth = tweepy.OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth_handler = auth)
#つぶやき&つぶやいた文字をコマンドラインで表示
api.update_status(tweet)
print tweet.encode("utf-8")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment