Class: Twitter2MixiVoice::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/application.rb

Overview

This class provides mixi bot application.

Constant Summary collapse

Posted_Tweets_Dir =

constants

File.join(ENV["HOME"], ".twitter2mixivoice")
Posted_File_Name =
"posted_tweets.yml"

Class Method Summary collapse

Class Method Details

.run(args) ⇒ Object

Run Application.

*args

command line options.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/application.rb', line 35

def self.run(args)
  
  # parse arguments.
  options = Options.new(args, Twitter2MixiVoice::version)
  
  #
  # check options.
  #
  if options[:invalid_argument]
    $stderr.puts options[:invalid_argument]
    options[:show_help] = true
  end
  
  if options[:show_version]
    $stdout.puts options[:show_version]
    return Success
  end
  
  if options[:show_help]
    $stderr.puts options.opts.to_s
    return Error
  end
  
  
  result = Error
  begin
    
    # login to twitter.
    twitter = (options[:twitter_id], options[:twitter_password])
    
    # login to mixi.
    mixi = (options[:mixi_email], options[:mixi_password])
    
    # read cache tweets from YAML file.
    posted_tweets = read_tweets(Posted_Tweets_Dir, Posted_File_Name)
    
    # get tweets from twitter.
    tweets = twitter.get_tweets_from_timeline
    
    # post twitter tweet to mixi voice.
    tweets.each do |tweet|
      if (!posted_tweets.has_key?(tweet.id))
        mixi.post_voice(tweet.text)
        posted_tweets[tweet.id] = tweet
        $stdout.puts "tweet -> voice : id = #{tweet.id}, text = #{tweet.text}"
      end
    end
    
    # write YAML file from cache tweets.
    result = write_tweets(Posted_Tweets_Dir, Posted_File_Name, posted_tweets)
    
    result = Success
    
  rescue MixiException => e
    $stderr.puts "Raise MixiException : #{e.to_s}"
    result = Error
  rescue TwitterClientException => e
    $stderr.puts "Raise MixiException : #{e.to_s}"
    result = Error
  rescue Exception => e
    $stderr.puts "Raise Exception : #{e.to_s}"
    result = Error
  end
  
  return result
  
end