Compare commits

..

1 Commits

Author SHA1 Message Date
8d90b052a0 Load configuration from YAML file. 2020-04-25 19:34:12 -07:00
5 changed files with 28 additions and 78 deletions

View File

@@ -1,22 +0,0 @@
version: 2.0
shards:
cron_parser:
git: https://github.com/kostya/cron_parser.git
version: 0.4.0
cron_scheduler:
git: https://github.com/kostya/cron_scheduler.git
version: 0.4.0
db:
git: https://github.com/crystal-lang/crystal-db.git
version: 0.14.0
sqlite3:
git: https://github.com/crystal-lang/crystal-sqlite3.git
version: 0.22.0
telepathy:
git: https://dev.danilafe.com/Crystal-Bots/telepathy
version: 0.2.0

View File

@@ -16,6 +16,6 @@ dependencies:
sqlite3: sqlite3:
github: crystal-lang/crystal-sqlite3 github: crystal-lang/crystal-sqlite3
crystal: 1.0.0 crystal: 0.24.1
license: MIT license: MIT

View File

@@ -1,18 +1,18 @@
require "log" require "logger"
require "telepathy" require "telepathy"
require "time" require "time"
require "sqlite3" require "sqlite3"
require "cron_scheduler" require "cron_scheduler"
EXTENSIONS = ["png", "jpeg", "jpg"] EXTENSIONS = ["png", "jpeg", "jpg"]
LOGGER = Log.for("joann-pupper-bot") LOGGER = Logger.new(STDOUT)
class PupperBot class PupperBot
@db : DB::Database @db : DB::Database
def initialize(@configuration : BotConfiguration) def initialize(@configuration : BotConfiguration)
@telegram_bot = Telepathy::Bot.new @configuration.token @telegram_bot = Telepathy::Bot.new configuration.token
@db = DB.open "sqlite3://#{@configuration.database}" @db = DB.open "sqlite3://#{configuration.database}"
initialize_db initialize_db
update_database update_database
initialize_timers initialize_timers
@@ -49,12 +49,12 @@ class PupperBot
unsent_query = "select id, title, url from posts where not exists (select * from recepient_posts where recepient=? and post=id) limit 1" unsent_query = "select id, title, url from posts where not exists (select * from recepient_posts where recepient=? and post=id) limit 1"
unless to_send = @db.query_one? unsent_query, chatid, as: { Int64, String, String } unless to_send = @db.query_one? unsent_query, chatid, as: { Int64, String, String }
LOGGER.info { "Unable to find a post to send to #{chatid}." } LOGGER.info "Unable to find a post to send to #{chatid}."
return return
end end
id, title, url = to_send id, title, url = to_send
LOGGER.info { "Using URL #{url} for request from #{chatid}" } LOGGER.info "Using URL #{url} for request from #{chatid}"
@db.exec "insert into recepient_posts(recepient, post) values(?, ?)", chatid, id @db.exec "insert into recepient_posts(recepient, post) values(?, ?)", chatid, id
@telegram_bot.send_photo(chatid, url, title) @telegram_bot.send_photo(chatid, url, title)
end end
@@ -67,13 +67,13 @@ class PupperBot
def update_database def update_database
unless response = RedditResponse.from_subreddits(@configuration.subreddits) unless response = RedditResponse.from_subreddits(@configuration.subreddits)
LOGGER.info { "Unable to find more posts for the database" } LOGGER.info "Unable to find more posts for the database"
return return
end end
posts = response.data.posts_matching { |post| EXTENSIONS.any? { |it| post.url.ends_with? it } } posts = response.data.posts_matching { |post| EXTENSIONS.any? { |it| post.url.ends_with? it } }
posts.each do |post| posts.each do |post|
LOGGER.info { "Trying to save post #{post.title} #{post.url}" } LOGGER.info "Trying to save post #{post.title} #{post.url}"
begin begin
@db.exec "insert into posts(title, url) values(?, ?)", post.title, post.url @db.exec "insert into posts(title, url) values(?, ?)", post.title, post.url
rescue rescue

View File

@@ -1,23 +1,11 @@
require "yaml" require "yaml"
class BotConfiguration class BotConfiguration
include YAML::Serializable YAML.mapping(
token: String,
@[YAML::Field(key: "token")] database: { type: String, default: "./data.sqlite" },
property token : String subreddits: { type: Array(String), default: [ "rarepuppers" ] },
send_cron: { type: String, default: "*/30 8-21 * * *" },
@[YAML::Field(key: "database")] refresh_cron: { type: String, default: "*/15 * * * *" },
property database : String = "./data.sqlite" recipients: Array(Int64))
@[YAML::Field(key: "subreddits")]
property subreddits : Array(String) = ["rarepuppers"]
@[YAML::Field(key: "send_cron")]
property send_cron : String = "*/30 8-21 * * *"
@[YAML::Field(key: "refresh_cron")]
property refresh_cron : String = "*/15 * * * *"
@[YAML::Field(key: "recipients")]
property recipients : Array(Int64)
end end

View File

@@ -1,40 +1,24 @@
require "http/client" require "http/client"
require "json" require "json"
class RedditWrapper(T) class RedditWrapper(T)
include JSON::Serializable JSON.mapping(
kind: String,
@[JSON::Field(key: "kind")] data: T)
property kind : String
@[JSON::Field(key: "data")]
property data : T
end end
class RedditChild class RedditChild
include JSON::Serializable JSON.mapping(
url: String,
@[JSON::Field(key: "url")] name: String,
property url : String title: String)
@[JSON::Field(key: "name")]
property name : String
@[JSON::Field(key: "title")]
property title : String
end end
class RedditResponse class RedditResponse
include JSON::Serializable JSON.mapping(
modhash: String,
@[JSON::Field(key: "modhash")] dist: Int32,
property modhash : String children: Array(RedditWrapper(RedditChild)))
@[JSON::Field(key: "dist")]
property dist : Int32
@[JSON::Field(key: "children")]
property children : Array(RedditWrapper(RedditChild))
def self.from_subreddits(subreddits : Array(String)) def self.from_subreddits(subreddits : Array(String))
request_url = URI.new scheme: "https", host: "www.reddit.com", path: "/r/#{subreddits.join "+"}/hot.json", query: "limit=30" request_url = URI.new scheme: "https", host: "www.reddit.com", path: "/r/#{subreddits.join "+"}/hot.json", query: "limit=30"