From 587cc2fb554fce8751c5bcfadcd3d35fc1ffdcac Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Wed, 7 Dec 2022 17:19:22 -0500 Subject: [PATCH] Added some preliminary database stuff; added a script to mock adding a post to the DB --- .gitignore | 2 ++ Gemfile | 4 ++++ Gemfile.lock | 15 +++++++++++++++ add_post.rb | 23 +++++++++++++++++++++++ data/defaults.yaml | 4 ++++ db/migrations/0001_add_posts_table.rb | 18 ++++++++++++++++++ lib/models/post.rb | 5 +++++ vulture.rb | 17 +++++++++++++++++ 8 files changed, 88 insertions(+) create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 add_post.rb create mode 100644 db/migrations/0001_add_posts_table.rb create mode 100644 lib/models/post.rb diff --git a/.gitignore b/.gitignore index d6aa672..58e2b3e 100644 --- a/.gitignore +++ b/.gitignore @@ -56,3 +56,5 @@ build-iPhoneSimulator/ # Used by RuboCop. Remote config files pulled in from inherit_from directive. # .rubocop-https?--* +# Local database storage +data/vulture.db diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..21446cb --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org/' + +gem 'sequel', '~> 5.63' +gem 'sqlite3', '~> 1.5' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..e3cf193 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,15 @@ +GEM + remote: https://rubygems.org/ + specs: + sequel (5.63.0) + sqlite3 (1.5.4-x64-mingw-ucrt) + +PLATFORMS + x64-mingw-ucrt + +DEPENDENCIES + sequel (~> 5.63) + sqlite3 (~> 1.5) + +BUNDLED WITH + 2.3.7 diff --git a/add_post.rb b/add_post.rb new file mode 100644 index 0000000..9d662a4 --- /dev/null +++ b/add_post.rb @@ -0,0 +1,23 @@ +#!/usr/bin/env ruby + +require 'sequel' +require 'sqlite3' + +require_relative 'lib/config.rb' + +# Load the Sequel timestamps plugin +Sequel::Model.plugin :timestamps + +# Load configuration +conf = Config.new(File.join(__dir__, 'data/defaults.yaml')) + +# Initialize Sequel gem for database actions +DB = Sequel.connect(adapter: conf.get('database.adapter'), database: conf.get('database.database')) +# Load models +require_relative 'lib/models/post.rb' + +# Create new time object, three minutes in the future +t = Time.now + (3 * 60) + +# Insert post +Post.create(text: ARGV[0], publish_at: t) diff --git a/data/defaults.yaml b/data/defaults.yaml index fda6a21..7eddeb6 100644 --- a/data/defaults.yaml +++ b/data/defaults.yaml @@ -1,2 +1,6 @@ vulture: run_interval: 1 + +database: + adapter: 'sqlite' + database: 'data/vulture.db' diff --git a/db/migrations/0001_add_posts_table.rb b/db/migrations/0001_add_posts_table.rb new file mode 100644 index 0000000..d901f8b --- /dev/null +++ b/db/migrations/0001_add_posts_table.rb @@ -0,0 +1,18 @@ +Sequel.migration do + + up do + create_table(:posts) do + primary_key :id + String :text, null: false + DateTime :publish_at + Boolean :published, default: false + DateTime :created_at + DateTime :updated_at + end + end + + down do + drop_table(:posts) + end + +end diff --git a/lib/models/post.rb b/lib/models/post.rb new file mode 100644 index 0000000..61f2f13 --- /dev/null +++ b/lib/models/post.rb @@ -0,0 +1,5 @@ +class Post < Sequel::Model + + + +end diff --git a/vulture.rb b/vulture.rb index a2eaa41..ee92a34 100644 --- a/vulture.rb +++ b/vulture.rb @@ -1,6 +1,8 @@ #!/usr/bin/env ruby require 'logger' +require 'sequel' +require 'sqlite3' require_relative 'lib/config.rb' @@ -11,11 +13,26 @@ conf = Config.new(File.join(__dir__, 'data/defaults.yaml')) logger = Logger.new(STDOUT) logger.level = Logger::INFO +# Load the Sequel timestamps plugin +Sequel::Model.plugin :timestamps +# Initialize Sequel gem for database actions +DB = Sequel.connect(adapter: conf.get('database.adapter'), database: conf.get('database.database')) +# Load models +require_relative 'lib/models/post.rb' + # Main program loop while true # Let the user know something is going on logger.info('Checking for new social media posts to make public...') + # Search for posts that are due to be published + to_post = Post.dataset.where(published: false).where{publish_at < Time.now} + # If we have posts to handle, let's do it! + to_post.each { |post| + puts "Posting tweet: #{post.text}" + post.update(published: true) + } + # Wait for the specified wait period sleep(conf.get('vulture.run_interval') * 60) end