ntrs-dl 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 683e3b93f881d91910fcfd676d3259d5872bb2c1e081b62de11b2ddea2ddc763
4
+ data.tar.gz: d9ce0c9f2f890e4ef8b450fc1a2d12d7eb8bcbcd5c44c5ad6f5a1d745321dce5
5
+ SHA512:
6
+ metadata.gz: 623f8dac905e12ab4fdc8a839921bba87c550e7fa3391cc17644173f7e43092f57b73aea6969589a59593b4f0e3058d501a1570b6835fa2412defa1562bcd667
7
+ data.tar.gz: 54ea68152ed0b6417c6979d16d61761701e0d0128a173a354ae41d8931b786228b5b013d55a5c99eedd1127fb64ef69b6dc5737d1dede7826b035336314f9e05
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ ## [0.1.0]
2
+
3
+ First version. Per-citation offline archive of NASA Technical Reports Server documents, built on [dl-core](https://github.com/xoengineering/dl-core).
4
+
5
+ - Accepts NTRS IDs and ntrs.nasa.gov citation, API, and download URLs.
6
+ - Saves each file NTRS lists, its plaintext when NTRS has it, NTRS's citation record verbatim, and four sidecar metadata files. Authors carry their organizations as affiliations.
7
+ - Layout: `YYYY/MM/DD/<subject>/<ntrs-id>-<slug>/`.
8
+ - Archives only citations marked public with files to download. Others raise `NTRS::Downloader::NotAvailable`. Unknown IDs raise `NTRS::Downloader::PaperNotFound`.
9
+ - The README quotes NASA's expectation that copies of withdrawn documents be removed. `ntrs-dl` never deletes anything, so that is left to the user.
@@ -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,122 @@
1
+ # ntrs-dl
2
+
3
+ Download reports and metadata from the [NASA Technical Reports Server](https://ntrs.nasa.gov) (NTRS) for offline archives.
4
+
5
+ For each citation, `ntrs-dl` saves:
6
+
7
+ - Each file NTRS lists for it (usually one PDF), under its NTRS name
8
+ - The plaintext NTRS extracted from each file, when there is one
9
+ - NTRS's own citation record (`ntrs.json`), verbatim
10
+ - Four sidecar metadata files: `metadata.md`, `metadata.yaml`, `metadata.json`, `metadata.bib`
11
+
12
+ Only citations NTRS marks as public (`distribution: PUBLIC`) with files to download are archived.
13
+
14
+ ## NASA's terms: read before using
15
+
16
+ Using the NTRS API means accepting NASA's [STI OpenAPI terms](https://sti.nasa.gov/docs/STI_Open_API_Documentation_20210426.pdf). In particular:
17
+
18
+ > When a document or record is redistributed and becomes unavailable, it is expected that any copies be removed and the data be resynced.
19
+
20
+ NASA expects you to check the Redistributions endpoint (`GET https://ntrs.nasa.gov/api/citations/redistributions`) weekly at minimum, and to remove local copies of documents that become unavailable. **`ntrs-dl` does not do this.** It builds an offline archive and never deletes anything. Keeping your archive in line with NASA's expectation is up to you.
21
+
22
+ NASA also asks harvesters to cite NASA STI Compliance and Distribution Services as the source, and to share the citation URL (`https://ntrs.nasa.gov/citations/<id>`) rather than copies where possible. `ntrs-dl` writes both into every `metadata.md`.
23
+
24
+ The API allows 500 requests per 15 minutes. `ntrs-dl`'s default of one request every 3 seconds stays under that.
25
+
26
+ ## Installation
27
+
28
+ ```sh
29
+ gem install ntrs-dl
30
+ ```
31
+
32
+ ## CLI usage
33
+
34
+ ```sh
35
+ ntrs-dl <NTRS_ID_OR_URL> [<NTRS_ID_OR_URL>...]
36
+ ```
37
+
38
+ Accepted input forms:
39
+
40
+ | Form | Example |
41
+ | ---------------- | --------------------------------------------------------------------------- |
42
+ | NTRS ID | `19700020471` |
43
+ | Citation URL | `https://ntrs.nasa.gov/citations/19700020471` |
44
+ | API URL | `https://ntrs.nasa.gov/api/citations/19700020471` |
45
+ | Download URL | `https://ntrs.nasa.gov/api/citations/19700020471/downloads/19700020471.pdf` |
46
+
47
+ ### Flags
48
+
49
+ | Flag | Description |
50
+ | ------------------------- | ----------------------------------------------------------------------------- |
51
+ | `-i FILE`, `--input FILE` | Read IDs/URLs from FILE, one per line (`-` for stdin, blanks and `#` skipped) |
52
+ | `-p PATH`, `--path PATH` | Root download directory |
53
+ | `--rate-limit SECONDS` | Seconds between HTTP requests (`0` disables throttling) |
54
+ | `-v`, `--verbose` | Print step lines and per-request URL/byte logs to stdout |
55
+ | `-q`, `--quiet` | Print nothing to stdout. Errors still go to stderr. |
56
+ | `--version` | Print the gem version and exit |
57
+ | `-h`, `--help` | Print help and exit |
58
+
59
+ `-v` and `-q` are mutually exclusive.
60
+
61
+ ### Environment variables
62
+
63
+ | Variable | Effect |
64
+ | -------------------- | ---------------------------------------------------------------- |
65
+ | `NTRS_DOWNLOAD_PATH` | Root download directory (default: `$HOME/Downloads/NTRS_Papers`) |
66
+ | `NTRS_RATE_LIMIT` | Seconds between HTTP requests (default: `3`, `0` disables) |
67
+
68
+ Precedence: CLI flag, then ENV var, then default.
69
+
70
+ ### Errors and exit status
71
+
72
+ A target that fails (unrecognized ID, no such citation, no public files, HTTP error, network failure) is reported on stderr as `<target>: <message>`, and the remaining targets still download. Exit status is `0` when every target succeeds and `1` when any fails.
73
+
74
+ ## Output layout
75
+
76
+ ```txt
77
+ $NTRS_DOWNLOAD_PATH/ # default: $HOME/Downloads/NTRS_Papers
78
+ YYYY/MM/DD/<subject>/<ntrs-id>-<slug>/
79
+ <ntrs-id>.pdf # each file NTRS lists, under its NTRS name
80
+ <ntrs-id>.txt # NTRS's plaintext of it
81
+ ntrs.json # NTRS's citation record, verbatim
82
+ metadata.md # YAML frontmatter + Markdown body
83
+ metadata.yaml
84
+ metadata.json
85
+ metadata.bib # synthesized @techreport
86
+ ```
87
+
88
+ `YYYY/MM/DD` is the publication date, or the submission date when there is none. `<subject>` is the first NTRS subject category as a slug (`space-sciences`). `<slug>` is derived from the title.
89
+
90
+ Each citation downloads into a sibling `.partial` folder and is renamed into place only when every file succeeded. Re-running skips citations already archived.
91
+
92
+ ## Library usage
93
+
94
+ ```ruby
95
+ require 'ntrs/downloader'
96
+
97
+ identifier = NTRS::Downloader::Identifier.new 'https://ntrs.nasa.gov/citations/19700020471'
98
+ client = NTRS::Downloader::Client.new # 3-second rate limit by default
99
+ path = NTRS::Downloader::Archive.new(identifier, root: '/tmp/papers', client: client).run
100
+ ```
101
+
102
+ ## Development
103
+
104
+ ```sh
105
+ script/setup # install dependencies
106
+ script/test # run specs and rubocop
107
+ script/console # interactive prompt
108
+ ```
109
+
110
+ Specs run offline against recorded fixtures in `spec/fixtures/http/`. The PDF fixture is the first 4 KB of the real file. To check the citation fixtures against the live NTRS API, run:
111
+
112
+ ```sh
113
+ NTRS_LIVE=1 script/test
114
+ ```
115
+
116
+ ## License
117
+
118
+ MIT. See [LICENSE.md](LICENSE.md).
119
+
120
+ ## Code of Conduct
121
+
122
+ 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/exe/ntrs-dl ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'ntrs/downloader'
4
+
5
+ exit NTRS::Downloader::CLI.new(ARGV).run
@@ -0,0 +1,95 @@
1
+ require 'fileutils'
2
+
3
+ module NTRS
4
+ module Downloader
5
+ class Archive
6
+ def initialize identifier, root:, client: Client.new
7
+ @identifier = identifier
8
+ @root = root
9
+ @client = client
10
+ end
11
+
12
+ # Downloads into <paper>.partial/ and
13
+ # renames it into place only once everything succeeded.
14
+ # An existing paper folder is always complete and is skipped.
15
+ def run
16
+ raise NotAvailable unless parser.available?
17
+ return paper_dir if Dir.exist? paper_dir
18
+
19
+ FileUtils.rm_rf staging_dir
20
+ FileUtils.mkdir_p staging_dir
21
+
22
+ download_files
23
+ write_citation
24
+ write_sidecars
25
+
26
+ File.rename staging_dir, paper_dir
27
+ paper_dir
28
+ end
29
+
30
+ private
31
+
32
+ def citation
33
+ @citation ||= fetch_citation
34
+ end
35
+
36
+ def fetch_citation
37
+ @client.get("https://ntrs.nasa.gov/api/citations/#{@identifier}").to_s
38
+ rescue DL::Core::HTTPError => e
39
+ raise PaperNotFound if e.status == 404
40
+
41
+ raise
42
+ end
43
+
44
+ def parser
45
+ @parser ||= CitationParser.new citation
46
+ end
47
+
48
+ def metadata
49
+ @metadata ||= parser.metadata
50
+ end
51
+
52
+ def paper_dir
53
+ @paper_dir ||= File.join @root, Path.new(metadata).to_s
54
+ end
55
+
56
+ # a sibling of paper_dir, so the rename is a single atomic step
57
+ def staging_dir
58
+ "#{paper_dir}.partial"
59
+ end
60
+
61
+ # each file under its NTRS name, plus its plaintext when NTRS has it.
62
+ # File.basename keeps a name from the JSON from pointing outside the folder.
63
+ def download_files
64
+ parser.downloads.each do |download|
65
+ File.binwrite File.join(staging_dir, File.basename(download.name)), @client.get(download.original).to_s
66
+ download_fulltext download
67
+ end
68
+ end
69
+
70
+ def download_fulltext download
71
+ return if download.fulltext.nil?
72
+
73
+ text_name = "#{File.basename(download.name, '.*')}.txt"
74
+ File.write File.join(staging_dir, text_name), @client.get(download.fulltext).to_s
75
+ rescue DL::Core::HTTPError => e
76
+ raise unless e.status == 404
77
+ end
78
+
79
+ # NTRS's own citation record, verbatim
80
+ def write_citation
81
+ File.write File.join(staging_dir, 'ntrs.json'), citation
82
+ end
83
+
84
+ def write_sidecars
85
+ body = MarkdownBody.new(metadata).to_s
86
+ key = Bibtex.new(metadata).key
87
+
88
+ DL::Core::Sidecar::Markdown.new(metadata, body:, extras: { bibtex_key: key }).write to: staging_dir
89
+ DL::Core::Sidecar::YAML.new(metadata).write to: staging_dir
90
+ DL::Core::Sidecar::JSON.new(metadata).write to: staging_dir
91
+ File.write File.join(staging_dir, 'metadata.bib'), Bibtex.new(metadata).to_s
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,54 @@
1
+ module NTRS
2
+ module Downloader
3
+ # Synthesized from the citation metadata. NTRS has no BibTeX export.
4
+ class Bibtex
5
+ def initialize metadata
6
+ @metadata = metadata
7
+ end
8
+
9
+ def to_s
10
+ <<~BIBTEX
11
+ @techreport{#{key},
12
+ title={#{@metadata.title}},
13
+ author={#{authors}},
14
+ institution={#{institution}},
15
+ year={#{year}},
16
+ number={#{@metadata.report_numbers.first}},
17
+ type={#{@metadata.report_type}},
18
+ url={#{@metadata.ntrs_url}},
19
+ }
20
+ BIBTEX
21
+ end
22
+
23
+ def key
24
+ title_word = DL::Core::Slug.new(@metadata.title).to_s.split('-').first
25
+
26
+ "#{surname}#{year}#{title_word}"
27
+ end
28
+
29
+ private
30
+
31
+ def authors
32
+ @metadata.authors.map(&:name).join ' and '
33
+ end
34
+
35
+ def institution
36
+ @metadata.authors.flat_map(&:affiliations).uniq.join ' and '
37
+ end
38
+
39
+ # "Schneider, L. E." and "Lynn E. Schneider" both give a lowercase surname
40
+ def surname
41
+ first_author = @metadata.authors.first
42
+ return 'anonymous' if first_author.nil?
43
+
44
+ name = first_author.name
45
+ last = name.include?(',') ? name.split(',').first : name.split.last
46
+ last.downcase.gsub(/[^a-z]/, '')
47
+ end
48
+
49
+ def year
50
+ (@metadata.published || @metadata.submitted).to_s[0, 4]
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,75 @@
1
+ require 'json'
2
+
3
+ module NTRS
4
+ module Downloader
5
+ # Parses an NTRS citation (https://ntrs.nasa.gov/api/citations/<id>)
6
+ class CitationParser
7
+ BASE_URL = 'https://ntrs.nasa.gov'.freeze
8
+
9
+ Download = Data.define :name, :original, :fulltext
10
+
11
+ def initialize json
12
+ @citation = JSON.parse json
13
+ end
14
+
15
+ def metadata
16
+ Metadata.new(
17
+ ntrs_id: @citation.fetch('id').to_s,
18
+ ntrs_url: "#{BASE_URL}/citations/#{@citation.fetch('id')}",
19
+ title: @citation['title'],
20
+ authors: authors,
21
+ abstract: @citation['abstract'],
22
+ published: date_of(Array(@citation['publications']).first&.fetch('publicationDate', nil)),
23
+ submitted: date_of(@citation['submittedDate']),
24
+ subjects: Array(@citation['subjectCategories']),
25
+ report_type: @citation['stiTypeDetails'],
26
+ center: @citation.dig('center', 'name'),
27
+ report_numbers: report_numbers,
28
+ distribution: @citation['distribution'],
29
+ copyright: @citation.dig('copyright', 'determinationType')
30
+ )
31
+ end
32
+
33
+ def downloads
34
+ Array(@citation['downloads']).map do |download|
35
+ links = download.fetch 'links', {}
36
+
37
+ Download.new(
38
+ name: download.fetch('name'),
39
+ original: absolute(links['original'] || links['pdf']),
40
+ fulltext: absolute(links['fulltext'])
41
+ )
42
+ end
43
+ end
44
+
45
+ # NASA only allows public citations to be harvested, and some list no files
46
+ def available?
47
+ @citation['distribution'] == 'PUBLIC' && @citation['downloadsAvailable'] == true && downloads.any?
48
+ end
49
+
50
+ private
51
+
52
+ def authors
53
+ Array(@citation['authorAffiliations']).sort_by { it['sequence'].to_i }.map do |affiliation|
54
+ meta = affiliation.fetch 'meta', {}
55
+ organization = meta.dig 'organization', 'name'
56
+
57
+ DL::Core::Author.new name: meta.dig('author', 'name'), affiliations: Array(organization)
58
+ end
59
+ end
60
+
61
+ # otherReportNumbers repeats each number with a "Report Number: " label
62
+ def report_numbers
63
+ Array(@citation['otherReportNumbers']).map { it.delete_prefix 'Report Number: ' }.uniq
64
+ end
65
+
66
+ def date_of timestamp
67
+ timestamp&.slice 0, 10
68
+ end
69
+
70
+ def absolute path
71
+ path && "#{BASE_URL}#{path}"
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,24 @@
1
+ module NTRS
2
+ module Downloader
3
+ class CLI < DL::Core::CLI
4
+ def program = 'ntrs-dl'
5
+
6
+ def target_name = 'NTRS_ID_OR_URL'
7
+
8
+ # NTRS_DOWNLOAD_PATH, NTRS_RATE_LIMIT
9
+ def env_prefix = 'NTRS'
10
+
11
+ def default_path = File.join(Dir.home, 'Downloads', 'NTRS_Papers')
12
+
13
+ def version = VERSION
14
+
15
+ def user_agent = Client::USER_AGENT
16
+
17
+ def client_for(rate_limit:, log:) = Client.new(rate_limit:, log:)
18
+
19
+ def identifier_for(target) = Identifier.new(target)
20
+
21
+ def archive_for(identifier, root:, client:) = Archive.new(identifier, root:, client:)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,14 @@
1
+ module NTRS
2
+ module Downloader
3
+ # DL::Core::Client with ntrs-dl's User-Agent filled in. NTRS allows 500 requests
4
+ # per 15 minutes. The 3-second default stays under that.
5
+ class Client < DL::Core::Client
6
+ SOURCE_URL = 'https://github.com/xoengineering/ntrs-dl'.freeze
7
+ USER_AGENT = "ntrs-dl/#{VERSION} (+#{SOURCE_URL})".freeze
8
+
9
+ def initialize log: nil, rate_limit: DEFAULT_RATE_LIMIT
10
+ super(user_agent: USER_AGENT, log:, rate_limit:)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ module NTRS
2
+ module Downloader
3
+ class Error < DL::Core::Error; end
4
+ end
5
+ end
@@ -0,0 +1,36 @@
1
+ require 'uri'
2
+
3
+ module NTRS
4
+ module Downloader
5
+ # An NTRS citation ID: eleven digits, starting with a year (19700020471).
6
+ class Identifier
7
+ class Invalid < Error; end
8
+
9
+ ID = /\A\d{11}\z/
10
+ PATH = %r{\A(?:/api)?/citations/(\d{11})(?:/|\z)}
11
+
12
+ attr_reader :id, :input
13
+
14
+ def initialize input
15
+ @input = input
16
+ @id = parse input.to_s.strip
17
+ raise Invalid, "not an NTRS identifier: #{input}" if @id.nil?
18
+ end
19
+
20
+ def to_s = id
21
+
22
+ private
23
+
24
+ def parse text
25
+ return text if ID.match? text
26
+
27
+ uri = URI.parse(text.include?('://') ? text : "https://#{text}")
28
+ return unless uri.host == 'ntrs.nasa.gov'
29
+
30
+ PATH.match(uri.path.to_s)&.captures&.first
31
+ rescue URI::InvalidURIError
32
+ nil
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,43 @@
1
+ module NTRS
2
+ module Downloader
3
+ # The body of metadata.md. NASA asks harvesters to cite NASA STI Compliance and
4
+ # Distribution Services as the source, and to share the citation URL.
5
+ class MarkdownBody
6
+ def initialize metadata
7
+ @metadata = metadata
8
+ end
9
+
10
+ def to_s
11
+ <<~MARKDOWN
12
+
13
+ # #{@metadata.title}
14
+
15
+ #{authors_list}
16
+
17
+ - Published: #{@metadata.published || @metadata.submitted}
18
+ - Report type: #{@metadata.report_type}
19
+ - Subjects: #{@metadata.subjects.join ', '}
20
+ - NTRS: [#{@metadata.ntrs_id}](#{@metadata.ntrs_url})
21
+
22
+ ## Abstract
23
+
24
+ #{@metadata.abstract}
25
+
26
+ Source: NASA STI Compliance and Distribution Services, via the NASA Technical Reports Server.
27
+ MARKDOWN
28
+ end
29
+
30
+ private
31
+
32
+ def authors_list
33
+ @metadata.authors.map { |author| "- #{author_line author}" }.join "\n"
34
+ end
35
+
36
+ def author_line author
37
+ return author.name if author.affiliations.empty?
38
+
39
+ "#{author.name} (#{author.affiliations.join '; '})"
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,20 @@
1
+ module NTRS
2
+ module Downloader
3
+ # Field order is the reading order of the metadata sidecars.
4
+ Metadata = Data.define(
5
+ :ntrs_id,
6
+ :ntrs_url,
7
+ :title,
8
+ :authors,
9
+ :abstract,
10
+ :published,
11
+ :submitted,
12
+ :subjects,
13
+ :report_type,
14
+ :center,
15
+ :report_numbers,
16
+ :distribution,
17
+ :copyright
18
+ )
19
+ end
20
+ end
@@ -0,0 +1,9 @@
1
+ module NTRS
2
+ module Downloader
3
+ class NotAvailable < Error
4
+ def initialize message = 'NTRS has no public files for this citation'
5
+ super
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module NTRS
2
+ module Downloader
3
+ class PaperNotFound < Error
4
+ def initialize message = 'no such citation on NTRS'
5
+ super
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,33 @@
1
+ module NTRS
2
+ module Downloader
3
+ class Path
4
+ def initialize metadata
5
+ @metadata = metadata
6
+ end
7
+
8
+ def to_s
9
+ [date_dir, subject, "#{@metadata.ntrs_id}-#{slug}"].join '/'
10
+ end
11
+
12
+ private
13
+
14
+ # 1970-01-06 becomes 1970/01/06
15
+ def date_dir
16
+ date = @metadata.published || @metadata.submitted
17
+ date.to_s.split('-').join '/'
18
+ end
19
+
20
+ # the first subject category, e.g. "Space Sciences" becomes space-sciences
21
+ def subject
22
+ first = @metadata.subjects.first
23
+ return 'uncategorized' if first.nil?
24
+
25
+ DL::Core::Slug.new(first).to_s
26
+ end
27
+
28
+ def slug
29
+ DL::Core::Slug.new(@metadata.title).to_s
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,5 @@
1
+ module NTRS
2
+ module Downloader
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
@@ -0,0 +1,21 @@
1
+ require 'dl/core'
2
+
3
+ require_relative 'downloader/version' # before client: Client::USER_AGENT uses VERSION
4
+
5
+ require_relative 'downloader/archive'
6
+ require_relative 'downloader/bibtex'
7
+ require_relative 'downloader/citation_parser'
8
+ require_relative 'downloader/cli'
9
+ require_relative 'downloader/client' # after version
10
+ require_relative 'downloader/error' # before errors below that subclass Error
11
+ require_relative 'downloader/identifier' # after error
12
+ require_relative 'downloader/markdown_body'
13
+ require_relative 'downloader/metadata'
14
+ require_relative 'downloader/not_available' # after error
15
+ require_relative 'downloader/paper_not_found' # after error
16
+ require_relative 'downloader/path'
17
+
18
+ module NTRS
19
+ module Downloader
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ module NTRS
2
+ module Downloader
3
+ VERSION: String
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ntrs-dl
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: dl-core
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '0.2'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '0.2'
26
+ description: |
27
+ Command line tool and Ruby library for archiving publicly available NASA Technical Reports Server
28
+ documents as PDFs and plaintext with NTRS's own metadata and sidecar metadata.
29
+ email:
30
+ - veganstraightedge@gmail.com
31
+ executables:
32
+ - ntrs-dl
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - CHANGELOG.md
37
+ - CODE_OF_CONDUCT.md
38
+ - LICENSE.md
39
+ - README.md
40
+ - exe/ntrs-dl
41
+ - lib/ntrs/downloader.rb
42
+ - lib/ntrs/downloader/archive.rb
43
+ - lib/ntrs/downloader/bibtex.rb
44
+ - lib/ntrs/downloader/citation_parser.rb
45
+ - lib/ntrs/downloader/cli.rb
46
+ - lib/ntrs/downloader/client.rb
47
+ - lib/ntrs/downloader/error.rb
48
+ - lib/ntrs/downloader/identifier.rb
49
+ - lib/ntrs/downloader/markdown_body.rb
50
+ - lib/ntrs/downloader/metadata.rb
51
+ - lib/ntrs/downloader/not_available.rb
52
+ - lib/ntrs/downloader/paper_not_found.rb
53
+ - lib/ntrs/downloader/path.rb
54
+ - lib/ntrs/downloader/version.rb
55
+ - sig/ntrs/downloader.rbs
56
+ homepage: https://github.com/xoengineering/ntrs-dl
57
+ licenses:
58
+ - MIT
59
+ metadata:
60
+ allowed_push_host: https://rubygems.org
61
+ homepage_uri: https://github.com/xoengineering/ntrs-dl
62
+ source_code_uri: https://github.com/xoengineering/ntrs-dl
63
+ bug_tracker_uri: https://github.com/xoengineering/ntrs-dl/issues
64
+ changelog_uri: https://github.com/xoengineering/ntrs-dl/blob/main/CHANGELOG.md
65
+ rubygems_mfa_required: 'true'
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: 4.0.10
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubygems_version: 4.0.20
81
+ specification_version: 4
82
+ summary: Download reports and metadata from the NASA Technical Reports Server (NTRS)
83
+ for offline archives.
84
+ test_files: []