<commit_before>#encoding: utf-8
class FlickrPhoto < Photo
  
  Photo.descendent_classes ||= []
  Photo.descendent_classes << self
  
  validates_presence_of :native_photo_id
  validate :user_owns_photo

  def user_owns_photo
    if user
      @api_response ||= FlickrPhoto.get_api_response(native_photo_id, :user => user)
      fp_flickr_user_id = @api_response.owner.nsid
      
      if user.flickr_identity.blank? || fp_flickr_user_id != user.flickr_identity.flickr_user_id
        errors.add(:user, "must own the photo on Flickr.")
      end
    end
  end
  
  def self.flickraw_for_user(user)
    return flickr unless (user && user.flickr_identity)
    f = FlickRaw::Flickr.new
    f.access_token = user.flickr_identity.token
    f.access_secret = user.flickr_identity.secret
    f
  end
  
  def self.get_api_response(native_photo_id, options = {})
    f = options[:user] ? flickraw_for_user(options[:user]) : flickr
    f.photos.getInfo(:photo_id => native_photo_id)
  end
  
  def self.new_from_api_response(api_response, options = {})
    new_from_flickraw(api_response, options)
  end
  
  def self.new_from_flickraw(fp, options = {})
    if fp.respond_to?(:urls)
      urls = fp.urls.index_by{|u| u.type}
      photopage_url = urls['photopage']._content rescue nil
    else
      photopage_url = "http://flickr.com/photos/#{fp.owner}/#{fp.id}"
    end
    options[:native_photo_id] = fp.id<commit_msg>Fallback to non-authenticated Flickr request for get_api_response if user token is invalid.<commit_after>#encoding: utf-8
class FlickrPhoto < Photo
  
  Photo.descendent_classes ||= []
  Photo.descendent_classes << self
  
  validates_presence_of :native_photo_id
  validate :user_owns_photo

  def user_owns_photo
    if user
      @api_response ||= FlickrPhoto.get_api_response(native_photo_id, :user => user)
      fp_flickr_user_id = @api_response.owner.nsid
      
      if user.flickr_identity.blank? || fp_flickr_user_id != user.flickr_identity.flickr_user_id
        errors.add(:user, "must own the photo on Flickr.")
      end
    end
  end
  
  def self.flickraw_for_user(user)
    return flickr unless (user && user.flickr_identity)
    f = FlickRaw::Flickr.new
    f.access_token = user.flickr_identity.token
    f.access_secret = user.flickr_identity.secret
    f
  end
  
  def self.get_api_response(native_photo_id, options = {})
    f = options[:user] ? flickraw_for_user(options[:user]) : flickr
    f.photos.getInfo(:photo_id => native_photo_id)
  rescue FlickRaw::FailedResponse => e
    raise e unless e.message =~ /Invalid auth token/
    flickr.photos.getInfo(:photo_id => native_photo_id)
  end
  
  def self.new_from_api_response(api_response, options = {})
    new_from_flickraw(api_response, options)
  end
  
  def self.new_from_flickraw(fp, options = {})
    if fp.respond_to?(:urls)
      urls = fp.urls.index_by{|u| u.type}
      photopage_url = urls['photopage']._content rescue nil
    else
      photopage_url = "http://flickr.com/photos/#{fp.owner}/#{fp.id}"
    end
    options[:native_photo_id] = fp.id
{'fraction_non_alphanumeric': 0.10688524590163935, 'fraction_numerical': 0.0006557377049180328, 'mean_word_length': 3.608761329305136, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3712680', 'n_tokens_mistral': 1116, 'n_tokens_neox': 1089, 'n_words': 192}