Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
require 'j_api_agent/twitter_collection/base'
module JApiAgent::TwitterCollection
  class TwProfiles
    include JApiAgent::TwitterCollection::Base

    @queue = :twitter

    attr_accessor :friends_ids, :followers_ids, :profile
    def initialize(twitter_handle)
      @handle = twitter_handle
      @profile = JApiAgent::TwitterAgent::TwUserProfile.execute(:realtime,:screen_name=> twitter_handle)
      @friends_ids = prepare_ids(:friends)
      @followers_ids = prepare_ids(:followers)
    end

    def prepare_ids(type)
      if self.class.get_connection_keys(@handle,type.to_s).empty?
        ids = case type
              when :followers
                JApiAgent::TwitterAgent::TwFollowers.all_followers_ids(:screen_name => @handle)
              when :friends
                JApiAgent::TwitterAgent::TwFriends.all_friends_ids(:screen_name => @handle)
              end
        ids.each {|x| Resque.redis.sadd(self.class.profile_connection_update_key(@handle,type.to_s),x)}
      else
        ids = self.class.get_connection_keys(@handle,type.to_s)
      end
      file_name = "twitter_connections_#{Time.now.strftime("%Y%m%d")}.csv"
      self.class.output_user_connections(file_name, @profile[0]["id"], type.to_s, ids)
      ids
    end

    def pending_ids(type)
      asking_ids = self.send(type)
      asking_ids - JApiAgent::TwitterAgent::TwUserProfile.updated_profiles.map(&:to_i)
    end

    def self.output_profiles(filename,profiles)
      add_header = !File.exists?(filename)
      CSV.open(filename, "a", {:headers => USER_PROFILE_MAPPING_KEYS, :write_headers => add_header }) do |csv|
        profiles.each do |profile|
          profile = profile.merge({"channel" => "twitter"})
          csv << profile.values_at(*(USER_PROFILE_MAPPING.values_at(*USER_PROFILE_MAPPING_KEYS)))
        end
      end
    end

    def self.output_user_connections(filename, user_id,type,friends)
      add_header = !File.exists?(filename)
      CSV.open(filename, "a", {:headers =>USER_CONNECTIONS_KEYS, :write_headers => add_header }) do |csv|
        friends.each do |f|
          csv << ['twitter', user_id, type, f]
        end
      end
    end

    def self.fetch(twitter_handle)
      filename = self.profiles_filename
      begin
        profile = self.new(twitter_handle)
        JApiAgent::TwitterAgent::TwUserProfile.batch(:delayed, :user_id, profile.pending_ids(:friends_ids))
        JApiAgent::TwitterAgent::TwUserProfile.batch(:delayed, :user_id, profile.pending_ids(:followers_ids))
        Resque.enqueue(self, :output_profiles, filename,profile.profile)
      rescue
        Resque.enqueue_in(3600, self,:fetch, twitter_handle)
      end
    end

    def self.perform(method, *params)
      begin
        self.send method, *params
      rescue
        Resque.enqueue_in(3600,self,method, *params)
      end
    end

    def self.profile_update_key
      "updated:#{self.name}:#{Time.now.strftime("%Y%m%d")}"
    end

    def self.profile_connection_update_key(handle,type)
      "updated:#{self.name}:#{handle}:#{type}:#{Time.now.strftime("%Y%m%d")}"
    end

    def self.get_connection_keys(handle,type)
      Resque.redis.smembers(self.profile_connection_update_key(handle,type))
    end

    def self.profiles_filename
      "twitter_profiles_#{Time.now.strftime("%Y%m%d")}.csv"
    end

  end
end