mirror of
https://github.com/thewesker/ebooks_example.git
synced 2025-12-23 05:41:13 -05:00
More Cloudinary work
Move Cloudinary require/calls to boodoo.rb Add upload_archive, upload_model, fetch_archive, fetch_model rename get_archive! to update_archive! TODO: - Learn how to load model from content instead of File.open - Come up with sensible check/update logic
This commit is contained in:
52
boodoo.rb
52
boodoo.rb
@@ -1,4 +1,6 @@
|
||||
require 'twitter_ebooks'
|
||||
require 'cloudinary'
|
||||
|
||||
include Ebooks
|
||||
|
||||
module Ebooks::Boodoo
|
||||
@@ -162,6 +164,10 @@ class Ebooks::Boodoo::BoodooBot < Ebooks::Bot
|
||||
end
|
||||
end
|
||||
|
||||
def block(*args)
|
||||
twitter.block(*args)
|
||||
end
|
||||
|
||||
def has_model?
|
||||
File.exists? @model_path
|
||||
end
|
||||
@@ -170,14 +176,10 @@ class Ebooks::Boodoo::BoodooBot < Ebooks::Bot
|
||||
File.exists? @archive_path
|
||||
end
|
||||
|
||||
def get_archive!
|
||||
def update_archive!
|
||||
@archive = Archive.new(@original, @archive_path, make_client).sync
|
||||
end
|
||||
|
||||
def block(*args)
|
||||
twitter.block(*args)
|
||||
end
|
||||
|
||||
def make_model!
|
||||
log "Updating model: #{@model_path}"
|
||||
Ebooks::Model.consume(@archive_path).save(@model_path)
|
||||
@@ -185,6 +187,46 @@ class Ebooks::Boodoo::BoodooBot < Ebooks::Bot
|
||||
@model = Ebooks::Model.load(@model_path)
|
||||
end
|
||||
|
||||
def upload_archive
|
||||
log "Uploading JSON archive ~~TO THE CLOUD~~..."
|
||||
res = Cloudinary::Uploader.upload(@archive_path, :resource_type=>:raw, :public_id=>File.basename(@archive_path) )
|
||||
log "Upload complete!"
|
||||
@archive_url = Cloudinary::Utils.cloudinary_url(File.basename(@archive_path), :resource_type=>:raw)
|
||||
end
|
||||
|
||||
def upload_model
|
||||
log "Uploading bot model ~~TO THE CLOUD~~..."
|
||||
res = Cloudinary::Uploader.upload(@model_path, :resource_type=>:raw, :public_id=>File.basename(@model_path) )
|
||||
log "Upload complete!"
|
||||
@model_url = Cloudinary::Utils.cloudinary_url(File.basename(@model_path), :resource_type=>:raw)
|
||||
end
|
||||
|
||||
def fetch_archive
|
||||
log "Fetching JSON archive ~~~FROM THE CLOUD~~~"
|
||||
archive_url = Cloudinary::Utils.cloudinary_url(File.basename(@archive_path), :resource_type=>:raw)
|
||||
archive_content = Cloudinary::Downloader.download(archive_url))
|
||||
if archive_content.empty?
|
||||
log "WARNING: JSON archive not found ~~~IN THE CLOUD~~~"
|
||||
nil
|
||||
else
|
||||
log "Download complete!"
|
||||
archive_content
|
||||
end
|
||||
end
|
||||
|
||||
def fetch_model
|
||||
log "Fetching bot model ~~~FROM THE CLOUD~~~"
|
||||
model_url = Cloudinary::Utils.cloudinary_url(File.basename(@model_path), :resouce_type=>:raw)
|
||||
model_content = Cloudinary::Downloader.download(model_url)
|
||||
if model_content.empty?
|
||||
log "WARNING: bot model not found ~~~IN THE CLOUD~~~"
|
||||
nil
|
||||
else
|
||||
log "Download complete!"
|
||||
model_content
|
||||
end
|
||||
end
|
||||
|
||||
def can_run?
|
||||
missing_fields.empty?
|
||||
end
|
||||
|
||||
6
bots.rb
6
bots.rb
@@ -1,14 +1,12 @@
|
||||
require 'twitter_ebooks'
|
||||
require_relative 'boodoo'
|
||||
require 'dotenv'
|
||||
require 'cloudinary'
|
||||
|
||||
include Ebooks::Boodoo
|
||||
|
||||
# Read defaults and lay env vars on top:
|
||||
SETTINGS = Dotenv.load('secrets.env').merge(ENV)
|
||||
|
||||
|
||||
# Information about a particular Twitter user we know
|
||||
class UserInfo
|
||||
attr_reader :username
|
||||
@@ -68,7 +66,7 @@ class BoodooBot
|
||||
@model_path = "model/#{@original}.model"
|
||||
|
||||
if can_run?
|
||||
get_archive!
|
||||
update_archive!
|
||||
make_model!
|
||||
else
|
||||
missing_fields.each {|missing|
|
||||
@@ -105,7 +103,7 @@ class BoodooBot
|
||||
|
||||
scheduler.interval @refresh_model_interval do
|
||||
log "Refreshing archive/model..."
|
||||
get_archive!
|
||||
update_archive!
|
||||
make_model!
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user