dl-core 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/CHANGELOG.md +9 -0
- data/CODE_OF_CONDUCT.md +83 -0
- data/LICENSE.md +21 -0
- data/README.md +64 -0
- data/lib/dl/core/author.rb +11 -0
- data/lib/dl/core/cli.rb +135 -0
- data/lib/dl/core/client.rb +85 -0
- data/lib/dl/core/error.rb +6 -0
- data/lib/dl/core/http_error.rb +14 -0
- data/lib/dl/core/sidecar/json.rb +39 -0
- data/lib/dl/core/sidecar/markdown.rb +36 -0
- data/lib/dl/core/sidecar/yaml.rb +38 -0
- data/lib/dl/core/slug.rb +44 -0
- data/lib/dl/core/version.rb +5 -0
- data/lib/dl/core.rb +15 -0
- data/sig/dl/core.rbs +5 -0
- metadata +107 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: cf4c6083775de6f5cfb392c154831eab236fca2b9981dd0e43acd72c96c52d0f
|
|
4
|
+
data.tar.gz: 87ed15f963c4e8e6c571ec7ad9f2a10f9f4c79b1badb166911e4c106b7da22b2
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: f080be0fd4f22e3c96bc3d7c2581bf4cd4bdf6e95bc85f5cc501c0c5be0b379513e4a0ac96f509152a96bb074c556dc7956f2a902becc98220d14ba99c9cee31
|
|
7
|
+
data.tar.gz: af8f7747b957236d540661d1440e3b7eddd78f83f04a4017ec62da54498602a55e1f947bb43e60b8e9283ef96e2b8c1558ed7c790398fe4c0e66aa19c15d2cb5
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
## [0.1.0]
|
|
2
|
+
|
|
3
|
+
First version, extracted from arxiv-dl 0.3.0 and jstor-dl 0.1.0.
|
|
4
|
+
|
|
5
|
+
- `DL::Core::Client`: rate-limited HTTP client with timeouts and retries on 429/503. The User-Agent is now an argument, so each gem names itself.
|
|
6
|
+
- `DL::Core::Error` (base for every gem's errors) and `DL::Core::HTTPError`.
|
|
7
|
+
- `DL::Core::Author` and `DL::Core::Slug`.
|
|
8
|
+
- `DL::Core::Sidecar::YAML`, `JSON`, and `Markdown` metadata writers for any metadata `Data` object.
|
|
9
|
+
- `DL::Core::CLI`: the shared command line; each gem subclasses it with its name, defaults, identifier, and archive.
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# Contributor Covenant 3.0 Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We pledge to make our community welcoming, safe, and equitable for all.
|
|
6
|
+
|
|
7
|
+
We are committed to fostering an environment that respects and promotes the dignity, rights, and contributions of all individuals, regardless of characteristics including race, ethnicity, caste, color, age, physical characteristics, neurodiversity, disability, sex or gender, gender identity or expression, sexual orientation, language, philosophy or religion, national or social origin, socio-economic position, level of education, or other status. The same privileges of participation are extended to everyone who participates in good faith and in accordance with this Covenant.
|
|
8
|
+
|
|
9
|
+
## Encouraged Behaviors
|
|
10
|
+
|
|
11
|
+
While acknowledging differences in social norms, we all strive to meet our community's expectations for positive behavior. We also understand that our words and actions may be interpreted differently than we intend based on culture, background, or native language.
|
|
12
|
+
|
|
13
|
+
With these considerations in mind, we agree to behave mindfully toward each other and act in ways that center our shared values, including:
|
|
14
|
+
|
|
15
|
+
1. Respecting the **purpose of our community**, our activities, and our ways of gathering.
|
|
16
|
+
2. Engaging **kindly and honestly** with others.
|
|
17
|
+
3. Respecting **different viewpoints** and experiences.
|
|
18
|
+
4. **Taking responsibility** for our actions and contributions.
|
|
19
|
+
5. Gracefully giving and accepting **constructive feedback**.
|
|
20
|
+
6. Committing to **repairing harm** when it occurs.
|
|
21
|
+
7. Behaving in other ways that promote and sustain the **well-being of our community**.
|
|
22
|
+
|
|
23
|
+
## Restricted Behaviors
|
|
24
|
+
|
|
25
|
+
We agree to restrict the following behaviors in our community. Instances, threats, and promotion of these behaviors are violations of this Code of Conduct.
|
|
26
|
+
|
|
27
|
+
1. **Harassment.** Violating explicitly expressed boundaries or engaging in unnecessary personal attention after any clear request to stop.
|
|
28
|
+
2. **Character attacks.** Making insulting, demeaning, or pejorative comments directed at a community member or group of people.
|
|
29
|
+
3. **Stereotyping or discrimination.** Characterizing anyone’s personality or behavior on the basis of immutable identities or traits.
|
|
30
|
+
4. **Sexualization.** Behaving in a way that would generally be considered inappropriately intimate in the context or purpose of the community.
|
|
31
|
+
5. **Violating confidentiality**. Sharing or acting on someone's personal or private information without their permission.
|
|
32
|
+
6. **Endangerment.** Causing, encouraging, or threatening violence or other harm toward any person or group.
|
|
33
|
+
7. Behaving in other ways that **threaten the well-being** of our community.
|
|
34
|
+
|
|
35
|
+
### Other Restrictions
|
|
36
|
+
|
|
37
|
+
1. **Misleading identity.** Impersonating someone else for any reason, or pretending to be someone else to evade enforcement actions.
|
|
38
|
+
2. **Failing to credit sources.** Not properly crediting the sources of content you contribute.
|
|
39
|
+
3. **Promotional materials**. Sharing marketing or other commercial content in a way that is outside the norms of the community.
|
|
40
|
+
4. **Irresponsible communication.** Failing to responsibly present content which includes, links or describes any other restricted behaviors.
|
|
41
|
+
|
|
42
|
+
## Reporting an Issue
|
|
43
|
+
|
|
44
|
+
Tensions can occur between community members even when they are trying their best to collaborate. Not every conflict represents a code of conduct violation, and this Code of Conduct reinforces encouraged behaviors and norms that can help avoid conflicts and minimize harm.
|
|
45
|
+
|
|
46
|
+
When an incident does occur, it is important to report it promptly. To report a possible violation, **email the maintainer at [veganstraightedge@gmail.com](mailto:veganstraightedge@gmail.com).**
|
|
47
|
+
|
|
48
|
+
Community Moderators take reports of violations seriously and will make every effort to respond in a timely manner. They will investigate all reports of code of conduct violations, reviewing messages, logs, and recordings, or interviewing witnesses and other participants. Community Moderators will keep investigation and enforcement actions as transparent as possible while prioritizing safety and confidentiality. In order to honor these values, enforcement actions are carried out in private with the involved parties, but communicating to the whole community may be part of a mutually agreed upon resolution.
|
|
49
|
+
|
|
50
|
+
## Addressing and Repairing Harm
|
|
51
|
+
|
|
52
|
+
If an investigation by the Community Moderators finds that this Code of Conduct has been violated, the following enforcement ladder may be used to determine how best to repair harm, based on the incident's impact on the individuals involved and the community as a whole. Depending on the severity of a violation, lower rungs on the ladder may be skipped.
|
|
53
|
+
|
|
54
|
+
1. Warning
|
|
55
|
+
1. Event: A violation involving a single incident or series of incidents.
|
|
56
|
+
2. Consequence: A private, written warning from the Community Moderators.
|
|
57
|
+
3. Repair: Examples of repair include a private written apology, acknowledgement of responsibility, and seeking clarification on expectations.
|
|
58
|
+
2. Temporarily Limited Activities
|
|
59
|
+
1. Event: A repeated incidence of a violation that previously resulted in a warning, or the first incidence of a more serious violation.
|
|
60
|
+
2. Consequence: A private, written warning with a time-limited cooldown period designed to underscore the seriousness of the situation and give the community members involved time to process the incident. The cooldown period may be limited to particular communication channels or interactions with particular community members.
|
|
61
|
+
3. Repair: Examples of repair may include making an apology, using the cooldown period to reflect on actions and impact, and being thoughtful about re-entering community spaces after the period is over.
|
|
62
|
+
3. Temporary Suspension
|
|
63
|
+
1. Event: A pattern of repeated violation which the Community Moderators have tried to address with warnings, or a single serious violation.
|
|
64
|
+
2. Consequence: A private written warning with conditions for return from suspension. In general, temporary suspensions give the person being suspended time to reflect upon their behavior and possible corrective actions.
|
|
65
|
+
3. Repair: Examples of repair include respecting the spirit of the suspension, meeting the specified conditions for return, and being thoughtful about how to reintegrate with the community when the suspension is lifted.
|
|
66
|
+
4. Permanent Ban
|
|
67
|
+
1. Event: A pattern of repeated code of conduct violations that other steps on the ladder have failed to resolve, or a violation so serious that the Community Moderators determine there is no way to keep the community safe with this person as a member.
|
|
68
|
+
2. Consequence: Access to all community spaces, tools, and communication channels is removed. In general, permanent bans should be rarely used, should have strong reasoning behind them, and should only be resorted to if working through other remedies has failed to change the behavior.
|
|
69
|
+
3. Repair: There is no possible repair in cases of this severity.
|
|
70
|
+
|
|
71
|
+
This enforcement ladder is intended as a guideline. It does not limit the ability of Community Managers to use their discretion and judgment, in keeping with the best interests of our community.
|
|
72
|
+
|
|
73
|
+
## Scope
|
|
74
|
+
|
|
75
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public or other spaces. Examples of representing our community include using an official email address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
|
76
|
+
|
|
77
|
+
## Attribution
|
|
78
|
+
|
|
79
|
+
This Code of Conduct is adapted from the Contributor Covenant, version 3.0, permanently available at [https://www.contributor-covenant.org/version/3/0/](https://www.contributor-covenant.org/version/3/0/).
|
|
80
|
+
|
|
81
|
+
Contributor Covenant is stewarded by the Organization for Ethical Source and licensed under CC BY-SA 4.0. To view a copy of this license, visit [https://creativecommons.org/licenses/by-sa/4.0/](https://creativecommons.org/licenses/by-sa/4.0/)
|
|
82
|
+
|
|
83
|
+
For answers to common questions about Contributor Covenant, see the FAQ at [https://www.contributor-covenant.org/faq](https://www.contributor-covenant.org/faq). Translations are provided at [https://www.contributor-covenant.org/translations](https://www.contributor-covenant.org/translations). Additional enforcement and community guideline resources can be found at [https://www.contributor-covenant.org/resources](https://www.contributor-covenant.org/resources). The enforcement ladder was inspired by the work of [Mozilla’s code of conduct team](https://github.com/mozilla/inclusion).
|
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,64 @@
|
|
|
1
|
+
# dl-core
|
|
2
|
+
|
|
3
|
+
Shared foundation for the `<site>-dl` gems that build offline archives: [arxiv-dl](https://github.com/xoengineering/arxiv-dl), [jstor-dl](https://github.com/xoengineering/jstor-dl), and more.
|
|
4
|
+
|
|
5
|
+
## What's in it
|
|
6
|
+
|
|
7
|
+
| Class | Purpose |
|
|
8
|
+
| -------------------------------------- | ------------------------------------------------------------------------------------------------- |
|
|
9
|
+
| `DL::Core::Client` | Polite HTTP: User-Agent naming the gem, minimum interval between requests, timeouts, retries on 429/503 (honoring `Retry-After`) |
|
|
10
|
+
| `DL::Core::Error`, `DL::Core::HTTPError` | Base error for every gem; `HTTPError` for non-success responses (`status`, `url`) |
|
|
11
|
+
| `DL::Core::Author` | `name` and `affiliations` |
|
|
12
|
+
| `DL::Core::Slug` | Title → URL-safe slug, truncated at a word boundary |
|
|
13
|
+
| `DL::Core::Sidecar::YAML`, `JSON` | `metadata.yaml` / `metadata.json` from any metadata `Data` object |
|
|
14
|
+
| `DL::Core::Sidecar::Markdown` | `metadata.md`: YAML frontmatter from the metadata, plus a body each gem writes |
|
|
15
|
+
| `DL::Core::CLI` | The shared command line: targets as args or `--input FILE\|-`, `-p`, `--rate-limit`, `-v`, `-q`, per-target errors, exit 1 on any failure |
|
|
16
|
+
|
|
17
|
+
## Building a `<site>-dl` gem on it
|
|
18
|
+
|
|
19
|
+
Each gem's errors subclass `DL::Core::Error`, and its CLI subclasses `DL::Core::CLI`:
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
module Example
|
|
23
|
+
module Downloader
|
|
24
|
+
class Error < DL::Core::Error; end
|
|
25
|
+
|
|
26
|
+
class CLI < DL::Core::CLI
|
|
27
|
+
def program = 'example-dl'
|
|
28
|
+
|
|
29
|
+
def target_name = 'EXAMPLE_ID_OR_URL'
|
|
30
|
+
|
|
31
|
+
# EXAMPLE_DOWNLOAD_PATH, EXAMPLE_RATE_LIMIT
|
|
32
|
+
def env_prefix = 'EXAMPLE'
|
|
33
|
+
|
|
34
|
+
def default_path = File.join(Dir.home, 'Downloads', 'Example_Papers')
|
|
35
|
+
|
|
36
|
+
def version = VERSION
|
|
37
|
+
|
|
38
|
+
def user_agent = "example-dl/#{VERSION} (+https://github.com/xoengineering/example-dl)"
|
|
39
|
+
|
|
40
|
+
def identifier_for(target) = Identifier.new(target)
|
|
41
|
+
|
|
42
|
+
def archive_for(identifier, root:, client:) = Archive.new(identifier, root:, client:)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
`identifier_for` raises a `DL::Core::Error` subclass for input the gem doesn't recognize; the object it returns responds to `id`. `archive_for` returns an object whose `run` archives the paper and returns its folder.
|
|
49
|
+
|
|
50
|
+
## Development
|
|
51
|
+
|
|
52
|
+
```sh
|
|
53
|
+
script/setup # install dependencies
|
|
54
|
+
script/test # run specs and rubocop
|
|
55
|
+
script/console # interactive prompt
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## License
|
|
59
|
+
|
|
60
|
+
MIT — see [LICENSE.md](LICENSE.md).
|
|
61
|
+
|
|
62
|
+
## Code of Conduct
|
|
63
|
+
|
|
64
|
+
This project follows the [Contributor Covenant](https://www.contributor-covenant.org/version/3/0/) 3.0 — see [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md).
|
data/lib/dl/core/cli.rb
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
require 'optparse'
|
|
2
|
+
|
|
3
|
+
module DL
|
|
4
|
+
module Core
|
|
5
|
+
# The shared <site>-dl command line. Each gem subclasses it and defines:
|
|
6
|
+
#
|
|
7
|
+
# program "jstor-dl"
|
|
8
|
+
# target_name "JSTOR_ID_OR_URL", for the usage line
|
|
9
|
+
# env_prefix "JSTOR", for JSTOR_DOWNLOAD_PATH and JSTOR_RATE_LIMIT
|
|
10
|
+
# default_path where downloads go without -p or the ENV var
|
|
11
|
+
# version the gem's version, for --version
|
|
12
|
+
# user_agent for requests, naming the gem
|
|
13
|
+
# identifier_for(target) parses one target; raises an Error subclass if invalid
|
|
14
|
+
# archive_for(identifier, root:, client:) an object whose #run archives it and returns its folder
|
|
15
|
+
class CLI
|
|
16
|
+
def initialize argv, stderr: $stderr, stdin: $stdin, stdout: $stdout
|
|
17
|
+
@argv = argv
|
|
18
|
+
@stderr = stderr
|
|
19
|
+
@stdin = stdin
|
|
20
|
+
@stdout = stdout
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def run
|
|
24
|
+
options = parse
|
|
25
|
+
return options.fetch(:exit_status) if options.key? :exit_status
|
|
26
|
+
|
|
27
|
+
return error_with usage if options[:targets].empty?
|
|
28
|
+
return error_with conflict if options[:verbose] && options[:quiet]
|
|
29
|
+
|
|
30
|
+
failures = download_each options
|
|
31
|
+
failures.zero? ? 0 : 1
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def program = raise(NotImplementedError, "#{self.class} must define #program")
|
|
35
|
+
|
|
36
|
+
def target_name = raise(NotImplementedError, "#{self.class} must define #target_name")
|
|
37
|
+
|
|
38
|
+
def env_prefix = raise(NotImplementedError, "#{self.class} must define #env_prefix")
|
|
39
|
+
|
|
40
|
+
def default_path = raise(NotImplementedError, "#{self.class} must define #default_path")
|
|
41
|
+
|
|
42
|
+
def version = raise(NotImplementedError, "#{self.class} must define #version")
|
|
43
|
+
|
|
44
|
+
def user_agent = raise(NotImplementedError, "#{self.class} must define #user_agent")
|
|
45
|
+
|
|
46
|
+
def identifier_for(_target) = raise(NotImplementedError, "#{self.class} must define #identifier_for")
|
|
47
|
+
|
|
48
|
+
def archive_for(_identifier, root:, client:) = raise(NotImplementedError, "#{self.class} must define #archive_for")
|
|
49
|
+
|
|
50
|
+
def client_for(rate_limit:, log:) = Client.new(user_agent:, rate_limit:, log:)
|
|
51
|
+
|
|
52
|
+
private
|
|
53
|
+
|
|
54
|
+
def usage
|
|
55
|
+
"Usage: #{program} [options] <#{target_name}> [<#{target_name}>...]"
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def conflict
|
|
59
|
+
'-v and -q are mutually exclusive'
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def parse
|
|
63
|
+
options = { targets: [], verbose: false, quiet: false }
|
|
64
|
+
parser = build_parser options
|
|
65
|
+
|
|
66
|
+
begin
|
|
67
|
+
parser.parse! @argv
|
|
68
|
+
options[:targets] = @argv + input_targets(options[:input])
|
|
69
|
+
rescue OptionParser::ParseError, SystemCallError => e
|
|
70
|
+
@stderr.puts e.message
|
|
71
|
+
return { exit_status: 1 }
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
options[:path] ||= ENV["#{env_prefix}_DOWNLOAD_PATH"] || default_path
|
|
75
|
+
options[:rate_limit] ||= (ENV["#{env_prefix}_RATE_LIMIT"] || Client::DEFAULT_RATE_LIMIT).to_i
|
|
76
|
+
options
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def build_parser options
|
|
80
|
+
OptionParser.new do |parser|
|
|
81
|
+
parser.banner = usage
|
|
82
|
+
parser.on('-i FILE', '--input FILE') { |value| options[:input] = value }
|
|
83
|
+
parser.on('-p PATH', '--path PATH') { |value| options[:path] = value }
|
|
84
|
+
parser.on('--rate-limit SECONDS', Integer) { |value| options[:rate_limit] = value }
|
|
85
|
+
parser.on('-v', '--verbose') { options[:verbose] = true }
|
|
86
|
+
parser.on('-q', '--quiet') { options[:quiet] = true }
|
|
87
|
+
parser.on('--version') do
|
|
88
|
+
@stdout.puts version
|
|
89
|
+
options[:exit_status] = 0
|
|
90
|
+
end
|
|
91
|
+
parser.on('-h', '--help') do
|
|
92
|
+
@stdout.puts parser.help
|
|
93
|
+
options[:exit_status] = 0
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# one target per line from FILE, or stdin for "-"; blank lines and # comments skipped
|
|
99
|
+
def input_targets input
|
|
100
|
+
return [] if input.nil?
|
|
101
|
+
|
|
102
|
+
text = input == '-' ? @stdin.read : File.read(input)
|
|
103
|
+
text.lines.map(&:strip).reject { it.empty? || it.start_with?('#') }
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def error_with message
|
|
107
|
+
@stderr.puts message
|
|
108
|
+
1
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def download_each options
|
|
112
|
+
client = client_for rate_limit: options[:rate_limit], log: (options[:verbose] ? @stdout : nil)
|
|
113
|
+
|
|
114
|
+
failures = 0
|
|
115
|
+
options[:targets].each do |target|
|
|
116
|
+
failures += 1 unless download_one(target, client:, options:)
|
|
117
|
+
end
|
|
118
|
+
failures
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# true on success; reports the failure and returns false otherwise
|
|
122
|
+
def download_one target, client:, options:
|
|
123
|
+
identifier = identifier_for target
|
|
124
|
+
@stdout.puts "==> Downloading #{identifier.id}" if options[:verbose]
|
|
125
|
+
|
|
126
|
+
path = archive_for(identifier, root: options[:path], client: client).run
|
|
127
|
+
@stdout.puts path unless options[:quiet]
|
|
128
|
+
true
|
|
129
|
+
rescue Error, HTTP::Error => e
|
|
130
|
+
@stderr.puts "#{target}: #{e.message}"
|
|
131
|
+
false
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
require 'http'
|
|
2
|
+
|
|
3
|
+
module DL
|
|
4
|
+
module Core
|
|
5
|
+
# Polite HTTP: a User-Agent naming the calling gem, a minimum interval between
|
|
6
|
+
# requests, timeouts, retries on 429/503, and HTTPError on other failures.
|
|
7
|
+
class Client
|
|
8
|
+
DEFAULT_RATE_LIMIT = 3
|
|
9
|
+
TIMEOUTS = { connect: 10, read: 60, write: 10 }.freeze # seconds, per operation
|
|
10
|
+
MAX_RETRIES = 3
|
|
11
|
+
RETRY_BACKOFF = 10 # seconds before the first retry. doubles on each retry.
|
|
12
|
+
RETRYABLE_STATUSES = [429, 503].freeze
|
|
13
|
+
|
|
14
|
+
attr_reader :rate_limit, :user_agent
|
|
15
|
+
|
|
16
|
+
# user_agent names the calling gem, e.g. "jstor-dl/0.1.0 (+https://github.com/xoengineering/jstor-dl)"
|
|
17
|
+
def initialize user_agent:, log: nil, rate_limit: DEFAULT_RATE_LIMIT
|
|
18
|
+
@log = log
|
|
19
|
+
@rate_limit = rate_limit
|
|
20
|
+
@user_agent = user_agent
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def get url
|
|
24
|
+
retries = 0
|
|
25
|
+
|
|
26
|
+
loop do
|
|
27
|
+
response = request url
|
|
28
|
+
return response if response.status.success?
|
|
29
|
+
raise http_error(url, response) unless retryable? response, retries
|
|
30
|
+
|
|
31
|
+
retries += 1
|
|
32
|
+
wait_before_retry response, retries
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
def request url
|
|
39
|
+
throttle
|
|
40
|
+
response = HTTP.timeout(TIMEOUTS).headers('User-Agent' => user_agent).follow.get(url)
|
|
41
|
+
@last_request_at = Time.now
|
|
42
|
+
log_request url, response
|
|
43
|
+
response
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def http_error url, response
|
|
47
|
+
HTTPError.new status: response.status.code, url: url, reason: response.status.reason
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def retryable? response, retries
|
|
51
|
+
RETRYABLE_STATUSES.include?(response.status.code) && retries < MAX_RETRIES
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def wait_before_retry response, retries
|
|
55
|
+
seconds = retry_after(response) || (RETRY_BACKOFF * (2**(retries - 1)))
|
|
56
|
+
@log&.puts "==> #{response.status}. Retrying in #{seconds}s"
|
|
57
|
+
sleep seconds
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Retry-After in delay-seconds form. The HTTP-date form falls back to backoff.
|
|
61
|
+
def retry_after response
|
|
62
|
+
value = response.headers['Retry-After']
|
|
63
|
+
return if value.nil?
|
|
64
|
+
|
|
65
|
+
Integer(value, exception: false)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def log_request url, response
|
|
69
|
+
return if @log.nil?
|
|
70
|
+
|
|
71
|
+
@log.puts "==> GET #{url} (#{response.body.to_s.bytesize} bytes)"
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def throttle
|
|
75
|
+
return if @rate_limit.zero?
|
|
76
|
+
return if @last_request_at.nil?
|
|
77
|
+
|
|
78
|
+
elapsed = Time.now - @last_request_at
|
|
79
|
+
return if elapsed >= @rate_limit
|
|
80
|
+
|
|
81
|
+
sleep(@rate_limit - elapsed)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module DL
|
|
2
|
+
module Core
|
|
3
|
+
class HTTPError < Error
|
|
4
|
+
attr_reader :status, :url
|
|
5
|
+
|
|
6
|
+
def initialize status:, url:, reason: nil
|
|
7
|
+
@status = status
|
|
8
|
+
@url = url
|
|
9
|
+
|
|
10
|
+
super("GET #{url} failed: #{[status, reason].compact.join ' '}")
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
require 'date'
|
|
2
|
+
require 'fileutils'
|
|
3
|
+
require 'json'
|
|
4
|
+
|
|
5
|
+
module DL
|
|
6
|
+
module Core
|
|
7
|
+
module Sidecar
|
|
8
|
+
# metadata.json: every field of a metadata Data object, dates as ISO 8601 strings
|
|
9
|
+
class JSON
|
|
10
|
+
FILENAME = 'metadata.json'.freeze
|
|
11
|
+
|
|
12
|
+
def initialize metadata
|
|
13
|
+
@metadata = metadata
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def to_s
|
|
17
|
+
"#{::JSON.pretty_generate(serialize(@metadata.to_h))}\n"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def write to:
|
|
21
|
+
FileUtils.mkdir_p to
|
|
22
|
+
File.write File.join(to, FILENAME), to_s
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def serialize object
|
|
28
|
+
case object
|
|
29
|
+
when Hash then object.to_h { |key, value| [key.to_s, serialize(value)] }
|
|
30
|
+
when Array then object.map { |item| serialize item }
|
|
31
|
+
when Data then serialize object.to_h
|
|
32
|
+
when Date, Time then object.iso8601
|
|
33
|
+
else object
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
require 'fileutils'
|
|
2
|
+
|
|
3
|
+
module DL
|
|
4
|
+
module Core
|
|
5
|
+
module Sidecar
|
|
6
|
+
# metadata.md: YAML frontmatter from the metadata (plus any extras), then a
|
|
7
|
+
# Markdown body that each gem writes for its own source
|
|
8
|
+
class Markdown
|
|
9
|
+
FILENAME = 'metadata.md'.freeze
|
|
10
|
+
|
|
11
|
+
def initialize metadata, body:, extras: {}
|
|
12
|
+
@metadata = metadata
|
|
13
|
+
@body = body
|
|
14
|
+
@extras = extras
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def to_s
|
|
18
|
+
"#{frontmatter}\n#{@body}"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def write to:
|
|
22
|
+
FileUtils.mkdir_p to
|
|
23
|
+
File.write File.join(to, FILENAME), to_s
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
def frontmatter
|
|
29
|
+
yaml = YAML.new(@metadata, extras: @extras).to_s.delete_prefix "---\n"
|
|
30
|
+
|
|
31
|
+
"---\n#{yaml}---"
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
require 'fileutils'
|
|
2
|
+
require 'yaml'
|
|
3
|
+
|
|
4
|
+
module DL
|
|
5
|
+
module Core
|
|
6
|
+
module Sidecar
|
|
7
|
+
# metadata.yaml: every field of a metadata Data object (plus any extras), in field order
|
|
8
|
+
class YAML
|
|
9
|
+
FILENAME = 'metadata.yaml'.freeze
|
|
10
|
+
|
|
11
|
+
def initialize metadata, extras: {}
|
|
12
|
+
@metadata = metadata
|
|
13
|
+
@extras = extras
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def to_s
|
|
17
|
+
::YAML.dump stringify(@metadata.to_h.merge(@extras))
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def write to:
|
|
21
|
+
FileUtils.mkdir_p to
|
|
22
|
+
File.write File.join(to, FILENAME), to_s
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def stringify object
|
|
28
|
+
case object
|
|
29
|
+
when Hash then object.to_h { |key, value| [key.to_s, stringify(value)] }
|
|
30
|
+
when Array then object.map { |item| stringify item }
|
|
31
|
+
when Data then stringify object.to_h
|
|
32
|
+
else object
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
data/lib/dl/core/slug.rb
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require 'stringex'
|
|
2
|
+
|
|
3
|
+
module DL
|
|
4
|
+
module Core
|
|
5
|
+
class Slug
|
|
6
|
+
MAX_LENGTH = 80
|
|
7
|
+
|
|
8
|
+
TEX_INLINE_MATH = /\$[^$]*\$/ # $...$
|
|
9
|
+
TEX_DISPLAY_MATH = /\\\(.*?\\\)|\\\[.*?\\\]/m # \(...\) or \[...\]
|
|
10
|
+
TEX_COMMAND = /\\[a-zA-Z]+\*?/ # \emph, \alpha, etc.
|
|
11
|
+
|
|
12
|
+
def initialize title
|
|
13
|
+
@title = title
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def to_s
|
|
17
|
+
truncate strip_tex(@title).to_url
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
def strip_tex string
|
|
23
|
+
string
|
|
24
|
+
.gsub(TEX_INLINE_MATH, ' ')
|
|
25
|
+
.gsub(TEX_DISPLAY_MATH, ' ')
|
|
26
|
+
.gsub(TEX_COMMAND, ' ')
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def truncate slug
|
|
30
|
+
return slug if slug.length <= MAX_LENGTH
|
|
31
|
+
|
|
32
|
+
words = slug.split '-'
|
|
33
|
+
result = +''
|
|
34
|
+
words.each do |word|
|
|
35
|
+
break if result.length + 1 + word.length > MAX_LENGTH
|
|
36
|
+
|
|
37
|
+
result << '-' unless result.empty?
|
|
38
|
+
result << word
|
|
39
|
+
end
|
|
40
|
+
result
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
data/lib/dl/core.rb
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
require_relative 'core/author'
|
|
2
|
+
require_relative 'core/cli'
|
|
3
|
+
require_relative 'core/client'
|
|
4
|
+
require_relative 'core/error' # before errors below that subclass Error
|
|
5
|
+
require_relative 'core/http_error' # after error
|
|
6
|
+
require_relative 'core/sidecar/json'
|
|
7
|
+
require_relative 'core/sidecar/markdown'
|
|
8
|
+
require_relative 'core/sidecar/yaml'
|
|
9
|
+
require_relative 'core/slug'
|
|
10
|
+
require_relative 'core/version'
|
|
11
|
+
|
|
12
|
+
module DL
|
|
13
|
+
module Core
|
|
14
|
+
end
|
|
15
|
+
end
|
data/sig/dl/core.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: dl-core
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Shane Becker
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: http
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '6.0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '6.0'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: ostruct
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - "~>"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0.6'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0.6'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: stringex
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - "~>"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '2.8'
|
|
47
|
+
type: :runtime
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - "~>"
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '2.8'
|
|
54
|
+
description: |
|
|
55
|
+
Polite rate-limited HTTP client with retries and timeouts, sidecar metadata writers,
|
|
56
|
+
title slugs, and a shared CLI for the <site>-dl family of gems that build offline archives.
|
|
57
|
+
email:
|
|
58
|
+
- veganstraightedge@gmail.com
|
|
59
|
+
executables: []
|
|
60
|
+
extensions: []
|
|
61
|
+
extra_rdoc_files: []
|
|
62
|
+
files:
|
|
63
|
+
- CHANGELOG.md
|
|
64
|
+
- CODE_OF_CONDUCT.md
|
|
65
|
+
- LICENSE.md
|
|
66
|
+
- README.md
|
|
67
|
+
- lib/dl/core.rb
|
|
68
|
+
- lib/dl/core/author.rb
|
|
69
|
+
- lib/dl/core/cli.rb
|
|
70
|
+
- lib/dl/core/client.rb
|
|
71
|
+
- lib/dl/core/error.rb
|
|
72
|
+
- lib/dl/core/http_error.rb
|
|
73
|
+
- lib/dl/core/sidecar/json.rb
|
|
74
|
+
- lib/dl/core/sidecar/markdown.rb
|
|
75
|
+
- lib/dl/core/sidecar/yaml.rb
|
|
76
|
+
- lib/dl/core/slug.rb
|
|
77
|
+
- lib/dl/core/version.rb
|
|
78
|
+
- sig/dl/core.rbs
|
|
79
|
+
homepage: https://github.com/xoengineering/dl-core
|
|
80
|
+
licenses:
|
|
81
|
+
- MIT
|
|
82
|
+
metadata:
|
|
83
|
+
allowed_push_host: https://rubygems.org
|
|
84
|
+
homepage_uri: https://github.com/xoengineering/dl-core
|
|
85
|
+
source_code_uri: https://github.com/xoengineering/dl-core
|
|
86
|
+
bug_tracker_uri: https://github.com/xoengineering/dl-core/issues
|
|
87
|
+
changelog_uri: https://github.com/xoengineering/dl-core/blob/main/CHANGELOG.md
|
|
88
|
+
rubygems_mfa_required: 'true'
|
|
89
|
+
rdoc_options: []
|
|
90
|
+
require_paths:
|
|
91
|
+
- lib
|
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - ">="
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: 4.0.7
|
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
98
|
+
requirements:
|
|
99
|
+
- - ">="
|
|
100
|
+
- !ruby/object:Gem::Version
|
|
101
|
+
version: '0'
|
|
102
|
+
requirements: []
|
|
103
|
+
rubygems_version: 4.0.21
|
|
104
|
+
specification_version: 4
|
|
105
|
+
summary: Shared foundation for the <site>-dl offline-archive gems (arxiv-dl, jstor-dl,
|
|
106
|
+
and more).
|
|
107
|
+
test_files: []
|