class Unfollow::Unfollow

Attributes

client[RW]
options[RW]
username[RW]

Public Class Methods

new(o) click to toggle source
# File lib/ruby-unfollow.rb, line 199
def initialize(o)
        @options = o
        
        if File.exist?("/etc/ruby-unfollow.json") then
                conf=JSON.parse(File.read("/etc/ruby-unfollow.json"))

                @client = TwitterOAuth::Client.new(
                        :consumer_key => "XegcON7xJ0ONDc16BijevA",
                        :consumer_secret => "ktFOJy92lgDKHkQhvUPmU3JM9oBI19UfhyeuUwhfVho",
                        :token => conf["token"],
                        :secret => conf["secret"]
                )
        elsif File.exist?("#{Dir.home}/.ruby-unfollow/conf.json") then
                conf=JSON.parse(File.read("#{Dir.home}/.ruby-unfollow/conf.json"))

                @client = TwitterOAuth::Client.new(
                        :consumer_key => "XegcON7xJ0ONDc16BijevA",
                        :consumer_secret => "ktFOJy92lgDKHkQhvUPmU3JM9oBI19UfhyeuUwhfVho",
                        :token => conf["token"],
                        :secret => conf["secret"]
                )
        end

        if ! @client.authorized? then
                @client = TwitterOAuth::Client.new(
                        :consumer_key => "XegcON7xJ0ONDc16BijevA",
                        :consumer_secret => "ktFOJy92lgDKHkQhvUPmU3JM9oBI19UfhyeuUwhfVho"
                )

                request_token = @client.authentication_request_token(
                        :oauth_callback => 'oob'
                )

                puts request_token.authorize_url

                print 'Please visit the URL and enter the code: '
                code = gets.strip

                access_token = @client.authorize(
                    request_token.token,
                        request_token.secret,
                        :oauth_verifier => code
                )

                if @client.authorized? then
                        conf = {:token => access_token.token, :secret => access_token.secret}
                        begin
                                File.open("/etc/ruby-unfollow.json", "w+") {|f| f.write(conf.to_json) }
                        rescue
                                FileUtils.mkdir_p("#{Dir.home}/.ruby-unfollow")
                                File.open("#{Dir.home}/.ruby-unfollow/conf.json", "w+") {|f| f.write(conf.to_json) }
                        end
                end
        end
end

Public Instance Methods

d_ago(t1,d=nil) click to toggle source
# File lib/ruby-unfollow.rb, line 24
def d_ago(t1,d=nil)
        t2=Time.now()
        day_since=(t1-t2).abs/3600/24
        if d != nil then 
                if day_since>d then
                        return true
                else
                        return false
                end
        else
                return day_since.round
        end
end
exist_save?() click to toggle source
# File lib/ruby-unfollow.rb, line 42
def exist_save?
        if File.exist?("#{Dir.home}/.ruby-unfollow/unfollow.json") then
                return true
        else
                return false
        end
end
file_save(file,str) click to toggle source
# File lib/ruby-unfollow.rb, line 184
def file_save(file,str)
        if file != nil then
                if file != '' then
                        if File.exist?(file) then
                                File.open(file, "a+") {|f| f.write(str+"\n") }
                        else
                                FileUtils.mkdir_p(File.dirname(File.absolute_path(file)))
                                File.open(File.absolute_path(file), "w") {|f| f.write(str+"\n") }
                        end
                end
        else
                raise "File is nil"
        end
end
following(following,non_follower) click to toggle source
# File lib/ruby-unfollow.rb, line 154
def following(following,non_follower)
        following.reverse_each do |id|       
                u=@client.show(id)
                if u["screen_name"] != nil && u["status"] != nil then
                        t1=Time.parse(u["status"]["created_at"])
                        t=""
                        a=0
                        if d_ago(t1,@options.max_days) then # last tweet > 15 days ago
                                t=d_ago(t1).to_s.red
                                a=1
                        else
                                t=d_ago(t1).to_s
                        end
                        if non_follower.include?(id) then
                                puts "@"+u["screen_name"].red+"|"+t1.to_s+"|"+t
                                if a ==1 then
                                        file_save(@options.file,"@"+u["screen_name"]+"|"+t1.to_s+"|"+d_ago(t1).to_s+"|non_follower,inactive")
                                else
                                        file_save(@options.file,"@"+u["screen_name"]+"|"+t1.to_s+"|"+d_ago(t1).to_s+"|non_follower")
                                end
                        else
                                puts "@"+u["screen_name"]+"|"+t1.to_s+"|"+t
                                if a ==1 then
                                        file_save(@options.file,"@"+u["screen_name"]+"|"+t1.to_s+"|"+d_ago(t1).to_s+"|inactive")
                                end
                        end
                end
        end 
