æ¥ã
ãTLãè¦ãã¨å¨ãã®äººéã¯ããããªã¤ãã³ãã«åå ãã¦ãããããã
èªåããããã®ã¤ãã³ãã«åå ãããããäºåã«éå¬æ
å ±ãç¥ããã¨ãåºæ¥ã¦ããªãã®ã§åå ã®ãããããªãã
ã¨ãã£ã¦ãæ¹æ³ãã¾ã£ããç¡ãããã§ã¯ãªãã¦ãåå¼·ä¼ã§ããã°ãITåå¼·ä¼ã«ã¬ã³ãã¼( http://bit.ly/qpPgrS ) ã¨ããè¦ãã¨ããæ¹æ³ã¯ãããããããITåå¼·ä¼ã«ã¬ã³ãã¼ã¯å¤§éã«ã¤ãã³ããããããã¦ãèªåã®å ´åã©ãã«åå ããã°è¯ãã決ããããªãããããããæ¥ã
éå¬ããã¦ããã¤ãã³ãã®ãã¡ã©ãã ãã網ç¾
ããã¦ããã®ãè¯ãåããããããã«é ¼ã£ã¦è¯ãã®ããä¸å®ãè½åçã«ãã§ãã¯ããã¨ããã®ãé¢åãããã¦ãèªåã®å ´åã¯ç¶ããªãã
# Googleã«ã¬ã³ãã¼ã使ã£ã¦ãã人ã¯èªåã®ã«ã¤ã³ãã¼ãããã°è¯ãããã ãã©ããã®éããã®ã¾ã¾ã¤ã³ãã¼ããã¦ãè¾ããã®ããã
ããã§ãèªåã®TLããèªåã§ä½æããlistã®ä¸ã§çºè¨ãããã¤ãã³ãã®æ
å ±ãåéããã¹ã¯ãªãããæ¸ãã¦ã¿ãã®ã§ç´¹ä»ããã
åä½æ¦è¦
æå®ãããªã¹ãã®Tweetsãããæå®ãããã¼ã¯ã¼ãã®ãããããå«ã Tweet ãæ½åºãæå®ãããã£ã¬ã¯ããªã¸ããã¹ããã¡ã¤ã«å½¢å¼ã§æ¸ãåºãã
ãã¡ã¤ã«ã¯æ¥æ¯ã§ä½æãããã
ã³ã¼ã ( collect.rb )
# -*- coding: euc-jp -*- require 'rubygems' require 'pp' # for Twitter require 'twitter' # for Userstream require 'userstream' require 'kconv' CONSUMER_KEY = "<åå¾ããå¤ãè¨å®>" CONSUMER_SECRET = "<åå¾ããå¤ãè¨å®>" OAUTH_TOKEN = "<åå¾ããå¤ãè¨å®>" OAUTH_TOKEN_SECRET = "<åå¾ããå¤ãè¨å®>" LIST_NAME = "<èªåã§ä½ã£ããªã¹ãåãè¨å®>" FILE_OUT_PATH_BASE = "./" KEYWORDS = ["ãªã","åå¼·ä¼"] # @brief æå®ããããªã¹ãã®ã¡ã³ããè¿ã # @param [in] list_name åå¾ãããªã¹ãå ( String ) # @param [in,out] # @param [out] # @retval æå±ããã¦ã¼ã¶ screen_nameãªã¹ã( Array <String> ) # @note Twitterã¯ã©ã¹ã®åæå(èªè¨¼)ãå®äºããå¾ã§å¼ã³åºãã㨠def get_list_members(list_name) if list_name == "" return nil end list_names_arr = Array.new() cursor_num = -1 loop do result = Twitter.list_members(list_name, {:cursor => cursor_num}) result[:users].each{ |elem| list_names_arr << elem.screen_name } cursor_num = result[:next_cursor] break if cursor_num == 0 end return list_names_arr end class TweetAnalyzer def initialize() end # @brief æååããã¼ã¯ã¼ã群ãå«ããå¤å®ãã # @param [in] str æ¤ç´¢å¯¾è±¡æåå ( String ) # @param [in] keyword_arr ãã¼ã¯ã¼ã群 ( String ) # @param [in,out] # @param [out] # @retval true ããããã®ãã¼ã¯ã¼ããå«ã¾ãã # @retval false ã©ã®ãã¼ã¯ã¼ããå«ã¾ããªã # @note Twitterã¯ã©ã¹ã®åæå(èªè¨¼)ãå®äºããå¾ã§å¼ã³åºãã㨠def is_contain_keyword(str, keyword_arr) keyword_arr.each{ |keyword| if str.index(keyword) != nil return true end } return false end # @brief ä¸ããããããã¹ããå¦çãã # @param [in] tweet_text tweetããã¹ã ( String ) # @param [in,out] # @param [out] # @retval ç¡ã # @note Twitterã¯ã©ã¹ã®åæå(èªè¨¼)ãå®äºããå¾ã§å¼ã³åºãã㨠def process_tweet(tweet_text) # ãã¼ã¯ã¼ããå«ãã§ããããã§ããã°ä¿æ if is_contain_keyword(tweet_text, KEYWORDS) == true ofile_path = FILE_OUT_PATH_BASE + Time.now().strftime("%Y-%m-%d") + ".txt" `echo "#{tweet_text.tosjis()}" >> #{ofile_path}` end end end # ãã°ã¤ã³(1) Twitter.configure do |config| config.consumer_key = CONSUMER_KEY config.consumer_secret = CONSUMER_SECRET config.oauth_token = OAUTH_TOKEN config.oauth_token_secret = OAUTH_TOKEN_SECRET end # ãã°ã¤ã³(2) consumer = OAuth::Consumer.new( CONSUMER_KEY, CONSUMER_SECRET, {:site => 'https://userstream.twitter.com/'} ) access_token = OAuth::AccessToken.new( consumer, OAUTH_TOKEN, OAUTH_TOKEN_SECRET ) list_members = get_list_members(LIST_NAME) analyzer = TweetAnalyzer.new() userstream = Userstream.new(consumer, access_token) userstream.user do |status| begin # æå®ããããªã¹ãã«å«ã¾ããã¦ã¼ã¶ã®çºè¨ã®ã¿æ½åº if status[:text] && ( list_members == nil || list_members.include?(status[:user][:screen_name])) analyzer.process_tweet(status[:text].toeuc()) end rescue => e p e end end
使ãæ¹
å¿ è¦ãªgemãã¤ã³ã¹ãã¼ã«ãã
gem install userstream twitter
1.APIã®èªè¨¼ãã¼ãè¨å®ããã
ã¾ãã以ä¸ã®4ã¤ãåå¾ãããåå¾æ¹æ³ã¯ã³ãã© ( http://bit.ly/qDrJtM ) ãåç
§ã
åå¾ã§ãããã³ã¼ãã®ååã®å®æ°ã«è¨å®ã
- ã»CONSUMER_KEY
- ã»CONSUMER_SECRET
- ã»OAUTH_TOKEN
- ã»OAUTH_TOKEN_SECRET
2.ãã¼ã¯ã¼ããéãããªã¹ãåãLIST_NAMEå®æ°ã«è¨å®ããã
ãªã¹ããæå®ããªã(TLã®tweetså
¨ã¦ãæå®)ããå ´åã¯ç©ºæå("")ãè¨å®ãä¸ã®ã³ã¼ãã§ã¯ç§ãä½ã£ãnagoyaãªã¹ã ( http://twitter.com/#!/list/ryo_grid/nagoya ) ã«ãªã£ã¦ããã
# ã¡ãªã¿ã«ããã®ãªã¹ãã¯åå¤å±è¿è¾ºå¨ä½(ã¨ãã¼ãã)æ¹ã
ã®ãªã¹ãã§ãã³ã¬ ( http://bit.ly/nCM7Pp ) ã使ã£ã¦èã¥ãæ¹å¼ã§ä½æãã½ã¼ã·ã£ã«ããã¯ããããã§ã^p^
3.ãã£ã«ã¿ãããããã¼ã¯ã¼ããKEYWORDSå®æ°ã«è¨å®ããã
ä¸ã®ã³ã¼ãã§ã¯"ãªã"ã¨"åå¼·ä¼"ã®2ã¤ãè¨å®ããã¦ããã
ä»ã«ã足ãããæ¹ã¯ã©ããã
4.ãã¨ã¯ä»¥ä¸ã§èµ·åããã ãã
ruby collect.rb
ããã¯ã°ã©ã¦ã³ãã§å®è¡ãããªããããªæã?
nohup ruby collect.rb 2>&1 > /dev/null &
ä¾
èªåã¯ä»¥ä¸ã«ä½æãã¦ãWebããã¢ã¯ã»ã¹ã§ããããã«ãã¦ããã
http://ryogrid.net/~ryo/dist/nagoya_evt_tweets/
è½åçã«ãã§ãã¯ãããã¨ããã¨ãã¾ããããªãã®ã§ãèªåã¯ä½æããããã¡ã¤ã«ã®URLãã±ã¤ã¿ã¤(ã¹ãã¼ããã©ã³)ã«ã¡ã¼ã«ããããcronãè¨å®ãã¦ããã
% crontab -l 55 23 * * * /bin/sh /home/ryo/cron_script/mail_evt_txt.sh % less /home/ryo/cron_script/mail_evt_txt.sh #! /bin/sh echo "http://ryogrid.net/~ryo/dist/nagoya_evt_tweets/"`date +%Y-%m-%d.txt` | mail -s "nagoya_evt_tweets "`date +%Y-%m-%d` [email protected] -c ""
ãããã
Tweetã®é¸å¥æ¹æ³ãåç´ãããã®ã§ããå°ãè³¢ãããã¤ããã
ä¾ãã°ã"ãªã"ã¨ãããã¼ã¯ã¼ããè¨å®ããå ´åã"ãªãã£ã¹"ã¨ã"ãªãã©ã¤ã³"ãªãã¦ã®ãå¼ã£ããã£ã¦ãã¾ããããã¯ããã¾ãã
ã²ã¨ã¾ããæ£è¦è¡¨ç¾ã§ã使ãã®ããªã
ãã¨ãTweetå
¨æã¯ãããªãã®ã§ãã¼ã¯ã¼ãã ãæãåºãã¦ã«ã¦ã³ãæ°ã§ã©ã³ãã³ã°ããã¨ããããã
ãããããããã«ã¯ãå½¢æ
ç´ è§£æã¨ãå¿
è¦ãªæ°ããããã©Rubyã§åºæ¥ãã®ã ãããã
è¤æ°æååããä¸è´ãããã¼ã¯ã¼ããæ½åºãããã¨ãã§ããã°å½¢æ
ç´ è§£æããªãã§æ¸ãã ããããå¦çã³ã¹ãã¯ãªã¼ãºããã«ãªç¯å²ã«åã¾ãã ããããRubyã ã¨ã¢ã«ã´ãªãºã ãè³¢ãã¦ãå³ããããªã
å ã«è¨ã£ã¦ãã
Twitterã¯ã©ã¤ã¢ã³ãã§ãã£ã«ã¿ãããã°è¯ããããã¨ãæã£ã人ããããããããªãã
ããããããã ã¨èªçºçã«ãã§ãã¯ããªãã¨ãã¡ã§ç¶ããªãã®ã§ããã
ãã®ç¹ãä»åã®ã¹ã¯ãªããã使ãã°ãã¡ã¼ã«ã§å¼·å¶çã«èªã¾ããã¨ãã§ããã®ã§è¯ãã