Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit

Permalink
Created scrape all events for orgs job, swapped user tokens for appli…
Browse files Browse the repository at this point in the history
…cation-level token
  • Loading branch information
afischer committed Aug 24, 2017
1 parent 974befe commit 810371f
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 17 deletions.
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ApplicationController < ActionController::Base
###########################

def index
ScrapeEventsForAllOrgsJob.perform_later
@meta_description = %(
Your guide to all the events Penn and Philly have to offer.
)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/orgs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def show
title: "#{@org.name} on Events@Penn",
image: meta_img
}
ScrapeNewEventsJob.perform_later(@org, access_token) if @org.fb? && user_signed_in?
ScrapeNewEventsJob.perform_later(@org) if @org.fb? && user_signed_in?
end

# GET /orgs/new
Expand Down
5 changes: 5 additions & 0 deletions app/jobs/application_job.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
class ApplicationJob < ActiveJob::Base
# Automatically retry jobs that encountered a deadlock
# retry_on ActiveRecord::Deadlocked

# Most jobs are safe to ignore if underlying records are no longer available
# discard_on ActiveJob::DeserializationError
end
9 changes: 9 additions & 0 deletions app/jobs/scrape_events_for_all_orgs_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ScrapeEventsForAllOrgsJob < ApplicationJob
queue_as :default

def perform
Org.all.each do |org|
ScrapeNewEventsJob.perform_later(org) if org.fb?
end
end
end
17 changes: 15 additions & 2 deletions app/jobs/scrape_new_events_job.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
class ScrapeNewEventsJob < ApplicationJob
queue_as :default

def perform(org, access_token)
group = FbGraph2::Page.new(org.fbID).authenticate(access_token)
# This is a special use token that should only be used for serverside calls.
# This string value works in place of an "App Token," which is generated by
# facebook server-side when this is substituted for an access token. See
# https://developers.facebook.com/docs/facebook-login/access-tokens#apptokens
def application_token
app_id = Rails.application.secrets.fb_app_id
app_sec = Rails.application.secrets.fb_app_secret
"#{app_id}|#{app_sec}"
end

def perform(org)
logger.debug "Scraping events for #{org.name}"
group = FbGraph2::Page.new(org.fbID).authenticate(application_token)
added_events = 0
group.events.each do |event|
evt = event.fetch(fields: 'name,category,description,place,start_time,end_time,id')
Expand All @@ -11,6 +22,7 @@ def perform(org, access_token)
added_events += 1
end
end
logger.debug "Added #{added_events} events for #{org.name}"
end

def create_event_for_org(org, event)
Expand All @@ -23,6 +35,7 @@ def create_event_for_org(org, event)
lat = place['location']['latitude']
lon = place['location']['longitude']
end
logger.debug "Creating event #{event.name} for #{org.name}"
new_event = Event.new(
title: event.name, start_date: event.start_time,
category: event.raw_attributes['category'],
Expand Down
28 changes: 14 additions & 14 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@
ActiveRecord::Schema.define(version: 20170824010210) do

create_table "events", force: :cascade do |t|
t.string "title", null: false
t.datetime "start_date", null: false
t.datetime "end_date", null: false
t.datetime "event_date", null: false
t.boolean "all_day", default: false, null: false
t.string "title"
t.datetime "start_date"
t.datetime "end_date"
t.datetime "event_date"
t.boolean "all_day"
t.string "description"
t.string "location", null: false
t.string "location"
t.float "location_lat"
t.float "location_lon"
t.string "category", null: false
t.string "category"
t.string "fbID"
t.boolean "twentyone", default: false, null: false
t.boolean "twentyone"
t.boolean "recurring"
t.string "recurrence_freq"
t.integer "recurrence_amt"
Expand All @@ -39,7 +39,7 @@
t.string "name", null: false
t.string "bio"
t.string "fbID"
t.string "category", null: false
t.string "category"
t.string "website"
t.string "photo_url"
t.datetime "created_at", null: false
Expand Down Expand Up @@ -71,11 +71,11 @@

create_table "users", force: :cascade do |t|
t.string "provider"
t.string "uid", null: false
t.string "full_name", null: false
t.string "first_name", null: false
t.string "last_name", null: false
t.string "email", null: false
t.string "uid"
t.string "full_name"
t.string "first_name"
t.string "last_name"
t.string "email"
t.string "image_url"
t.boolean "admin", default: false, null: false
t.datetime "created_at", null: false
Expand Down
7 changes: 7 additions & 0 deletions test/jobs/scrape_events_for_all_orgs_job_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class ScrapeEventsForAllOrgsJobTest < ActiveJob::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 810371f

Please sign in to comment.