end
get_unfollow() click to toggle source
# File lib/ruby-unfollow.rb, line 50
def get_unfollow()
        following=@client.friends_ids()["ids"]
        follower=@client.followers_ids()["ids"]
        non_follower=[]
        following.each do |id|
                if !(follower.include? id) then
                        #puts id
                        non_follower.push(id)
                end
        end

        data={}
        data["follower"] = follower
        data["following"] = following
        data["non_follower"] = non_follower
        save_unfollow(data)
        return follower,following,non_follower
end
inactive(follower) click to toggle source
# File lib/ruby-unfollow.rb, line 122
def inactive(follower)
        follower.reverse_each do |id|
                u=@client.show(id)
                if u["screen_name"] != nil && u["status"] != nil then
                        t1=Time.parse(u["status"]["created_at"])
                         
                        if d_ago(t1,@options.max_days) then # last tweet > 15 days ago
                                puts "@"+u["screen_name"]+"|"+t1.to_s+"|"+d_ago(t1).to_s.red
                                file_save(@options.file,"@"+u["screen_name"]+"|"+t1.to_s+"|"+d_ago(t1).to_s+"|inactive")
                        end
                end
        end 
end
non_follower(non_follower,a=nil) click to toggle source
# File lib/ruby-unfollow.rb, line 98
def non_follower(non_follower,a=nil)
        non_follower.reverse_each do |id|
                u=@client.show(id)
                if u["screen_name"] != nil then
                        if u["status"] != nil then
                                t1=Time.parse(u["status"]["created_at"])
                         
                                if d_ago(t1,@options.max_days) then # last tweet > 15 days ago
                                        puts "@"+u["screen_name"].red+"|"+t1.to_s+"|"+d_ago(t1).to_s.red+"|inactive"
                                        file_save(@options.file,"@"+u["screen_name"]+"|"+t1.to_s+"|"+d_ago(t1).to_s+"|inactive")
                                else
                                        if a != :and then
                                                puts "@"+u["screen_name"].red+"|"+t1.to_s+"|"+d_ago(t1).to_s+"|non_follower"
                                                file_save(@options.file,"@"+u["screen_name"]+"|"+t1.to_s+"|"+d_ago(t1).to_s+"|non_follower")
                                        end
                                end
                        else
                                puts "@"+u["screen_name"].red+"|"+"Never".red+"|"+"Never".red
                                file_save(@options.file,"@"+u["screen_name"]+"|"+t1.to_s+"|"+d_ago(t1).to_s+"|inactive")
                        end
                end
        end 
end
non_follower_list(non_follower,l) click to toggle source
# File lib/ruby-unfollow.rb, line 69
def non_follower_list(non_follower,l)
        i=0
        c=true
        puts "Here is your "+l.to_s+" oldest unfollower: "
        while i<non_follower.count && c==true
                i+=l
                non_follower[-i,l].reverse_each do |id|
                        u=@client.show(id)
                        if u["screen_name"] != nil && u["status"] != nil then
                                t1=Time.parse(u["status"]["created_at"])
                                 
                                if d_ago(t1,@options.max_days) then # last tweet > 15 days ago
                                        puts "@"+u["screen_name"].red+"\t\t "+t1.to_s+"\t "+d_ago(t1).to_s.red
                                else
                                        puts "@"+u["screen_name"].red+"\t\t "+t1.to_s+"\t "+d_ago(t1).to_s
                                end
                        end
                end 
                puts "Print next "+l.to_s+" follower? (yes/no*)"
                c = gets.chomp
                if c == 'yes' then
                        c=true
                else
                        c=false
                        puts "Bye~"
                end
        end
end
or_follower(following,non_follower) click to toggle source
# File lib/ruby-unfollow.rb, line 136
def or_follower(following,non_follower)
        following.reverse_each do |id|       
                u=@client.show(id)
                if u["screen_name"] != nil && u["status"] != nil then
                        t1=Time.parse(u["status"]["created_at"])
                        if non_follower.include?(id) then
                                puts "@"+u["screen_name"].red+"|"+t1.to_s+"|"+d_ago(t1).to_s
                                file_save(@options.file,"@"+u["screen_name"]+"|"+t1.to_s+"|"+d_ago(t1).to_s+"|non_follower")
                        else
                                if d_ago(t1,@options.max_days) then # last tweet > 15 days ago
                                        puts "@"+u["screen_name"]+"|"+t1.to_s+"|"+d_ago(t1).to_s.red
                                        file_save(@options.file,"@"+u["screen_name"]+"|"+t1.to_s+"|"+d_ago(t1).to_s+"|inactive")
                                end
                        end
                end
        end 
end
save_unfollow(data) click to toggle source
# File lib/ruby-unfollow.rb, line 38
def save_unfollow(data)
        File.open("#{Dir.home}/.ruby-unfollow/unfollow.json", "w+") {|f| f.write(data.to_json) }
end