Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'net/http'
- require 'json'
- # need to accumulate twitter usernames to avoid hitting twitter api
- # rate limit. the users/lookup method will lookup up to 100 users at
- # a time.
- @twitter_username_sxsocial_id_map = {}
- def get_user_id
- @username = @link.url.split('/').last
- @network = @link.url.match(/([\w]+).com/)[1]
- case @network
- when 'facebook' then get_facebook_id if @link.user.facebook_id.blank?
- when 'twitter' then push_twitter_username if @link.user.twitter_id.blank?
- when 'linkedin' then get_linkedin_id if @link.user.linkedin_id.blank?
- else
- @id = nil
- end
- if @id
- @link.user.save
- end
- end
- def get_facebook_id
- response = Net::HTTP.get_response('graph.facebook.com', "/#{@username}")
- if response.code == '200'
- puts response.body
- unless response.body.match(/^false/) # some kind of error
- json = JSON.parse(response.body)
- @id = json['id']
- @link.user.facebook_id = @id
- end
- else
- @id = nil
- end
- end
- def push_twitter_username
- @twitter_username_sxsocial_id_map[@username.downcase] = @link.user.id
- # can only do 100 at a time due to twitter api limitation
- if @twitter_username_sxsocial_id_map.size == 99
- pop_twitter_usernames
- @twitter_username_sxsocial_id_map = {}
- end
- @id = nil # so we don't try to save this user yet
- end
- def pop_twitter_usernames
- puts "POPPING TWITTER USERNAMES"
- username_list = []
- @twitter_username_sxsocial_id_map.each_key do |username|
- username_list << username
- end
- twitter_users = Twitter.users(username_list.join(', '))
- twitter_users.each do |twitter_user|
- our_user_id = @twitter_username_sxsocial_id_map[twitter_user['screen_name'].downcase]
- unless our_user_id.blank?
- our_user = User.find(our_user_id)
- our_user.update_attribute :twitter_id, twitter_user['id']
- end
- end
- end
- ### not implemented yet
- def get_linkedin_id
- @id = nil
- end
- #links = Link.find(:all, :conditions => 'user_id = 444')
- links = Link.find(:all,
- :conditions => "url ilike '%facebook.com%' or url ilike '%twitter.com%'"#,
- #:limit => 10
- )
- links.each do |link|
- @link = link
- get_user_id
- end
- # pop the remaining twitter usernames
- pop_twitter_usernames
Add Comment
Please, Sign In to add comment