comicinfo-local 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +34 -0
- data/CODE_OF_CONDUCT.md +10 -0
- data/LICENSE.md +21 -0
- data/README.md +96 -0
- data/Rakefile +10 -0
- data/lib/comicinfo/local/comic_vine_url.rb +66 -0
- data/lib/comicinfo/local/errors.rb +26 -0
- data/lib/comicinfo/local/grand_comics_database_url.rb +51 -0
- data/lib/comicinfo/local/key.rb +9 -0
- data/lib/comicinfo/local/linked_issues.rb +39 -0
- data/lib/comicinfo/local/metron_url.rb +39 -0
- data/lib/comicinfo/local/record.rb +144 -0
- data/lib/comicinfo/local/recorder.rb +45 -0
- data/lib/comicinfo/local/response.rb +6 -0
- data/lib/comicinfo/local/saved_issue.rb +133 -0
- data/lib/comicinfo/local/search_key.rb +27 -0
- data/lib/comicinfo/local/store.rb +49 -0
- data/lib/comicinfo/local/version.rb +5 -0
- data/lib/comicinfo/local.rb +173 -0
- data/sig/comicinfo/local.rbs +6 -0
- metadata +153 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 1a7274d9469be7da667609f01ce3c2672754edf9c1c5b3ba0bbe004ae7c5f78a
|
|
4
|
+
data.tar.gz: c6b5a90fd7d5448e1742d8f6af32c4bf487514b720b3e15c2e14f04c62577a11
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 495bce5c860000e18131cc88b2d17f9d15676d6aa3db1c4574e8dd748703c8583eac769bd3a7db86dd9003f84fecf15e2d8be5359d059d454c3c49aa72c661a2
|
|
7
|
+
data.tar.gz: 6ed6e6e732d2a3a72ac1232dd240860799896204b51f1b461ca571bb68bb226fb1f4f50393ce84bcdadf1527753f0083d11f4ed4bc329ce7fdce3f426d63ba20
|
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
4.0.10
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [0.1.0] - 2026-12-01
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
- The gem's scaffolding: `ComicInfo::Local`, scripts, RSpec and RuboCop
|
|
14
|
+
- `ComicInfo::Local.new(root:)`: the folder responses are saved in. Without one, it's `COMICINFO_LOCAL_ROOT`, or else `comicinfo-local` in `XDG_DATA_HOME`, or else in `~/.local/share`
|
|
15
|
+
- `ComicInfo::Local#recorder(provider)`: saves what a provider answers under the root, for its adapter gem's `recorder:` argument
|
|
16
|
+
- `ComicInfo::Local#issue(match)`: a saved issue, as a `ComicInfo::Issue` read as its provider's gem reads it, for a match ID such as `metron-31660`, `comic_vine-140529` or `grand_comics_database-911609`. A Comic Vine issue is read with its saved volume, for its publisher, start year and issue count. An issue Metron links to saved Comic Vine and GCD issues, by its `cv_id` and `gcd_id`, is merged: Metron's, with blanks filled from Comic Vine's, then the GCD's. Any linked issue's ID gives the same merged issue
|
|
17
|
+
- `ComicInfo::Local#matches(issue)`: each saved issue with the issue's number in a series whose name contains its series, ignoring case and leading zeros, ranked by `issue.rank`. Linked issues are one match, with the preferred provider's ID, and its details credit every provider. A match is headed with the year its series began, as the providers head theirs, such as "Saga (2012) #1"
|
|
18
|
+
- `ComicInfo::Local#issue_at(url)`: the saved issue whose page on any linked provider's site is the URL, merged, or `nil`
|
|
19
|
+
- `ComicInfo::Local#write_records`: writes each saved issue's merged record under `series/` as YAML, JSON and Markdown, such as `series/saga-2012/001.yml`, made again from `raw/` each time, and returns their paths
|
|
20
|
+
- `ComicInfo::Local#missing_details(issue)`: asks for a Series and a Number when either is blank
|
|
21
|
+
- `ComicInfo::Local#name` and `#home_url`: "comicinfo-local" and its GitHub page
|
|
22
|
+
- `ComicInfo::Local::Record#path`: where an issue's merged record is written, such as `series/saga-2012/001`: the series made safe for a folder with the year it began, then the number zero-padded
|
|
23
|
+
- `ComicInfo::Local::Record#to_h`: an issue's merged record: the preferred provider's ID, every linked ID, each provider's name and page, and each filled field's merged value with what each provider said
|
|
24
|
+
- `ComicInfo::Local::Record#to_yaml`, `#to_json` and `#to_markdown`: the record as YAML, as pretty JSON, and as Markdown with the record as YAML front matter, then the issue's heading, title and summary, and a credit linking each provider's page
|
|
25
|
+
- `ComicInfo::Local::Store#ids`: the IDs saved for a provider's resource, searches left out
|
|
26
|
+
- `ComicInfo::Local::InvalidIssueIDError` and `ComicInfo::Local::NotSavedError`: a match ID that isn't a provider and its ID, or an issue that was never saved
|
|
27
|
+
- `ComicInfo::Local::Store#read(key:)`: an API response saved under a root folder, in `raw/<provider>/<resource>/<id>.json`, as a `ComicInfo::Local::Response` with its `url`, `fetched_at` and `body`
|
|
28
|
+
- `ComicInfo::Local::Store#write(key:, response:)`: saves a response there, as pretty JSON, replacing what was saved before
|
|
29
|
+
- `ComicInfo::Local::Key`: a provider, resource and ID, such as Metron's issue 31660
|
|
30
|
+
- `ComicInfo::Local::SearchKey`: a provider, resource and search parameters, saved in `raw/<provider>/<resource>/search/`, named by the sorted, escaped parameters, or by their SHA-256 digest when that's longer than 200 bytes
|
|
31
|
+
- `ComicInfo::Local::MetronURL`: where a Metron API response is saved, one issue or a search for issues, read from the end of its URL's path so any base URL works
|
|
32
|
+
- `ComicInfo::Local::ComicVineURL`: where a Comic Vine API response is saved, one issue, one volume, or a search or list of volumes or issues. A search's name leaves out its API key, format and field list
|
|
33
|
+
- `ComicInfo::Local::GrandComicsDatabaseURL`: where a Grand Comics Database API response is saved, one issue or a search for issues by series name, number and year
|
|
34
|
+
- `ComicInfo::Local::Recorder#record(url:, body:)`: saves one provider's API response, with its URL and the time, for an adapter gem to call as it fetches: `metron`, `comic_vine` or `grand_comics_database`
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Code of Conduct
|
|
2
|
+
|
|
3
|
+
"comicinfo-local" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
|
|
4
|
+
|
|
5
|
+
* Participants will be tolerant of opposing views.
|
|
6
|
+
* Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
|
|
7
|
+
* When interpreting the words and actions of others, participants should always assume good intentions.
|
|
8
|
+
* Behaviour which can be reasonably considered harassment will not be tolerated.
|
|
9
|
+
|
|
10
|
+
If you have any concerns about behaviour within this project, please contact us at ["veganstraightedge@gmail.com"](mailto:"veganstraightedge@gmail.com").
|
data/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Shane Becker
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# comicinfo-local
|
|
2
|
+
|
|
3
|
+
Looks up comic details saved on disk, for the [comicinfo](https://github.com/veganstraightedge/comicinfo) gem.
|
|
4
|
+
|
|
5
|
+
`ComicInfo::Local` is a metadata source like its siblings [comicinfo-metron](https://github.com/veganstraightedge/comicinfo-metron), [comicinfo-comic_vine](https://github.com/veganstraightedge/comicinfo-comic_vine) and [comicinfo-grand_comics_database](https://github.com/veganstraightedge/comicinfo-grand_comics_database). It keeps what they fetch, and reads it back later with no network.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'comicinfo-local'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
Hand each adapter a recorder, and everything it fetches is saved:
|
|
16
|
+
|
|
17
|
+
```ruby
|
|
18
|
+
require 'comicinfo/local'
|
|
19
|
+
|
|
20
|
+
local = ComicInfo::Local.new
|
|
21
|
+
|
|
22
|
+
metron = ComicInfo::Metron.new token: token, user_agent: 'MyApp/1.0', recorder: local.recorder('metron')
|
|
23
|
+
comic_vine = ComicInfo::ComicVine.new api_key: key, user_agent: 'MyApp/1.0', recorder: local.recorder('comic_vine')
|
|
24
|
+
gcd = ComicInfo::GrandComicsDatabase.new user_agent: 'MyApp/1.0', recorder: local.recorder('grand_comics_database')
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Later, offline, `local` is a source like the others:
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
issue = ComicInfo.load 'path/to/ComicInfo.xml'
|
|
31
|
+
matches = local.matches issue # best first
|
|
32
|
+
found = local.issue matches[0] # after a person picks one
|
|
33
|
+
updated = issue.with_blanks_filled_from found
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
- `matches` needs a Series and a Number. It finds every saved issue with that number in a series whose name contains the issue's series, ignoring case and leading zeros, from every provider.
|
|
37
|
+
- Metron's issues give their Comic Vine and GCD IDs. When those issues are saved too, they're linked: one match, with Metron's ID, such as `metron-31660`, and details naming every provider, such as "Metron, Grand Comics Database · Cover date 2012-03-01". An issue nothing links to is its own match, with its provider's ID, such as `comic_vine-140529`.
|
|
38
|
+
- `issue` reads each linked issue as its provider's own gem reads it, and merges them: Metron's details, with blanks filled from Comic Vine's, then the GCD's. Any linked issue's ID gives the same merged issue. A Comic Vine issue is read with its saved volume.
|
|
39
|
+
- `issue_at` reads the saved issue whose page on any linked provider's site is a URL, merged, or is `nil`.
|
|
40
|
+
- A match ID that isn't one of comicinfo-local's raises `ComicInfo::Local::InvalidIssueIDError`, and one never saved raises `ComicInfo::Local::NotSavedError`.
|
|
41
|
+
|
|
42
|
+
## Where it's saved
|
|
43
|
+
|
|
44
|
+
The root folder is, in order:
|
|
45
|
+
|
|
46
|
+
1. `ComicInfo::Local.new(root:)`
|
|
47
|
+
2. `COMICINFO_LOCAL_ROOT`
|
|
48
|
+
3. `comicinfo-local` in `XDG_DATA_HOME`
|
|
49
|
+
4. `~/.local/share/comicinfo-local`
|
|
50
|
+
|
|
51
|
+
Each response is a JSON file with its `url`, `fetched_at` and `body`, the response as the API answered:
|
|
52
|
+
|
|
53
|
+
```
|
|
54
|
+
raw/metron/issue/31660.json
|
|
55
|
+
raw/metron/issue/search/cover_year=2012&number=1&series_name=Saga.json
|
|
56
|
+
raw/comic_vine/issue/140529.json
|
|
57
|
+
raw/comic_vine/volume/23437.json
|
|
58
|
+
raw/grand_comics_database/issue/911609.json
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
A search is named by its parameters, or by their SHA-256 digest when that's too long for a file name. API keys are never saved.
|
|
62
|
+
|
|
63
|
+
`local.write_records` writes each issue's merged record beside them, as YAML, JSON and Markdown with YAML front matter:
|
|
64
|
+
|
|
65
|
+
```
|
|
66
|
+
series/saga-2012/001.yml
|
|
67
|
+
series/saga-2012/001.json
|
|
68
|
+
series/saga-2012/001.md
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
The folder is the series made safe for a file name, with the year it began when a provider says. A record has the preferred provider's ID, every linked ID, each provider's page, and each field's merged value with what each provider said:
|
|
72
|
+
|
|
73
|
+
```yaml
|
|
74
|
+
fields:
|
|
75
|
+
colorist:
|
|
76
|
+
value: Fiona Staples
|
|
77
|
+
sources:
|
|
78
|
+
grand_comics_database: Fiona Staples
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
`series/` is made again from `raw/` each time, so what's in it is removed first. Don't edit it by hand.
|
|
82
|
+
|
|
83
|
+
## Credit
|
|
84
|
+
|
|
85
|
+
Whatever shows the saved details must credit and link to the provider they came from, as its gem says. Each match's details name it.
|
|
86
|
+
|
|
87
|
+
## Development
|
|
88
|
+
|
|
89
|
+
```sh
|
|
90
|
+
script/setup # bundle install
|
|
91
|
+
script/test # specs and RuboCop
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## License
|
|
95
|
+
|
|
96
|
+
[MIT](LICENSE.md)
|
data/Rakefile
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
require 'uri'
|
|
2
|
+
require_relative 'key'
|
|
3
|
+
require_relative 'search_key'
|
|
4
|
+
|
|
5
|
+
module ComicInfo
|
|
6
|
+
class Local
|
|
7
|
+
# A Comic Vine API URL that comicinfo-comic_vine asks for, and where its response is saved
|
|
8
|
+
#
|
|
9
|
+
# Only the end of its path is read, so it's the same with any base URL.
|
|
10
|
+
class ComicVineURL
|
|
11
|
+
PROVIDER = 'comic_vine'.freeze
|
|
12
|
+
|
|
13
|
+
# The parameters left out of a search's name: they don't change what's found, and an API key is a secret
|
|
14
|
+
UNNAMED_PARAMETERS = %i[api_key field_list format].freeze
|
|
15
|
+
|
|
16
|
+
def initialize url
|
|
17
|
+
@uri = URI.parse url
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Where the response is saved, or nil when it isn't one comicinfo-local keeps
|
|
21
|
+
#
|
|
22
|
+
# - issue/4000-140529/ is one issue
|
|
23
|
+
# - volume/4050-23437/ is one volume
|
|
24
|
+
# - search/ and volumes/ find volumes
|
|
25
|
+
# - issues/ finds issues
|
|
26
|
+
def key
|
|
27
|
+
resource, path_id = segments.last(2)
|
|
28
|
+
|
|
29
|
+
issue_id = id_after(prefix: '4000-', path_id:) if resource == 'issue'
|
|
30
|
+
volume_id = id_after(prefix: '4050-', path_id:) if resource == 'volume'
|
|
31
|
+
|
|
32
|
+
return Key.new(provider: PROVIDER, resource: 'issue', id: issue_id) if issue_id
|
|
33
|
+
return Key.new(provider: PROVIDER, resource: 'volume', id: volume_id) if volume_id
|
|
34
|
+
|
|
35
|
+
search_key
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
attr_reader :uri
|
|
41
|
+
|
|
42
|
+
# Where a search or list is saved, or nil for any other URL
|
|
43
|
+
def search_key
|
|
44
|
+
case segments.last
|
|
45
|
+
when 'search', 'volumes' then SearchKey.new(provider: PROVIDER, resource: 'volume', parameters:)
|
|
46
|
+
when 'issues' then SearchKey.new(provider: PROVIDER, resource: 'issue', parameters:)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Comic Vine's ID in a path part, such as 140529 in 4000-140529, or nil
|
|
51
|
+
def id_after prefix:, path_id:
|
|
52
|
+
id = path_id.to_s.delete_prefix prefix
|
|
53
|
+
|
|
54
|
+
id if path_id.to_s.start_with?(prefix) && id.match?(/\A\d+\z/)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def segments = uri.path.split('/').reject(&:empty?)
|
|
58
|
+
|
|
59
|
+
def parameters
|
|
60
|
+
all = URI.decode_www_form(uri.query.to_s).to_h { |name, value| [name.to_sym, value] }
|
|
61
|
+
|
|
62
|
+
all.except(*UNNAMED_PARAMETERS)
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
module ComicInfo
|
|
2
|
+
class Local
|
|
3
|
+
# Why a lookup on disk failed
|
|
4
|
+
class Error < ComicInfo::Error; end
|
|
5
|
+
|
|
6
|
+
# A match's ID isn't a provider and its ID, such as metron-31660, so it can't have come from comicinfo-local
|
|
7
|
+
class InvalidIssueIDError < Error
|
|
8
|
+
attr_reader :id
|
|
9
|
+
|
|
10
|
+
def initialize id
|
|
11
|
+
@id = id
|
|
12
|
+
super("“#{id}” isn’t a comicinfo-local ID, such as metron-31660.")
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# No response is saved for the issue
|
|
17
|
+
class NotSavedError < Error
|
|
18
|
+
attr_reader :id
|
|
19
|
+
|
|
20
|
+
def initialize id
|
|
21
|
+
@id = id
|
|
22
|
+
super("Nothing is saved for #{id}.")
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
require 'uri'
|
|
2
|
+
require_relative 'key'
|
|
3
|
+
require_relative 'search_key'
|
|
4
|
+
|
|
5
|
+
module ComicInfo
|
|
6
|
+
class Local
|
|
7
|
+
# A Grand Comics Database API URL that comicinfo-grand_comics_database asks for, and where its response is saved
|
|
8
|
+
#
|
|
9
|
+
# Only the end of its path is read, so it's the same with any base URL.
|
|
10
|
+
class GrandComicsDatabaseURL
|
|
11
|
+
PROVIDER = 'grand_comics_database'.freeze
|
|
12
|
+
|
|
13
|
+
def initialize url
|
|
14
|
+
@uri = URI.parse url
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Where the response is saved, or nil when it isn't one comicinfo-local keeps
|
|
18
|
+
#
|
|
19
|
+
# - issue/911609/ is one issue
|
|
20
|
+
# - series/name/Saga/issue/1/year/2012/ is a search for issues
|
|
21
|
+
def key
|
|
22
|
+
return search_key if search?
|
|
23
|
+
|
|
24
|
+
resource, id = segments.last(2)
|
|
25
|
+
return unless resource == 'issue' && id&.match?(/\A\d+\z/)
|
|
26
|
+
|
|
27
|
+
Key.new(provider: PROVIDER, resource: 'issue', id:)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
attr_reader :uri
|
|
33
|
+
|
|
34
|
+
# Whether the path ends series/name/<name>/issue/<number>/year/<year>
|
|
35
|
+
def search?
|
|
36
|
+
last_seven = segments.last(7)
|
|
37
|
+
|
|
38
|
+
last_seven.length == 7 && last_seven.values_at(0, 1, 3, 5) == %w[series name issue year]
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def search_key
|
|
42
|
+
_, _, name, _, number, _, year = segments.last(7)
|
|
43
|
+
parameters = { name: URI.decode_uri_component(name), number: URI.decode_uri_component(number), year: }
|
|
44
|
+
|
|
45
|
+
SearchKey.new(provider: PROVIDER, resource: 'issue', parameters:)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def segments = uri.path.split('/').reject(&:empty?)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
module ComicInfo
|
|
2
|
+
class Local
|
|
3
|
+
# Where one provider's response about one thing, such as Metron's issue 31660, is saved
|
|
4
|
+
Key = Data.define :provider, :resource, :id do
|
|
5
|
+
# The saved file's path under raw/, without its extension, such as "metron/issue/31660"
|
|
6
|
+
def path = "#{provider}/#{resource}/#{id}"
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
module ComicInfo
|
|
2
|
+
class Local
|
|
3
|
+
# One issue as several providers saved it, linked by the IDs Metron gives for Comic Vine and the GCD.
|
|
4
|
+
#
|
|
5
|
+
# The saved issues are in the order providers are preferred: Metron, then Comic Vine, then the GCD.
|
|
6
|
+
# An issue no other links to is linked to nothing, alone.
|
|
7
|
+
class LinkedIssues
|
|
8
|
+
attr_reader :saved_issues
|
|
9
|
+
|
|
10
|
+
def initialize saved_issues:
|
|
11
|
+
@saved_issues = saved_issues
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Whether one of the saved issues has the comicinfo-local ID, such as metron-31660
|
|
15
|
+
def include?(local_id) = saved_issues.any? { it.local_id == local_id }
|
|
16
|
+
|
|
17
|
+
# Everything the providers said, merged: the preferred provider's issue, with its blanks filled
|
|
18
|
+
# from the next, and so on
|
|
19
|
+
def issue
|
|
20
|
+
@issue ||= saved_issues.map(&:issue).reduce { |merged, next_issue| merged.with_blanks_filled_from next_issue }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# The preferred provider's match, crediting every provider
|
|
24
|
+
def match
|
|
25
|
+
providers = saved_issues.map(&:provider_name).join ', '
|
|
26
|
+
cover_date = preferred.cover_date || 'unknown'
|
|
27
|
+
|
|
28
|
+
preferred.match.with details: "#{providers} · Cover date #{cover_date}"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Every saved issue's pages on its provider's site
|
|
32
|
+
def web_links = saved_issues.flat_map { it.issue.web_links }
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def preferred = saved_issues.first
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
require 'uri'
|
|
2
|
+
require_relative 'key'
|
|
3
|
+
require_relative 'search_key'
|
|
4
|
+
|
|
5
|
+
module ComicInfo
|
|
6
|
+
class Local
|
|
7
|
+
# A Metron API URL that comicinfo-metron asks for, and where its response is saved
|
|
8
|
+
#
|
|
9
|
+
# Only the end of its path is read, so it's the same with any base URL.
|
|
10
|
+
class MetronURL
|
|
11
|
+
PROVIDER = 'metron'.freeze
|
|
12
|
+
|
|
13
|
+
def initialize url
|
|
14
|
+
@uri = URI.parse url
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Where the response is saved, or nil when it isn't one comicinfo-local keeps
|
|
18
|
+
#
|
|
19
|
+
# - issue/31660/ is one issue
|
|
20
|
+
# - issue/?number=1&series_name=Saga is a search for issues
|
|
21
|
+
def key
|
|
22
|
+
resource, id = segments.last(2)
|
|
23
|
+
|
|
24
|
+
return Key.new(provider: PROVIDER, resource:, id:) if resource == 'issue' && id&.match?(/\A\d+\z/)
|
|
25
|
+
return unless segments.last == 'issue'
|
|
26
|
+
|
|
27
|
+
SearchKey.new(provider: PROVIDER, resource: 'issue', parameters:)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
attr_reader :uri
|
|
33
|
+
|
|
34
|
+
def segments = uri.path.split('/').reject(&:empty?)
|
|
35
|
+
|
|
36
|
+
def parameters = URI.decode_www_form(uri.query.to_s).to_h { |name, value| [name.to_sym, value] }
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
require 'stringex'
|
|
3
|
+
require 'yaml'
|
|
4
|
+
|
|
5
|
+
module ComicInfo
|
|
6
|
+
class Local
|
|
7
|
+
# One issue's merged details, to be written beside the raw responses in YAML, JSON and Markdown.
|
|
8
|
+
#
|
|
9
|
+
# It's made from the issue's linked saved issues, so it can always be made again from raw/.
|
|
10
|
+
class Record
|
|
11
|
+
# ComicInfo::Issue#to_h keys left out: the raw text of fields it also has as lists, the comma
|
|
12
|
+
# separated fields it also has as lists, and the pages, which no provider knows
|
|
13
|
+
LEFT_OUT_FIELDS = %i[
|
|
14
|
+
characters_raw_data
|
|
15
|
+
genre
|
|
16
|
+
genres_raw_data
|
|
17
|
+
locations_raw_data
|
|
18
|
+
pages
|
|
19
|
+
story_arc
|
|
20
|
+
story_arc_number
|
|
21
|
+
story_arc_numbers_raw_data
|
|
22
|
+
story_arcs_raw_data
|
|
23
|
+
teams_raw_data
|
|
24
|
+
web
|
|
25
|
+
].freeze
|
|
26
|
+
|
|
27
|
+
# The values ComicInfo::Issue gives a field nobody filled. A page count nobody knows is 0.
|
|
28
|
+
BLANK_VALUES = [nil, '', -1, 0, [], 'Unknown'].freeze
|
|
29
|
+
|
|
30
|
+
def initialize linked_issues:
|
|
31
|
+
@linked_issues = linked_issues
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# The record, with symbol keys:
|
|
35
|
+
#
|
|
36
|
+
# - id: the preferred provider's comicinfo-local ID, such as metron-31660
|
|
37
|
+
# - linked_ids: every linked issue's
|
|
38
|
+
# - sources: each provider's ID, name and page, for the credit
|
|
39
|
+
# - fields: each filled field's merged value, and what each provider said
|
|
40
|
+
#
|
|
41
|
+
# Its keys are in that order, so the written files read top down.
|
|
42
|
+
def to_h
|
|
43
|
+
{
|
|
44
|
+
id: linked_issues.saved_issues.first.local_id,
|
|
45
|
+
linked_ids: linked_issues.saved_issues.map(&:local_id),
|
|
46
|
+
sources:,
|
|
47
|
+
fields:
|
|
48
|
+
}
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# The record as YAML, with string keys
|
|
52
|
+
def to_yaml = YAML.dump(string_keyed)
|
|
53
|
+
|
|
54
|
+
# The record as pretty JSON
|
|
55
|
+
def to_json(*) = "#{JSON.pretty_generate to_h}\n"
|
|
56
|
+
|
|
57
|
+
# The record as YAML front matter, then the issue's heading, title and summary, and the credit
|
|
58
|
+
def to_markdown
|
|
59
|
+
<<~MARKDOWN
|
|
60
|
+
#{to_yaml.chomp}
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
#{markdown_body}
|
|
64
|
+
MARKDOWN
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Where the record is written under the root, without an extension, such as series/saga-2012/001:
|
|
68
|
+
# the series named with the year it began, when a provider says, then the number
|
|
69
|
+
def path = "series/#{series_folder}/#{number_file}"
|
|
70
|
+
|
|
71
|
+
private
|
|
72
|
+
|
|
73
|
+
attr_reader :linked_issues
|
|
74
|
+
|
|
75
|
+
def issue = linked_issues.issue
|
|
76
|
+
|
|
77
|
+
# The record with string keys, as JSON has them
|
|
78
|
+
def string_keyed = JSON.parse(JSON.generate(to_h))
|
|
79
|
+
|
|
80
|
+
def markdown_body
|
|
81
|
+
heading = linked_issues.match.heading
|
|
82
|
+
title = issue.title.strip
|
|
83
|
+
summary = issue.summary.strip
|
|
84
|
+
headline = title.empty? ? heading : "#{heading}: #{title}"
|
|
85
|
+
|
|
86
|
+
[
|
|
87
|
+
"# #{headline}",
|
|
88
|
+
(summary unless summary.empty?),
|
|
89
|
+
"Details from #{credit_links}."
|
|
90
|
+
].compact.join "\n\n"
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Each provider's name, linked to the issue's page on its site, joined as a sentence
|
|
94
|
+
def credit_links
|
|
95
|
+
links = sources.map { |source| source[:url] ? "[#{source[:name]}](#{source[:url]})" : source[:name] }
|
|
96
|
+
return links.first if links.one?
|
|
97
|
+
|
|
98
|
+
"#{links[..-2].join ', '} and #{links.last}"
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Each provider's ID, name and the issue's page on its site
|
|
102
|
+
def sources
|
|
103
|
+
linked_issues.saved_issues.map do |saved|
|
|
104
|
+
{ id: saved.local_id, name: saved.provider_name, url: saved.issue.web_links.first }
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Each field the merged issue has filled: its value, and the value from each provider that filled it
|
|
109
|
+
def fields
|
|
110
|
+
filled(issue.to_h).to_h do |field, value|
|
|
111
|
+
[field, { value:, sources: sources_of(field) }]
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# What each provider said a field is, by provider, leaving out those that left it blank
|
|
116
|
+
def sources_of field
|
|
117
|
+
linked_issues.saved_issues.each_with_object({}) do |saved, said|
|
|
118
|
+
value = saved.issue.to_h[field]
|
|
119
|
+
said[saved.provider.to_sym] = value unless blank? value
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# The fields of an issue's hash that are filled and not left out
|
|
124
|
+
def filled(fields) = fields.except(*LEFT_OUT_FIELDS).reject { |_, value| blank? value }
|
|
125
|
+
|
|
126
|
+
def blank?(value) = BLANK_VALUES.include?(value.is_a?(String) ? value.strip : value)
|
|
127
|
+
|
|
128
|
+
# The series name made safe for a folder, with the year it began, such as saga-2012
|
|
129
|
+
def series_folder
|
|
130
|
+
year = linked_issues.saved_issues.filter_map(&:series_start_year).first
|
|
131
|
+
|
|
132
|
+
[issue.series.to_url, year].compact.join '-'
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# The number zero-padded to three digits, such as 001, or else made safe for a file name
|
|
136
|
+
def number_file
|
|
137
|
+
number = issue.number.strip
|
|
138
|
+
return number.rjust(3, '0') if number.match?(/\A\d+\z/)
|
|
139
|
+
|
|
140
|
+
number.to_url
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
require_relative 'comic_vine_url'
|
|
2
|
+
require_relative 'grand_comics_database_url'
|
|
3
|
+
require_relative 'metron_url'
|
|
4
|
+
require_relative 'response'
|
|
5
|
+
|
|
6
|
+
module ComicInfo
|
|
7
|
+
class Local
|
|
8
|
+
# Saves what one provider's API answers, for an adapter gem to hand each response to as it's fetched:
|
|
9
|
+
#
|
|
10
|
+
# recorder = ComicInfo::Local::Recorder.new provider: 'metron', store: store
|
|
11
|
+
# metron = ComicInfo::Metron.new token:, user_agent:, recorder:
|
|
12
|
+
#
|
|
13
|
+
# ComicInfo::Local#recorder makes one saving under its root.
|
|
14
|
+
class Recorder
|
|
15
|
+
def initialize provider:, store:
|
|
16
|
+
raise ArgumentError, "Unknown provider: #{provider}" unless PROVIDERS.include? provider
|
|
17
|
+
|
|
18
|
+
@provider = provider
|
|
19
|
+
@store = store
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Saves body, the parsed response from url, unless url isn't one comicinfo-local keeps
|
|
23
|
+
def record url:, body:
|
|
24
|
+
key = key_for url
|
|
25
|
+
return unless key
|
|
26
|
+
|
|
27
|
+
response = Response.new(body:, fetched_at: Time.now.utc, url:)
|
|
28
|
+
|
|
29
|
+
store.write(key:, response:)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
attr_reader :provider, :store
|
|
35
|
+
|
|
36
|
+
def key_for url
|
|
37
|
+
case provider
|
|
38
|
+
when 'comic_vine' then ComicVineURL.new(url).key
|
|
39
|
+
when 'grand_comics_database' then GrandComicsDatabaseURL.new(url).key
|
|
40
|
+
when 'metron' then MetronURL.new(url).key
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
module ComicInfo
|
|
2
|
+
class Local
|
|
3
|
+
# One issue a provider answered with, as saved: read into a ComicInfo::Issue as the provider's gem
|
|
4
|
+
# reads it, and listed as a ComicInfo::Match
|
|
5
|
+
class SavedIssue
|
|
6
|
+
# Each provider's name, for the credit
|
|
7
|
+
PROVIDER_NAMES = {
|
|
8
|
+
'comic_vine' => 'Comic Vine',
|
|
9
|
+
'grand_comics_database' => 'Grand Comics Database',
|
|
10
|
+
'metron' => 'Metron'
|
|
11
|
+
}.freeze
|
|
12
|
+
|
|
13
|
+
attr_reader :id, :provider
|
|
14
|
+
|
|
15
|
+
# - provider: whose answer it is, such as metron
|
|
16
|
+
# - id: the provider's own ID for the issue
|
|
17
|
+
# - store: where the provider's responses are saved
|
|
18
|
+
def initialize provider:, id:, store:
|
|
19
|
+
@id = id
|
|
20
|
+
@provider = provider
|
|
21
|
+
@store = store
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# The ID comicinfo-local's matches have, such as metron-31660
|
|
25
|
+
def local_id = "#{provider}-#{id}"
|
|
26
|
+
|
|
27
|
+
# Everything the provider said about the issue, as a ComicInfo::Issue
|
|
28
|
+
def issue
|
|
29
|
+
@issue ||= case provider
|
|
30
|
+
when 'comic_vine' then comic_vine_issue
|
|
31
|
+
when 'grand_comics_database' then GrandComicsDatabase::FoundIssue.new(id:, issue: body).to_issue
|
|
32
|
+
when 'metron' then Metron::FoundIssue.new(issue: body).to_issue
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# The issue as a match, for a person to pick. ComicInfo::Local::LinkedIssues gives its details.
|
|
37
|
+
#
|
|
38
|
+
# It's headed with the year the series began, as the providers head theirs, such as Saga (2012) #1.
|
|
39
|
+
def match
|
|
40
|
+
year = series_start_year ? " (#{series_start_year})" : ''
|
|
41
|
+
title = present issue.title
|
|
42
|
+
site_url = issue.web_links.first
|
|
43
|
+
|
|
44
|
+
Match.new(cover_date:,
|
|
45
|
+
heading: "#{issue.series}#{year} ##{issue.number}",
|
|
46
|
+
id: local_id,
|
|
47
|
+
number: issue.number,
|
|
48
|
+
series: issue.series,
|
|
49
|
+
site_url:,
|
|
50
|
+
title:)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# The provider's name, for the credit
|
|
54
|
+
def provider_name = PROVIDER_NAMES[provider]
|
|
55
|
+
|
|
56
|
+
# The comicinfo-local IDs of other providers' issues the provider says are this one.
|
|
57
|
+
#
|
|
58
|
+
# Only Metron says: its issues have Comic Vine's and the GCD's IDs.
|
|
59
|
+
def linked_ids
|
|
60
|
+
return [] unless provider == 'metron'
|
|
61
|
+
|
|
62
|
+
comic_vine = "comic_vine-#{body[:cv_id]}" if body[:cv_id]
|
|
63
|
+
grand_comics_database = "grand_comics_database-#{body[:gcd_id]}" if body[:gcd_id]
|
|
64
|
+
|
|
65
|
+
[comic_vine, grand_comics_database].compact
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# The year the issue's series began, or nil when the provider doesn't say
|
|
69
|
+
def series_start_year
|
|
70
|
+
year = case provider
|
|
71
|
+
when 'comic_vine' then comic_vine_volume[:start_year]
|
|
72
|
+
when 'grand_comics_database' then body[:series_name].to_s[/\((\d{4}) series\)/, 1]
|
|
73
|
+
when 'metron' then body.dig(:series, :year_began)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
Integer year.to_s, 10, exception: false
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# The cover date as the provider writes it, usually "2012-03-01"
|
|
80
|
+
def cover_date
|
|
81
|
+
case provider
|
|
82
|
+
when 'comic_vine' then present body.dig(:results, :cover_date)
|
|
83
|
+
when 'grand_comics_database' then key_date
|
|
84
|
+
when 'metron' then present body[:cover_date]
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
private
|
|
89
|
+
|
|
90
|
+
attr_reader :store
|
|
91
|
+
|
|
92
|
+
# The provider's saved response about the issue, or nil
|
|
93
|
+
def response
|
|
94
|
+
return @response if defined? @response
|
|
95
|
+
|
|
96
|
+
key = Key.new(provider:, resource: 'issue', id:)
|
|
97
|
+
@response = store.read(key:)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# The provider's saved response, parsed
|
|
101
|
+
def body = response.body
|
|
102
|
+
|
|
103
|
+
# The GCD's key date without its unknown parts, so 2012-03-00 is 2012-03
|
|
104
|
+
def key_date
|
|
105
|
+
present(body[:key_date])&.sub(/(-00)+\z/, '')
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# A Comic Vine issue, as comicinfo-comic_vine reads it with its volume
|
|
109
|
+
def comic_vine_issue
|
|
110
|
+
ComicVine::FoundIssue.new(issue: body[:results], volume: comic_vine_volume).to_issue
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Comic Vine's saved detail for the issue's volume, or nothing when it wasn't saved.
|
|
114
|
+
#
|
|
115
|
+
# Comic Vine's issue detail names its volume, but its publisher, start year and issue count are
|
|
116
|
+
# in the volume's own detail.
|
|
117
|
+
def comic_vine_volume
|
|
118
|
+
volume_id = body.dig(:results, :volume, :id)
|
|
119
|
+
key = Key.new(provider:, resource: 'volume', id: volume_id)
|
|
120
|
+
saved = store.read(key:) if volume_id
|
|
121
|
+
|
|
122
|
+
saved&.body&.dig(:results) || {}
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# The text, trimmed, or nil when it's blank
|
|
126
|
+
def present text
|
|
127
|
+
trimmed = text.to_s.strip
|
|
128
|
+
|
|
129
|
+
trimmed unless trimmed.empty?
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
require 'digest'
|
|
2
|
+
require 'uri'
|
|
3
|
+
|
|
4
|
+
module ComicInfo
|
|
5
|
+
class Local
|
|
6
|
+
# The longest name a search's file gets before it's named by a digest instead.
|
|
7
|
+
# Most file systems allow 255 bytes, and ".json" needs 5 of them.
|
|
8
|
+
LONGEST_SEARCH_NAME = 200
|
|
9
|
+
|
|
10
|
+
# Where one provider's answer to one search or list, such as Metron's issues numbered 1 in Saga, is saved
|
|
11
|
+
SearchKey = Data.define :provider, :resource, :parameters do
|
|
12
|
+
# The saved file's path under raw/, without its extension, such as
|
|
13
|
+
# "metron/issue/search/number=1&series_name=Saga"
|
|
14
|
+
def path = "#{provider}/#{resource}/search/#{name}"
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
# The parameters sorted and escaped, or their digest when that's too long for a file name
|
|
19
|
+
def name
|
|
20
|
+
readable = URI.encode_www_form parameters.sort
|
|
21
|
+
return readable if readable.bytesize <= LONGEST_SEARCH_NAME
|
|
22
|
+
|
|
23
|
+
Digest::SHA256.hexdigest readable
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
require 'fileutils'
|
|
2
|
+
require 'json'
|
|
3
|
+
require 'time'
|
|
4
|
+
require_relative 'response'
|
|
5
|
+
|
|
6
|
+
module ComicInfo
|
|
7
|
+
class Local
|
|
8
|
+
# The API responses saved under root, one JSON file each, in raw/<key path>.json
|
|
9
|
+
class Store
|
|
10
|
+
def initialize root:
|
|
11
|
+
@root = root
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# The response saved at key, or nil when there isn't one
|
|
15
|
+
def read key:
|
|
16
|
+
path = path_for key
|
|
17
|
+
return unless File.exist? path
|
|
18
|
+
|
|
19
|
+
saved = JSON.parse File.read(path), symbolize_names: true
|
|
20
|
+
fetched_at = Time.iso8601 saved[:fetched_at]
|
|
21
|
+
|
|
22
|
+
Response.new body: saved[:body], fetched_at:, url: saved[:url]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# The ID of each response saved for the provider's resource, sorted. Searches aren't included.
|
|
26
|
+
def ids provider:, resource:
|
|
27
|
+
pattern = File.join root, 'raw', provider, resource, '*.json'
|
|
28
|
+
|
|
29
|
+
Dir.glob(pattern).map { File.basename it, '.json' }.sort
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Saves response at key, replacing what was saved before
|
|
33
|
+
def write key:, response:
|
|
34
|
+
path = path_for key
|
|
35
|
+
fetched_at = response.fetched_at.utc.iso8601
|
|
36
|
+
saved = { body: response.body, fetched_at:, url: response.url }
|
|
37
|
+
|
|
38
|
+
FileUtils.mkdir_p File.dirname(path)
|
|
39
|
+
File.write path, "#{JSON.pretty_generate saved}\n"
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
attr_reader :root
|
|
45
|
+
|
|
46
|
+
def path_for(key) = File.join root, 'raw', "#{key.path}.json"
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
require 'fileutils'
|
|
2
|
+
require 'comicinfo'
|
|
3
|
+
require 'comicinfo/comic_vine'
|
|
4
|
+
require 'comicinfo/grand_comics_database'
|
|
5
|
+
require 'comicinfo/metron'
|
|
6
|
+
require_relative 'local/comic_vine_url'
|
|
7
|
+
require_relative 'local/errors'
|
|
8
|
+
require_relative 'local/grand_comics_database_url'
|
|
9
|
+
require_relative 'local/linked_issues'
|
|
10
|
+
require_relative 'local/key'
|
|
11
|
+
require_relative 'local/metron_url'
|
|
12
|
+
require_relative 'local/record'
|
|
13
|
+
require_relative 'local/recorder'
|
|
14
|
+
require_relative 'local/saved_issue'
|
|
15
|
+
require_relative 'local/search_key'
|
|
16
|
+
require_relative 'local/store'
|
|
17
|
+
require_relative 'local/version'
|
|
18
|
+
|
|
19
|
+
module ComicInfo
|
|
20
|
+
# Looks up comic details saved on disk from Metron, Comic Vine and the Grand Comics Database
|
|
21
|
+
class Local
|
|
22
|
+
# The folder in the data directory that's the root when none is given
|
|
23
|
+
FOLDER = 'comicinfo-local'.freeze
|
|
24
|
+
|
|
25
|
+
# The providers whose responses are saved, by the names of their folders
|
|
26
|
+
PROVIDERS = %w[metron comic_vine grand_comics_database].freeze
|
|
27
|
+
|
|
28
|
+
attr_reader :root
|
|
29
|
+
|
|
30
|
+
# - root: the folder responses are saved in. Without one, it's COMICINFO_LOCAL_ROOT, or else
|
|
31
|
+
# comicinfo-local in XDG_DATA_HOME, or else in ~/.local/share
|
|
32
|
+
# - environment: where those variables are read from
|
|
33
|
+
def initialize root: nil, environment: ENV
|
|
34
|
+
@root = root || present(environment['COMICINFO_LOCAL_ROOT']) || File.join(data_home(environment), FOLDER)
|
|
35
|
+
@store = Store.new(root: @root)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Saves what provider answers under the root, for its adapter gem's recorder: argument
|
|
39
|
+
#
|
|
40
|
+
# metron = ComicInfo::Metron.new token:, user_agent:, recorder: local.recorder('metron')
|
|
41
|
+
def recorder(provider) = Recorder.new(provider:, store:)
|
|
42
|
+
|
|
43
|
+
def name = 'comicinfo-local'
|
|
44
|
+
def home_url = 'https://github.com/veganstraightedge/comicinfo-local'
|
|
45
|
+
|
|
46
|
+
# What the issue still needs before it can be searched for, or nil
|
|
47
|
+
def missing_details issue
|
|
48
|
+
return if present(issue.series) && present(issue.number)
|
|
49
|
+
|
|
50
|
+
'comicinfo-local needs a Series and a Number to search for.'
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Each saved issue with the issue's number in a series whose name contains its series, best first.
|
|
54
|
+
#
|
|
55
|
+
# Case is ignored in series names, and leading zeros in numbers, so "saga" #001 finds Saga #1.
|
|
56
|
+
# Issues Metron links to other providers' saved issues are one match, with Metron's ID.
|
|
57
|
+
def matches issue
|
|
58
|
+
series = issue.series.strip.downcase
|
|
59
|
+
number = normalized_number issue.number
|
|
60
|
+
|
|
61
|
+
found = linked_issues.select do |linked|
|
|
62
|
+
linked.saved_issues.any? do |saved|
|
|
63
|
+
saved.issue.series.downcase.include?(series) && normalized_number(saved.issue.number) == number
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
issue.rank found.map(&:match)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Everything saved about the match's issue, as a ComicInfo::Issue: what its preferred provider
|
|
71
|
+
# said, with blanks filled from the issues it's linked to, each read as its provider's gem reads it.
|
|
72
|
+
# Metron is preferred, then Comic Vine, then the GCD.
|
|
73
|
+
def issue match
|
|
74
|
+
provider_and_id match.id # raises InvalidIssueIDError for an ID that isn't comicinfo-local's
|
|
75
|
+
|
|
76
|
+
linked = linked_issues.find { it.include? match.id }
|
|
77
|
+
raise NotSavedError, match.id unless linked
|
|
78
|
+
|
|
79
|
+
linked.issue
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# The saved issue whose page on its provider's site is url, merged as #issue merges it, or nil when
|
|
83
|
+
# no saved issue has that page. A trailing slash is ignored.
|
|
84
|
+
def issue_at url
|
|
85
|
+
page = url.to_s.strip.delete_suffix '/'
|
|
86
|
+
linked = linked_issues.find do |candidate|
|
|
87
|
+
candidate.web_links.any? { it.delete_suffix('/') == page }
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
linked&.issue
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Writes each saved issue's merged record under series/, as YAML, JSON and Markdown, such as
|
|
94
|
+
# series/saga-2012/001.yml, and returns their paths under the root.
|
|
95
|
+
#
|
|
96
|
+
# series/ is made again from raw/ each time: what was in it before is removed first.
|
|
97
|
+
def write_records
|
|
98
|
+
series = File.join root, 'series'
|
|
99
|
+
FileUtils.rm_rf series
|
|
100
|
+
|
|
101
|
+
linked_issues.flat_map do |linked|
|
|
102
|
+
record = Record.new(linked_issues: linked)
|
|
103
|
+
|
|
104
|
+
[
|
|
105
|
+
write_file(path: "#{record.path}.yml", text: record.to_yaml),
|
|
106
|
+
write_file(path: "#{record.path}.json", text: record.to_json),
|
|
107
|
+
write_file(path: "#{record.path}.md", text: record.to_markdown)
|
|
108
|
+
]
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
private
|
|
113
|
+
|
|
114
|
+
attr_reader :store
|
|
115
|
+
|
|
116
|
+
# Writes text to path under the root, making its folder, and returns path
|
|
117
|
+
def write_file path:, text:
|
|
118
|
+
full_path = File.join root, path
|
|
119
|
+
|
|
120
|
+
FileUtils.mkdir_p File.dirname(full_path)
|
|
121
|
+
File.write full_path, text
|
|
122
|
+
|
|
123
|
+
path
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# Every saved issue, linked to the other providers' saved issues Metron says are the same one
|
|
127
|
+
def linked_issues
|
|
128
|
+
all = saved_issues
|
|
129
|
+
unclaimed = all.to_h { [it.local_id, it] }
|
|
130
|
+
|
|
131
|
+
all.filter_map do |saved_issue|
|
|
132
|
+
next unless unclaimed.key? saved_issue.local_id
|
|
133
|
+
|
|
134
|
+
local_ids = [saved_issue.local_id, *saved_issue.linked_ids]
|
|
135
|
+
saved_issues = local_ids.filter_map { unclaimed.delete it }
|
|
136
|
+
|
|
137
|
+
LinkedIssues.new(saved_issues:)
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# Every issue saved from every provider
|
|
142
|
+
def saved_issues
|
|
143
|
+
PROVIDERS.flat_map do |provider|
|
|
144
|
+
store.ids(provider:, resource: 'issue').map do |id|
|
|
145
|
+
SavedIssue.new(provider:, id:, store:)
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# The number without leading zeros, so 001 and 1 are the same issue
|
|
151
|
+
def normalized_number number
|
|
152
|
+
number.to_s.strip.sub(/\A0+(?=\d)/, '')
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# The provider and its own ID in a comicinfo-local ID, such as metron-31660
|
|
156
|
+
def provider_and_id local_id
|
|
157
|
+
provider, id = local_id.to_s.split '-', 2
|
|
158
|
+
raise InvalidIssueIDError, local_id unless PROVIDERS.include?(provider) && id&.match?(/\A\d+\z/)
|
|
159
|
+
|
|
160
|
+
[provider, id]
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# The XDG data directory
|
|
164
|
+
def data_home environment
|
|
165
|
+
present(environment['XDG_DATA_HOME']) || File.join(Dir.home, '.local', 'share')
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# The text, or nil when it's blank
|
|
169
|
+
def present text
|
|
170
|
+
text unless text.to_s.strip.empty?
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: comicinfo-local
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Shane Becker
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: comicinfo
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '2.3'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '2.3'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: comicinfo-comic_vine
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0.3'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0.3'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: comicinfo-grand_comics_database
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0.3'
|
|
47
|
+
type: :runtime
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0.3'
|
|
54
|
+
- !ruby/object:Gem::Dependency
|
|
55
|
+
name: comicinfo-metron
|
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - ">="
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '0.3'
|
|
61
|
+
type: :runtime
|
|
62
|
+
prerelease: false
|
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '0.3'
|
|
68
|
+
- !ruby/object:Gem::Dependency
|
|
69
|
+
name: ostruct
|
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - "~>"
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '0.6'
|
|
75
|
+
type: :runtime
|
|
76
|
+
prerelease: false
|
|
77
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
78
|
+
requirements:
|
|
79
|
+
- - "~>"
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '0.6'
|
|
82
|
+
- !ruby/object:Gem::Dependency
|
|
83
|
+
name: stringex
|
|
84
|
+
requirement: !ruby/object:Gem::Requirement
|
|
85
|
+
requirements:
|
|
86
|
+
- - "~>"
|
|
87
|
+
- !ruby/object:Gem::Version
|
|
88
|
+
version: '2.8'
|
|
89
|
+
type: :runtime
|
|
90
|
+
prerelease: false
|
|
91
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
92
|
+
requirements:
|
|
93
|
+
- - "~>"
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: '2.8'
|
|
96
|
+
description: |
|
|
97
|
+
A metadata source for the comicinfo gem that needs no network: it keeps what Metron,
|
|
98
|
+
Comic Vine and the Grand Comics Database answered, and looks issues up in it later
|
|
99
|
+
email:
|
|
100
|
+
- veganstraightedge@gmail.com
|
|
101
|
+
executables: []
|
|
102
|
+
extensions: []
|
|
103
|
+
extra_rdoc_files: []
|
|
104
|
+
files:
|
|
105
|
+
- ".ruby-version"
|
|
106
|
+
- CHANGELOG.md
|
|
107
|
+
- CODE_OF_CONDUCT.md
|
|
108
|
+
- LICENSE.md
|
|
109
|
+
- README.md
|
|
110
|
+
- Rakefile
|
|
111
|
+
- lib/comicinfo/local.rb
|
|
112
|
+
- lib/comicinfo/local/comic_vine_url.rb
|
|
113
|
+
- lib/comicinfo/local/errors.rb
|
|
114
|
+
- lib/comicinfo/local/grand_comics_database_url.rb
|
|
115
|
+
- lib/comicinfo/local/key.rb
|
|
116
|
+
- lib/comicinfo/local/linked_issues.rb
|
|
117
|
+
- lib/comicinfo/local/metron_url.rb
|
|
118
|
+
- lib/comicinfo/local/record.rb
|
|
119
|
+
- lib/comicinfo/local/recorder.rb
|
|
120
|
+
- lib/comicinfo/local/response.rb
|
|
121
|
+
- lib/comicinfo/local/saved_issue.rb
|
|
122
|
+
- lib/comicinfo/local/search_key.rb
|
|
123
|
+
- lib/comicinfo/local/store.rb
|
|
124
|
+
- lib/comicinfo/local/version.rb
|
|
125
|
+
- sig/comicinfo/local.rbs
|
|
126
|
+
homepage: https://github.com/veganstraightedge/comicinfo-local
|
|
127
|
+
licenses:
|
|
128
|
+
- MIT
|
|
129
|
+
metadata:
|
|
130
|
+
allowed_push_host: https://rubygems.org
|
|
131
|
+
homepage_uri: https://github.com/veganstraightedge/comicinfo-local
|
|
132
|
+
source_code_uri: https://github.com/veganstraightedge/comicinfo-local
|
|
133
|
+
changelog_uri: https://github.com/veganstraightedge/comicinfo-local/blob/main/CHANGELOG.md
|
|
134
|
+
rubygems_mfa_required: 'true'
|
|
135
|
+
rdoc_options: []
|
|
136
|
+
require_paths:
|
|
137
|
+
- lib
|
|
138
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
139
|
+
requirements:
|
|
140
|
+
- - ">="
|
|
141
|
+
- !ruby/object:Gem::Version
|
|
142
|
+
version: 4.0.10
|
|
143
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
144
|
+
requirements:
|
|
145
|
+
- - ">="
|
|
146
|
+
- !ruby/object:Gem::Version
|
|
147
|
+
version: '0'
|
|
148
|
+
requirements: []
|
|
149
|
+
rubygems_version: 4.0.20
|
|
150
|
+
specification_version: 4
|
|
151
|
+
summary: Looks up comic details saved on disk from other sources, for the comicinfo
|
|
152
|
+
gem
|
|
153
|
+
test_files: []
|