Skip to content

Commit

Permalink
Merge branch 'zine'
Browse files Browse the repository at this point in the history
  • Loading branch information
jphager2 committed Oct 28, 2018
2 parents e07a619 + decdb32 commit 8d5acd3
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby-2.4.0
ruby-2.5.3
38 changes: 19 additions & 19 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
PATH
remote: .
specs:
my_manga (1.1.6)
activerecord (~> 5.0)
my_manga (2.0.0)
activerecord (~> 5.2)
hanami-cli (~> 0.1.0)
mangdown (~> 0.17.5)
pg (~> 0.21)
mangdown (~> 0.18.0)
pg (~> 1.1)

GEM
remote: https://rubygems.org/
specs:
activemodel (5.2.0)
activesupport (= 5.2.0)
activerecord (5.2.0)
activemodel (= 5.2.0)
activesupport (= 5.2.0)
activemodel (5.2.1)
activesupport (= 5.2.1)
activerecord (5.2.1)
activemodel (= 5.2.1)
activesupport (= 5.2.1)
arel (>= 9.0)
activesupport (5.2.0)
activesupport (5.2.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
Expand All @@ -32,27 +32,27 @@ GEM
hanami-cli (0.1.1)
concurrent-ruby (~> 1.0)
hanami-utils (~> 1.1)
hanami-utils (1.2.0)
hanami-utils (1.3.0)
concurrent-ruby (~> 1.0)
transproc (~> 1.0)
i18n (1.0.1)
i18n (1.1.1)
concurrent-ruby (~> 1.0)
mangdown (0.17.5)
mangdown (0.18.0)
addressable
nokogiri (~> 1.6)
nokogiri (~> 1.8)
ruby-filemagic
rubyzip (~> 1.1)
typhoeus (~> 0.7)
mini_portile2 (2.3.0)
minitest (5.10.3)
nokogiri (1.8.3)
nokogiri (1.8.5)
mini_portile2 (~> 2.3.0)
parallel (1.12.0)
parser (2.4.0.0)
ast (~> 2.2)
pg (0.21.0)
pg (1.1.3)
powerpack (0.1.1)
public_suffix (3.0.2)
public_suffix (3.0.3)
rainbow (2.2.2)
rake
rake (12.0.0)
Expand All @@ -65,7 +65,7 @@ GEM
unicode-display_width (~> 1.0, >= 1.0.1)
ruby-filemagic (0.7.2)
ruby-progressbar (1.8.1)
rubyzip (1.2.1)
rubyzip (1.2.2)
thread_safe (0.3.6)
transproc (1.0.2)
typhoeus (0.8.0)
Expand All @@ -83,4 +83,4 @@ DEPENDENCIES
rubocop

BUNDLED WITH
1.15.4
1.17.1
4 changes: 4 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ namespace :db do
MyManga::DB.migrate
end

task :restore do
MyManga::DB.restore
end

task :dump do
MyManga::DB.dump
end
Expand Down
6 changes: 3 additions & 3 deletions db/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ default: &default
host: <%= ENV["PG_HOST"] %>
reconnect: false
# Set these in .bashrc or .bash_profile
username: <%= ENV["PG_USER"] %>
username: <%= ENV["PG_USER"] %>
password: <%= ENV["PG_PASS"] %>

production:
<<: *default
database: my_manga_library_database
database: my_manga

test:
<<: *default
database: my_manga_library_test_database
database: my_manga_test
28 changes: 16 additions & 12 deletions db/connection.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
module MyManga
class DB
module DB
MIGRATION_PATH = File.expand_path('../migrate', __FILE__)
CONFIG_PATH = File.expand_path('../config.yml', __FILE__)
CONFIG = YAML.load(ERB.new(File.read(CONFIG_PATH)).result)[MyManga.env]

def self.establish_connection
module_function

def establish_connection
ActiveRecord::Base.establish_connection(CONFIG)
end
end

def self.establish_base_connection
def establish_base_connection
ActiveRecord::Base.establish_connection(
CONFIG.merge(
{
Expand All @@ -19,26 +21,28 @@ def self.establish_base_connection
)
end

def self.connection
def connection
ActiveRecord::Base.connection
end

def self.create_database
def create_database
connection.create_database(CONFIG["database"], CONFIG)
end

def self.drop_database
def drop_database
connection.drop_database(CONFIG["database"])
end

def self.migrate
ActiveRecord::Migrator.migrate(MIGRATION_PATH, nil)
def migrate
ActiveRecord::MigrationContext.new(MIGRATION_PATH).migrate
end

def restore
system("pg_restore --verbose --clean --no-acl --no-owner -d #{CONFIG["database"]} #{File.expand_path(__dir__)}/latest.dump")
end

def self.dump
def dump
system("pg_dump -Fc #{CONFIG["database"]} > #{File.expand_path(__dir__)}/latest.dump")
end
end
end


3 changes: 3 additions & 0 deletions db/migrate/002_add_zine_to_manga.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class AddZineToManga < ActiveRecord::Migration[5.2]
add_column :manga, :zine, :boolean, null: false, default: false
end
12 changes: 12 additions & 0 deletions lib/my_manga.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ def names
Manga.pluck(:name)
end

def add_to_zine(names)
Manga.where(name: names).update(zine: true)
end

def remove_from_zine(names)
Manga.where(name: names).update(zine: false)
end

def zine
Manga.where(zine: true)
end

def add(uri)
return if Manga.find_by_uri(uri)

Expand Down
2 changes: 2 additions & 0 deletions lib/my_manga/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require_relative './mark'
require_relative './env'
require_relative './m_clean_up'
require_relative './zine'

module MyManga
module CLI
Expand All @@ -37,6 +38,7 @@ def call(*)
register 'mark', Mark
register 'env', Env, aliases: %w[-e --environment]
register 'm:clean_up', MCleanUp, aliases: %w[purge]
register 'zine', Zine
end
end
end
11 changes: 7 additions & 4 deletions lib/my_manga/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ def list(names)
puts 'Manga list'
puts '=========='
print pad('Name', column_width)
puts 'Chapters read/total (unread)'
puts 'Zine Chapters read/total (unread)'

zine_manga = MyManga.zine
names.sort.each do |name|
manga = MyManga[name]
read = manga.read_count
total = manga.total_count
unread = total - read
zine = zine_manga.include?(manga) ? 'zine' : ' '

print pad(name, column_width)
puts "#{read}/#{total} (#{unread}) #{manga.uri}"
puts "#{zine} #{read}/#{total} (#{unread}) #{manga.uri}"
end
end

Expand All @@ -39,12 +41,13 @@ def list_detail(name)
read = manga.read_count
total = manga.total_count
unread = total - read
zine = MyManga.zine.include?(manga) ? 'zine' : ' '

puts header
puts '=' * header.length
print pad('Name', name.length)
puts 'Chapters read/total (unread)'
puts "#{name} #{read}/#{total} (#{unread}) #{manga.uri}"
puts 'Zine Chapters read/total (unread)'
puts "#{name} #{zine} #{read}/#{total} (#{unread}) #{manga.uri}"
puts
puts 'Chapters Read'
puts '-------------'
Expand Down
2 changes: 1 addition & 1 deletion lib/my_manga/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module MyManga
VERSION = '1.1.6'
VERSION = '2.0.0'
end
112 changes: 112 additions & 0 deletions lib/my_manga/zine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# frozen_string_literal: true

require 'fileutils'

module MyManga
module CLI
module Commands
# See desc
class Zine < MyManga::CLI::Command
desc 'Build a new zine'
argument :names, desc: 'Manga names (comma separated)'

option :add,
type: :boolean,
default: false,
desc: 'Add manga to zine'
option :remove,
type: :boolean,
default: false,
desc: 'Remove manga from zine'
option :size,
default: '10',
desc: 'Number of chapters to include in the zine'
option :filename,
desc: 'Filename for the zine, (default `zine-<timestamp>-<hash of chapters included>`)'

TMP_DIR = File.expand_path('../../tmp', __dir__)

def call(names: nil, **options)
names = manga_names(names)
filename = options.fetch(:filename) { nil }
size = options.fetch(:size).to_i

if options[:add] && options[:remove]
puts "--add and --remove are mutually exclusive"
exit 1
end

if options[:add]
MyManga.add_to_zine(names)
puts %("#{names.join(', ')}" added to the zine!)
elsif options[:remove]
MyManga.remove_from_zine(names)
puts %("#{names.join(', ')}" removed from the zine!)
else
publish(filename, size)
end
end

private

def publish(filename, size)
FileUtils.rm_r(TMP_DIR) if Dir.exist?(TMP_DIR)
Dir.mkdir(TMP_DIR)

zine = zine_content(size)
serialized_name = []
zine.each do |chapter|
serialized_name << chapter.id
chapter.to_md.download_to(TMP_DIR)
MyManga.read!(chapter.manga, [chapter.number])
end

serialized_name = serialized_name.join.to_i.to_s(32)
filename ||= "zine-#{Time.now.to_i}-#{serialized_name}"

dir = File.join(MyManga.download_dir, filename)

cbz(dir)

FileUtils.rm_r(TMP_DIR)

puts "Pushlished a new zine (#{filename}) in #{MyManga.download_dir}"
end

def zine_content(chapter_count)
manga = MyManga.zine
chapters = Chapter
.unread
.where(manga_id: manga.map(&:id))
.order(:number)
.group_by(&:manga)
.values
.sort_by(&:length)
.reverse

return if chapters.empty?

zine = chapters.first.zip(*chapters.drop(1)).flatten.compact
zine.first(chapter_count).sort_by do |chapter|
[chapter.manga_id, chapter.number]
end
end

def cbz(dir)
pages = Dir["#{TMP_DIR}/**/*.*"]

Dir.mkdir(dir) unless Dir.exist?(dir)

pages.each do |page|
filename = File.basename(page)
FileUtils.cp(page, File.join(dir, filename))
end

Mangdown::CBZ.one(dir, false)

FileUtils.rm_r(dir)
end
end
end
end
end
7 changes: 3 additions & 4 deletions my_manga.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ Gem::Specification.new do |s|
s.executables = ['my_manga']
s.license = 'MIT'

s.add_runtime_dependency 'activerecord', '~> 5.0'
s.add_runtime_dependency 'activerecord', '~> 5.2'
s.add_runtime_dependency 'hanami-cli', '~> 0.1.0'
s.add_runtime_dependency 'mangdown', '~> 0.17.5'
s.add_runtime_dependency 'pg', '~> 0.21'
s.add_runtime_dependency 'mangdown', '~> 0.18.0'
s.add_runtime_dependency 'pg', '~> 1.1'

s.add_development_dependency 'rubocop'
s.add_development_dependency 'minitest'
end

Loading

0 comments on commit 8d5acd3

Please sign in to comment.