diff --git a/Gemfile b/Gemfile index 64c9945b..8f74fb75 100644 --- a/Gemfile +++ b/Gemfile @@ -121,3 +121,4 @@ gem 'discord-notifier' gem 'bootsnap', require: false gem 'thwait' gem 'e2mmap' +gem 'patreon' diff --git a/Gemfile.lock b/Gemfile.lock index 4bc25319..c2e85aa2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1141,6 +1141,7 @@ GEM jquery-ui-rails (6.0.1) railties (>= 3.2.16) json (2.3.0) + json-api-vanilla (1.0.2) jwt (2.2.1) kaminari (1.1.1) activesupport (>= 4.1.0) @@ -1225,6 +1226,9 @@ GEM parallel (1.19.1) parser (2.6.5.0) ast (~> 2.4.0) + patreon (0.5.0) + json-api-vanilla (~> 1.0.1) + rack pg (1.1.4) pry (0.12.2) coderay (~> 1.1.0) @@ -1494,6 +1498,7 @@ DEPENDENCIES omniauth-tumblr paper_trail paperclip + patreon pg pry-byebug rack-cors diff --git a/app/models/patreon.rb b/app/models/patreon.rb new file mode 100644 index 00000000..91619476 --- /dev/null +++ b/app/models/patreon.rb @@ -0,0 +1,3 @@ +class Patreon < ApplicationRecord + belongs_to :user +end diff --git a/app/services/sync_patreons.rb b/app/services/sync_patreons.rb new file mode 100644 index 00000000..4ed996f1 --- /dev/null +++ b/app/services/sync_patreons.rb @@ -0,0 +1,57 @@ +require 'patreon' + +class SyncPatreons + def self.perform + # client_id = ENV['PATREON_CLIENT_ID'] + # client_secret = ENV['PATREON_CLIENT_SECRET'] + # refresh_token = ENV['PATREON_REFRESH_TOKEN'] + # redirect_uri = ENV['PATREON_REDIRECT_URI'] + # + # oauth_client = Patreon::OAuth.new(client_id, client_secret) + # tokens = oauth_client.refresh_token(refresh_token, redirect_uri) + # puts tokens + # access_token = tokens['access_token'] + access_token = '2FRdFfQpYGiEXpFt3CL0guzYLSkPqok9udTx2_6N7T8' + + # Fetching basic data + api_client = Patreon::API.new(access_token) + + campaign_response = api_client.fetch_campaign() + campaign_id = campaign_response.data[0].id + + # Fetching all pledges + all_pledges = [] + cursor = nil + while true do + page_response = api_client.fetch_page_of_pledges(campaign_id, { :count => 25, :cursor => cursor }) + all_pledges += page_response.data + next_page_link = page_response.links[page_response.data]['next'] + if next_page_link + parsed_query = CGI::parse(next_page_link) + cursor = parsed_query['page[cursor]'][0] + else + break + end + end + + # Mapping to all patrons. Feel free to customize as needed. + # As with all standard Ruby objects, (pledge.methods - Object.methods) will list the available attributes and relationships + # puts all_pledges.map{ |pledge| + # pledge.inspect + # # return { + # # full_name: pledge.patron.full_name, + # # amount_cents: pledge.amount_cents + # # } + # } + all_pledges.each do |pledge| + full_name = pledge.patron.full_name + amount_cents = pledge.amount_cents + patreon_id = pledge.patron.id + + patreon = Patreon.find_or_create_by full_name: full_name + patreon.amount_cents = amount_cents + patreon.patreon_id = patreon_id + patreon.save! + end + end +end diff --git a/app/workers/sync_patreons_worker.rb b/app/workers/sync_patreons_worker.rb new file mode 100644 index 00000000..3fc3208e --- /dev/null +++ b/app/workers/sync_patreons_worker.rb @@ -0,0 +1,7 @@ +class SyncPatreonsWorker < ActiveJob::Base + queue_as :default + + def perform + SyncPatreons.perform + end +end diff --git a/config/sidekiq.yml b/config/sidekiq.yml index 09796eb4..8ede9013 100644 --- a/config/sidekiq.yml +++ b/config/sidekiq.yml @@ -13,5 +13,8 @@ schedule_monitor: every: 1m class: ScheduleMonitorWorker + sync_patreons: + every: 24h + class: SyncPatreonsWorker production: :concurrency: 5 diff --git a/db/migrate/20210630064951_create_patreons.rb b/db/migrate/20210630064951_create_patreons.rb new file mode 100644 index 00000000..cc43be78 --- /dev/null +++ b/db/migrate/20210630064951_create_patreons.rb @@ -0,0 +1,15 @@ +class CreatePatreons < ActiveRecord::Migration[5.0] + def change + create_table :patreons do |t| + t.integer :patreon_id, null: false + t.string :full_name, null: false + t.integer :amount_cents, null: false + t.references :user + + t.timestamps + end + + add_index :patreons, [:full_name, :id], unique: true + add_index :patreons, [:patreon_id, :id], unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 05c60453..e9c09fe0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20210606145058) do +ActiveRecord::Schema.define(version: 20210630064951) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -117,6 +117,18 @@ t.index ["user_id"], name: "index_microtexts_on_user_id", using: :btree end + create_table "patreons", force: :cascade do |t| + t.integer "patreon_id", null: false + t.string "full_name", null: false + t.integer "amount_cents", null: false + t.integer "user_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["full_name", "id"], name: "index_patreons_on_full_name_and_id", unique: true, using: :btree + t.index ["patreon_id", "id"], name: "index_patreons_on_patreon_id_and_id", unique: true, using: :btree + t.index ["user_id"], name: "index_patreons_on_user_id", using: :btree + end + create_table "plans", force: :cascade do |t| t.decimal "price" t.datetime "created_at"