# --- tweet list (maker id, list name)
   hogehoge myfav_list
   mogamoga shinshi-roku

source code

	#!/usr/bin/ruby
	
	require 'rubygems'
	require 'twitter'
	require 'dbi'
	
	# ----- login information
	TWITTER_ID = '@yourtwitid@'
	TWITTER_PASS = '@yourpass@'
	DBNAME = 'DBI:mysql:koruri:localhost'
	DBLOGIN = '@yourid@'
	DBPASS = '@yourpass@'
	TWEETLIST = 'twitter_list.txt'
	VERB = false
	
   # ---------------------------------
   # ----- subroutine
   
   def add_tweet(db, tweet, listname)
     time = Time.parse(tweet.created_at)
     sql = "insert into tweet_log values(NULL, ?, ?, ?, ?, ?, 0)"
     db.do(sql,
                tweet.text,
                time.to_i,
                tweet.user.name,
                tweet.user.screen_name,
                listname)
   end
   
   def check_already(db, tweet)
     count = 0
     sql = "SELECT * FROM tweet_log WHERE screen_name=? AND time=?"
     time = Time.parse(tweet.created_at)
     sth = db.execute(sql, tweet.user.screen_name, time.to_i )
     num = sth.fetch_all.size
     sth.finish
     if num > 0 then
       count += 1
     end
     # - return value
     count
   end
   
   def add_mentions(db, tweet)
     count = 0
     sql = "SELECT * FROM ruripeco_mention WHERE user=? AND time=?"
     time = Time.parse(tweet.created_at)
     sth = db.execute(sql, tweet.user.screen_name, time.to_i )
     num = sth.fetch_all.size
     sth.finish
     if num == 0 then
       time = Time.parse(tweet.created_at)
       sql = "insert into ruripeco_mention values(NULL, ?, ?, ?, 0)"
       db.do(sql,
             tweet.user.screen_name,
             tweet.text,
             time.to_i)
       end
   end
   
   
   # ---------------------------------
   # ----- main work
   
   # --- list open
   check_list = []
   open(TWEETLIST, "r") do |fp|
     fp.each do |line|
       next if line =~ /^\#/
       word = line.split(' ')
       next if word.size != 2
       check_list << line
     end
   end
   
   # --- open DB
   db = DBI.connect(DBNAME,DBLOGIN,DBPASS)
   db.do('SET NAMES utf8')
   
   httpauth = Twitter::HTTPAuth.new(TWITTER_ID, TWITTER_PASS)
   twit = Twitter::Base.new(httpauth)
   
   newline = 0
   
   # - my timeline
   puts "My timeline." if VERB == true
   twit.friends_timeline(:count => 25).each do |line|
     if check_already(db, line) == 0 then
       add_tweet(db, line, '') if line.user.screen_name != TWITTER_ID
       newline += 1
     end
   end
   
   # - mentions list
   puts "Get mentions." if VERB == true
   twit.mentions(:count => 10).each do |line|
     add_mentions(db, line) if line.user.screen_name != TWITTER_ID
   end
   
   # - from open list
   check_list.each do |list|
     item = list.split(' ')
     printf("author=%s, list=%s\n", item[0], item[1]) if VERB == true
     twit.list_timeline(item[0], item[1], :per_page => 50).each do |line|
       if check_already(db, line) == 0 then
         add_tweet(db, line, "#{item[0]}/#{item[1]}")
         newline += 1
       end
     end
   end
   
   printf("add %d new tweet.\n", newline) if VERB == true
   
   #done
   db.disconnect

トップ   差分 バックアップ リロード   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2017-11-06 (月) 01:22:22 (710d)