Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base setup for mangadex import; handling base cases #550

Open
wants to merge 6 commits into
base: the-future
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions app/services/mangadex_import.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
class MangadexImport
attr_reader :file_location

# ideally pass in location here?
def initialize
@file_location = 'tmp/mangadex_import/manga-batch-1-temp.ndjson'
end

def import!
each_mangadex_entry do |data, name, mal_id|
puts name

kitsu_id = kitsu_id_by_mal_id(mal_id) if mal_id.present?
kitsu_id = kitsu_id_by_name(name) if kitsu_id.blank?

row = Row.new(kitsu_data(kitsu_id), data)
row.create_or_update


toyhammered marked this conversation as resolved.
Show resolved Hide resolved

toyhammered marked this conversation as resolved.
Show resolved Hide resolved
end
end

def each_mangadex_entry
File.foreach(file_location) do |line|
line = JSON.parse(line)
name = line['title']['name']
mal_id = formatted_mal_id(line.dig('external_links', 'MyAnimeList'))

yield line, name, mal_id
end
end

def formatted_mal_id(url)
return nil if url.blank?

url.split('/').last
end

private

def kitsu_id_by_mal_id(mal_id)
Mapping.where(
external_site: 'myanimelist/manga',
external_id: mal_id
).first
end

def kitsu_id_by_name(name)
AlgoliaMediaIndex.search(
name,
filters: 'kind:manga AND NOT subtype:novel',
hitsPerPage: 3
).first.try(:id)
end

def kitsu_data(kitsu_id)
Manga.find_or_initialize_by(kitsu_id)
end
end
95 changes: 95 additions & 0 deletions app/services/mangadex_import/row.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
class MangadexImport
class Row
LANGUAGES = {
'Brazilian' => 'pt_br',
'Chinese (Simp)' => 'zh_Hans',
'English' => 'en',
'French' => 'fr',
'German' => 'de',
'Hebrew' => 'he',
'Hungarian' => 'hu',
'Indonesian' => 'id_in',
'Italian' => 'it',
'Japanese' => 'ja_jp',
'Korean' => 'ko',
'Malay' => 'ms',
'Spanish' => 'es',
'Thai' => 'th'
}.freeze

attr_reader :kitsu_data, :mangadex_data

def initialize(kitsu_data, mangadex_data)
@kitsu_data = kitsu_data
@mangadex_data = mangadex_data
end

def create_or_update
kitsu_generic_fields.each do |kitsu_field|
kitsu_data[kitsu_field] ||= public_send("mangadex_#{kitsu_field}")
end

kitsu_data.abbreviated_titles = mangadex_abbreviated_titles
kitsu_data.chapter_count = mangadex_chapter_count
kitsu_data.titles[kitsu_data.canonical_title] ||= mangadex_titles
end

def kitsu_generic_fields
%w[
age_rating canonical_title
end_date original_locale poster_image_file_name
serialization slug start_date subtype synopsis
volume_count
]
end

def mangadex_age_rating
mangadex['hentai'] ? 'R18' : nil
end

def mangadex_abbreviated_titles
kitsu_data.abbreviated_titles.concat(mangadex['alt_titles']).uniq
end

def mangadex_canonical_title
LANGUAGES[mangadex_original_locale]
end

def mangadex_chapter_count
[kitsu_data.chapter_count, mangadex['total_chapters']].max
end

def mangadex_original_locale
mangadex['title']['origin']
end

def mangadex_poster_image_file_name
mangadex['thumbnail']
end

def mangadex_serialization
nil
end

def mangadex_slug
mangadex['title']['slug']
end

def mangadex_subtype
'manga'
end

# TODO: do we need to sanitize?
def mangadex_synopsis
mangadex['description']
end

def mangadex_titles
mangadex['title']['name']
end

def mangadex_volume_count
nil
end
end
end