diff --git a/shard.yml b/shard.yml index c068ec7..4f48538 100644 --- a/shard.yml +++ b/shard.yml @@ -16,6 +16,6 @@ dependencies: sqlite3: github: crystal-lang/crystal-sqlite3 -crystal: 0.24.1 +crystal: 1.0.0 license: MIT diff --git a/src/joann-pupper-bot.cr b/src/joann-pupper-bot.cr index 39be481..27f54c8 100644 --- a/src/joann-pupper-bot.cr +++ b/src/joann-pupper-bot.cr @@ -1,5 +1,5 @@ require "./joann-pupper-bot/*" -require "logger" +require "log" require "telepathy" require "time" require "sqlite3" @@ -12,14 +12,22 @@ CHATID_DANIEL = 220888832_i64 DATABASE_URL = "sqlite3://./data.sqlite" BOT_TOKEN = "599474797:AAEmjQNO32uqurI16blS9FT4OoO7GdUZ6h0" EXTENSIONS = ["png", "jpeg", "jpg"] -LOGGER = Logger.new(STDOUT) +LOGGER = Log.for("joann-pupper-bot") class BotConfiguration - JSON.mapping( - subreddits: Array(String), - send_cron: String, - refresh_cron: String, - recepients: Array(Int64)) + include JSON::Serializable + + @[JSON::Field(key: "subreddits")] + property subreddits : Array(String) + + @[JSON::Field(key: "send_cron")] + property send_cron : String + + @[JSON::Field(key: "refresh_cron")] + property refresh_cron : String + + @[JSON::Field(key: "recepients")] + property recepients : Array(Int64) def initialize @subreddits = [ "rarepuppers" ] @@ -75,12 +83,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" 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 end 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 @telegram_bot.send_photo(chatid, url, title) end @@ -93,13 +101,13 @@ class PupperBot def update_database 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 end posts = response.data.posts_matching { |post| EXTENSIONS.any? { |it| post.url.ends_with? it } } 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 @db.exec "insert into posts(title, url) values(?, ?)", post.title, post.url rescue diff --git a/src/joann-pupper-bot/reddit.cr b/src/joann-pupper-bot/reddit.cr index 374dfb9..2622f83 100644 --- a/src/joann-pupper-bot/reddit.cr +++ b/src/joann-pupper-bot/reddit.cr @@ -1,24 +1,40 @@ require "http/client" -require "json" +require "json" class RedditWrapper(T) - JSON.mapping( - kind: String, - data: T) + include JSON::Serializable + + @[JSON::Field(key: "kind")] + property kind : String + + @[JSON::Field(key: "data")] + property data : T end class RedditChild - JSON.mapping( - url: String, - name: String, - title: String) + include JSON::Serializable + + @[JSON::Field(key: "url")] + property url : String + + @[JSON::Field(key: "name")] + property name : String + + @[JSON::Field(key: "title")] + property title : String end class RedditResponse - JSON.mapping( - modhash: String, - dist: Int32, - children: Array(RedditWrapper(RedditChild))) + include JSON::Serializable + + @[JSON::Field(key: "modhash")] + property modhash : String + + @[JSON::Field(key: "dist")] + property dist : Int32 + + @[JSON::Field(key: "children")] + property children : Array(RedditWrapper(RedditChild)) 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"