Last active
August 29, 2015 14:06
-
-
Save rubyrider/954449882162fdeac6bd to your computer and use it in GitHub Desktop.
Event Fetching Process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module FetchEvent | |
extend ActiveSupport::Concern | |
def fetch_additional_photo | |
@offset, limit, fixed = 50, 50, 50 | |
excluding_pid_lists = [].join(',') | |
0.upto(@offset) do |i| | |
increment = (fixed*i) | |
@offset+=increment | |
collections = FbGraph::Query.new("SELECT pid, aid ,caption, place_id ,link ,caption_tags, like_info, can_tag ,created, comment_info ,place_id, object_id, src, src_small, src_big FROM photo WHERE owner = me() AND NOT (pid IN '#{excluding_pid_lists}') LIMIT #{limit} OFFSET #{increment}").fetch(:access_token => access_token) | |
collections << FbGraph::Query.new("SELECT pid, aid ,caption, place_id ,link ,caption_tags, like_info, can_tag ,created, comment_info ,place_id, object_id, src, src_small, src_big, object_id FROM photo WHERE pid IN (SELECT pid FROM photo_tag WHERE subject = me() AND NOT (pid IN '#{excluding_pid_lists}')) LIMIT #{limit} OFFSET #{increment}").fetch(:access_token => access_token) | |
collections.flatten! | |
collections.each do |collection| | |
p = hashify_photo_content(collection) | |
c=CachedPhotoResource.find_by_pid(p[:pid]) | |
if !c | |
CachedPhotoResource.create!(p) | |
else | |
c.update_attributes!(p) | |
end | |
end | |
self.update_facebook_content | |
break if collections.size < fixed | |
end | |
self.update_attributes({initial_user_state: 'completed'}) | |
end | |
def update_facebook_content | |
self.cached_photo_resources.each do |resource| | |
match = (resource.categories & PageCategory.allowed_list) | |
next if match.blank? | |
pids = resource.pid | |
next if Photo.where(pid: resource.pid).count > 0 | |
next if (resource.user_id != self.id) | |
location_attr = resource.prepared_location_hash(self.id) | |
@location = Location.find_by(pid: resource.place_id) || Location.create(location_attr) | |
if resource.location_category_hash | |
resource.location_category_hash.each do |category| | |
begin | |
@location.location_categories.find_or_create_by(category) | |
rescue => e | |
logger.info e.message | |
end | |
end | |
end | |
@event = Event.unscoped.find_by(aid: resource.aid, place_object_id: resource.place_id.to_s, owner_id: self.id) || Event.create!(resource.prepared_event_hash(@location.pid)) | |
@photos = self.cached_photos.where(place_id: {"$ne" => nil}, place_id: @location.pid.to_i, aid: resource.aid) | |
logger.info "total photo...." | |
logger.info @photos.size | |
logger.info "#############" | |
@photos.each do |scrapped_photo| | |
begin | |
@photo = @event.photos.create!(scrapped_photo.prepared_photo_hash) | |
if @photo | |
begin | |
if scrapped_photo.try(:collaborators) and scrapped_photo.collaborators.size > 0 | |
scrapped_photo.collaborators.each do |collaborator| | |
c = @photo.tagged_persons.new(collaborator) | |
c.photo_id = @photo.id | |
c.event_id = @event.id | |
c.user_id = self.id | |
c.auto_add = true | |
c.save! | |
end | |
end | |
rescue => e | |
next | |
end | |
end | |
rescue => e | |
next | |
end | |
end | |
self.events << @event | |
end | |
get_contents_from_tags | |
end | |
def get_contents_from_tags | |
cache_resources = CachedPhotoResource.where(user_id: self.id).collect { |cpr| next if cpr.try(:name_tag_place).nil?; cpr }.reject { |c| c.nil? } | |
cache_resources.each do |res| | |
place = res.send :name_tag_place | |
location_attr = res.location_hash_from_name_tag(place) | |
@location = Location.find_by(pid: location_attr[:pid]) || Location.create!(location_attr) | |
categories = res.location_category_hash(place) | |
categories.each do |category| | |
@location.location_categories.find_or_create_by(category) | |
end | |
@event = Event.unscoped.find_by(aid: res.aid, place_object_id: @location.pid, owner_id: self.id) || Event.create(res.prepared_event_hash(@location.pid)) | |
scrapped_photo = res | |
@photo = @event.photos.create(scrapped_photo.prepared_photo_hash(@location.pid)) | |
begin | |
if scrapped_photo.try(:collaborators) and scrapped_photo.collaborators.size > 0 | |
scrapped_photo.collaborators.each do |collaborator| | |
c = @photo.tagged_persons.new(collaborator) | |
c.photo_id = @photo.id | |
c.event_id = @event.id | |
c.user_id = self.id | |
c.auto_add = true | |
c.save! | |
end | |
end | |
rescue => e | |
next | |
end | |
end | |
end | |
alias_method :update_event_contents, :update_facebook_content | |
module ClassMethods | |
def clear_old_data | |
list = %w(users photos user_accounts events tagged_people locations invitations facebook_friends facebook_pages locations user_events tagged_colors) | |
list.each do |table| | |
ActiveRecord::Base.connection.execute("truncate #{table}") | |
end | |
CachedPhotoResource.destroy_all | |
Rails.cache.clear | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment