Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP import patreons via API #112

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,4 @@ gem 'discord-notifier'
gem 'bootsnap', require: false
gem 'thwait'
gem 'e2mmap'
gem 'patreon'
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -1494,6 +1498,7 @@ DEPENDENCIES
omniauth-tumblr
paper_trail
paperclip
patreon
pg
pry-byebug
rack-cors
Expand Down
3 changes: 3 additions & 0 deletions app/models/patreon.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Patreon < ApplicationRecord
belongs_to :user
end
57 changes: 57 additions & 0 deletions app/services/sync_patreons.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions app/workers/sync_patreons_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class SyncPatreonsWorker < ActiveJob::Base
queue_as :default

def perform
SyncPatreons.perform
end
end
3 changes: 3 additions & 0 deletions config/sidekiq.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
schedule_monitor:
every: 1m
class: ScheduleMonitorWorker
sync_patreons:
every: 24h
class: SyncPatreonsWorker
production:
:concurrency: 5
15 changes: 15 additions & 0 deletions db/migrate/20210630064951_create_patreons.rb
Original file line number Diff line number Diff line change
@@ -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
14 changes: 13 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down