diff --git a/Rakefile b/Rakefile index 8e0ff0e..f0db9f7 100644 --- a/Rakefile +++ b/Rakefile @@ -2,12 +2,29 @@ require "dotenv" require "hanami/rake_tasks" +require "down" namespace :blog do task :load_environment do Dotenv.load("/home/blog/current/.env.production") end + task :backfill_movie_imdb_ids => ["blog:load_environment"] do + require "hanami/prepare" + + movie_repo = Admin::Container["repos.movie_repo"] + + movies = movie_repo.listing + + movies.each do |movie| + page = Down.download(movie.url) + match = page.read.match(/href=".+title\/(tt\d+)\/maindetails"/) + imdb_id = match[1] + + movie_repo.update(movie.id, {imdb_id: imdb_id}) + end + end + task :load_from_letterboxd => ["blog:load_environment"] do require "hanami/prepare" require "scraperd" @@ -15,15 +32,13 @@ namespace :blog do client = Scraperd::Base.new activities = client.fetch('dnitza') - repo = Adamantium::Container["repos.movie_repo"] + create_command = Admin::Container["commands.movies.create"] activities.each do |activity| title = CGI.unescapeHTML(activity.title) - next if repo.by_title_and_year(title: title, year: activity.year) - - repo.create({ + create_command.({ title: title, year: activity.year, url: activity.film_link, diff --git a/db/migrate/20230513012518_add_imdb_id_to_movies.rb b/db/migrate/20230513012518_add_imdb_id_to_movies.rb new file mode 100644 index 0000000..2bd3e6f --- /dev/null +++ b/db/migrate/20230513012518_add_imdb_id_to_movies.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +ROM::SQL.migration do + change do + alter_table :movies do + add_column :imdb_id, :text + end + end +end diff --git a/slices/admin/commands/movies/create.rb b/slices/admin/commands/movies/create.rb new file mode 100644 index 0000000..d901826 --- /dev/null +++ b/slices/admin/commands/movies/create.rb @@ -0,0 +1,27 @@ +require "down" + +module Admin + module Commands + module Movies + class Create + + include Deps["repos.movie_repo"] + + def call(movie) + repo = Adamantium::Container["repos.movie_repo"] + + next if repo.by_title_and_year(title: title, year: activity.year) + + page = Down.download(activity.film_link) + match = page.read.match(/href=".+title\/(tt\d+)\/maindetails"/) + imdb_id = match[1] + + movie = movie.merge(imdb_id: imdb_id) if imdb_id + + repo.create(movie) + end + end + end + end + end +end \ No newline at end of file diff --git a/slices/admin/repos/movie_repo.rb b/slices/admin/repos/movie_repo.rb new file mode 100644 index 0000000..599d287 --- /dev/null +++ b/slices/admin/repos/movie_repo.rb @@ -0,0 +1,19 @@ +module Admin + module Repos + class MovieRepo < Adamantium::Repo[:movies] + commands :create + + def by_title_and_year(title:, year:) + movies.where(title: title, year: year).one + end + + def listing + movies.order(Sequel.lit("year desc")).to_a + end + + def update(id, attrs) + movies.where(id: id).update(attrs) + end + end + end +end \ No newline at end of file