osf-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 +7 -0
- data/CHANGELOG.md +9 -0
- data/CODE_OF_CONDUCT.md +83 -0
- data/LICENSE.md +21 -0
- data/README.md +116 -0
- data/exe/osf-dl +5 -0
- data/lib/osf/downloader/archive.rb +110 -0
- data/lib/osf/downloader/bibtex.rb +47 -0
- data/lib/osf/downloader/checksum_mismatch.rb +5 -0
- data/lib/osf/downloader/cli.rb +27 -0
- data/lib/osf/downloader/client.rb +15 -0
- data/lib/osf/downloader/error.rb +5 -0
- data/lib/osf/downloader/file_parser.rb +25 -0
- data/lib/osf/downloader/identifier.rb +45 -0
- data/lib/osf/downloader/markdown_body.rb +35 -0
- data/lib/osf/downloader/metadata.rb +20 -0
- data/lib/osf/downloader/not_available.rb +9 -0
- data/lib/osf/downloader/paper_not_found.rb +9 -0
- data/lib/osf/downloader/path.rb +25 -0
- data/lib/osf/downloader/preprint_parser.rb +63 -0
- data/lib/osf/downloader/version.rb +5 -0
- data/lib/osf/downloader.rb +23 -0
- data/sig/osf/downloader.rbs +5 -0
- metadata +86 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 4dcd95a5d45ece418bf8fa93c6cb0796a93a295ba7273a20a7e98b5a12ace8c0
|
|
4
|
+
data.tar.gz: 619ec83cec7e8323a3ac718e769541f431938a0fc2ca0a66f96aa1a30fbf7665
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 149fa50a859a17d07218103e6f1d47138587aeacb49358764e92e9f8b315e08a83af1d7006517079df03569d13a40f371e99e9c53389a4d0927a4fca3f59d95c
|
|
7
|
+
data.tar.gz: 46a38f06cca4215216eb1d2211879b3404713ed8c0a7d2ea2a2a28849cbcef3ddf48ff76e186a37441ea01da3cfda20f0cc2f418fe5ea6a11359241dc0704b41
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
## [0.1.0]
|
|
2
|
+
|
|
3
|
+
First version. Per-preprint offline archive of OSF Preprints (PsyArXiv, SocArXiv, EarthArXiv, engrXiv, EdArXiv, and more), any version, built on [dl-core](https://github.com/xoengineering/dl-core) 0.3.
|
|
4
|
+
|
|
5
|
+
- Accepts OSF preprint IDs with or without a version, osf.io preprint, short, and API URLs, and osf.io DOIs.
|
|
6
|
+
- Saves the primary file, verified against OSF's SHA-256 checksum, plus OSF's preprint record verbatim and four sidecar metadata files.
|
|
7
|
+
- Layout: `YYYY/MM/DD/<server>/<osf-id>-<slug>/`, flat for a preprint with only v1 archived, `v<N>/` folders otherwise.
|
|
8
|
+
- Withdrawn and unpublished preprints raise `OSF::Downloader::NotAvailable`. A checksum mismatch raises `OSF::Downloader::ChecksumMismatch` and leaves nothing behind.
|
|
9
|
+
- Defaults to one request every 36 seconds, for OSF's anonymous limit of about 100 requests an hour.
|
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,116 @@
|
|
|
1
|
+
# osf-dl
|
|
2
|
+
|
|
3
|
+
Download preprints and metadata from [OSF Preprints](https://osf.io/preprints) for offline archives. OSF hosts many preprint servers, including PsyArXiv, SocArXiv, EarthArXiv, engrXiv, and EdArXiv. `osf-dl` works with all of them.
|
|
4
|
+
|
|
5
|
+
For each preprint version, `osf-dl` saves:
|
|
6
|
+
|
|
7
|
+
- The preprint's primary file, checked against the SHA-256 checksum OSF publishes for it
|
|
8
|
+
- OSF's own preprint record (`osf.json`), verbatim
|
|
9
|
+
- Four sidecar metadata files: `metadata.md`, `metadata.yaml`, `metadata.json`, `metadata.bib`
|
|
10
|
+
|
|
11
|
+
Any version can be archived, not just the latest. Withdrawn and unpublished preprints are not archived.
|
|
12
|
+
|
|
13
|
+
## OSF's terms and rate limit
|
|
14
|
+
|
|
15
|
+
The Center for Open Science's [terms of use](https://github.com/CenterForOpenScience/cos.io/blob/master/TERMS_OF_USE.md) offer a public API and license use of content marked public, "subject to patent, copyright and trademark law". Those are the terms as published on GitHub. The current terms at <https://osf.io/terms-of-use/> may differ. Each preprint's license is recorded as `license`.
|
|
16
|
+
|
|
17
|
+
OSF limits anonymous API use to about 100 requests an hour. Each preprint takes five requests (the preprint, its citation, its license, its file record, and the file). So `osf-dl` defaults to one request every 36 seconds, about three minutes per preprint. `--rate-limit` or `OSF_RATE_LIMIT` changes that, but going faster than OSF allows gets you HTTP 429 responses, which `osf-dl` retries with backoff.
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
gem install osf-dl
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## CLI usage
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
osf-dl <OSF_ID_OR_URL> [<OSF_ID_OR_URL>...]
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Accepted input forms:
|
|
32
|
+
|
|
33
|
+
| Form | Example |
|
|
34
|
+
| ------------------- | ------------------------------------------------ |
|
|
35
|
+
| Preprint ID | `cjy8e`, `cjy8e_v1` |
|
|
36
|
+
| Preprint URL | `https://osf.io/preprints/psyarxiv/cjy8e_v1/` |
|
|
37
|
+
| Short URL | `https://osf.io/cjy8e` |
|
|
38
|
+
| API URL | `https://api.osf.io/v2/preprints/cjy8e_v1/` |
|
|
39
|
+
| DOI | `10.31234/osf.io/cjy8e_v1` |
|
|
40
|
+
| DOI URL | `https://doi.org/10.31234/osf.io/cjy8e` |
|
|
41
|
+
|
|
42
|
+
An unversioned ID archives the latest version. A versioned ID archives that version.
|
|
43
|
+
|
|
44
|
+
### Flags
|
|
45
|
+
|
|
46
|
+
| Flag | Description |
|
|
47
|
+
| ------------------------- | ----------------------------------------------------------------------------- |
|
|
48
|
+
| `-i FILE`, `--input FILE` | Read IDs/URLs from FILE, one per line (`-` for stdin, blanks and `#` skipped) |
|
|
49
|
+
| `-p PATH`, `--path PATH` | Root download directory |
|
|
50
|
+
| `--rate-limit SECONDS` | Seconds between HTTP requests (default `36`, `0` disables throttling) |
|
|
51
|
+
| `-v`, `--verbose` | Print step lines and per-request URL/byte logs to stdout |
|
|
52
|
+
| `-q`, `--quiet` | Print nothing to stdout. Errors still go to stderr. |
|
|
53
|
+
| `--version` | Print the gem version and exit |
|
|
54
|
+
| `-h`, `--help` | Print help and exit |
|
|
55
|
+
|
|
56
|
+
`-v` and `-q` are mutually exclusive.
|
|
57
|
+
|
|
58
|
+
### Environment variables
|
|
59
|
+
|
|
60
|
+
| Variable | Effect |
|
|
61
|
+
| ------------------- | --------------------------------------------------------------- |
|
|
62
|
+
| `OSF_DOWNLOAD_PATH` | Root download directory (default: `$HOME/Downloads/OSF_Papers`) |
|
|
63
|
+
| `OSF_RATE_LIMIT` | Seconds between HTTP requests (default: `36`, `0` disables) |
|
|
64
|
+
|
|
65
|
+
Precedence: CLI flag, then ENV var, then default.
|
|
66
|
+
|
|
67
|
+
### Errors and exit status
|
|
68
|
+
|
|
69
|
+
A target that fails (unrecognized ID, no such preprint, withdrawn or unpublished, checksum mismatch, 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.
|
|
70
|
+
|
|
71
|
+
## Output layout
|
|
72
|
+
|
|
73
|
+
```txt
|
|
74
|
+
$OSF_DOWNLOAD_PATH/ # default: $HOME/Downloads/OSF_Papers
|
|
75
|
+
YYYY/MM/DD/<server>/<osf-id>-<slug>/
|
|
76
|
+
<file> # the preprint's primary file
|
|
77
|
+
osf.json # OSF's preprint record, verbatim
|
|
78
|
+
metadata.md # YAML frontmatter + Markdown body
|
|
79
|
+
metadata.yaml
|
|
80
|
+
metadata.json
|
|
81
|
+
metadata.bib # synthesized @misc with the preprint DOI
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
`YYYY/MM/DD` is the publication date. `<server>` is the OSF preprint server (`psyarxiv`, `socarxiv`, and so on). `<slug>` is derived from the title.
|
|
85
|
+
|
|
86
|
+
A preprint with only v1 archived is kept flat, as above. When it has more than one version, each version gets its own `v<N>/` folder with the same contents. Archiving a second version of a flat preprint first moves the existing files into `v<N>/`. A preprint whose latest version is v2 or later starts out in `v<N>/` folders.
|
|
87
|
+
|
|
88
|
+
Each version downloads into a sibling `.partial` folder and is renamed into place only when the file matched its checksum. Re-running skips versions already archived.
|
|
89
|
+
|
|
90
|
+
## Library usage
|
|
91
|
+
|
|
92
|
+
```ruby
|
|
93
|
+
require 'osf/downloader'
|
|
94
|
+
|
|
95
|
+
identifier = OSF::Downloader::Identifier.new 'https://osf.io/preprints/psyarxiv/cjy8e_v1/'
|
|
96
|
+
client = OSF::Downloader::Client.new # 36-second rate limit by default
|
|
97
|
+
path = OSF::Downloader::Archive.new(identifier, root: '/tmp/papers', client: client).run
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Development
|
|
101
|
+
|
|
102
|
+
```sh
|
|
103
|
+
script/setup # install dependencies
|
|
104
|
+
script/test # run specs and rubocop
|
|
105
|
+
script/console # interactive prompt
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Specs run offline against recorded fixtures in `spec/fixtures/http/`. The PDF fixture is the first 4 KB of the real file, and the archive spec sets the expected SHA-256 to match it.
|
|
109
|
+
|
|
110
|
+
## License
|
|
111
|
+
|
|
112
|
+
MIT. See [LICENSE.md](LICENSE.md).
|
|
113
|
+
|
|
114
|
+
## Code of Conduct
|
|
115
|
+
|
|
116
|
+
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/osf-dl
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
require 'digest'
|
|
2
|
+
require 'fileutils'
|
|
3
|
+
|
|
4
|
+
module OSF
|
|
5
|
+
module Downloader
|
|
6
|
+
class Archive
|
|
7
|
+
def initialize identifier, root:, client: Client.new
|
|
8
|
+
@identifier = identifier
|
|
9
|
+
@root = root
|
|
10
|
+
@client = client
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Downloads into <destination>.partial/ and
|
|
14
|
+
# renames it into place only once the file matched its checksum.
|
|
15
|
+
# An archived version is always complete and is skipped.
|
|
16
|
+
# PaperFolder decides flat (single version) vs v<N>/ (several versions).
|
|
17
|
+
def run
|
|
18
|
+
raise NotAvailable unless parser.available?
|
|
19
|
+
return paper_folder.location_of(metadata.version) if paper_folder.archived? metadata.version
|
|
20
|
+
|
|
21
|
+
paper_folder.unflatten!
|
|
22
|
+
FileUtils.rm_rf staging_dir
|
|
23
|
+
FileUtils.mkdir_p staging_dir
|
|
24
|
+
|
|
25
|
+
download_primary_file
|
|
26
|
+
write_preprint
|
|
27
|
+
write_sidecars
|
|
28
|
+
|
|
29
|
+
File.rename staging_dir, destination
|
|
30
|
+
destination
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
# an unversioned ID resolves to the latest version
|
|
36
|
+
def preprint
|
|
37
|
+
@preprint ||= get "https://api.osf.io/v2/preprints/#{@identifier}/"
|
|
38
|
+
rescue DL::Core::HTTPError => e
|
|
39
|
+
raise PaperNotFound if e.status == 404
|
|
40
|
+
|
|
41
|
+
raise
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def related
|
|
45
|
+
@related ||= PreprintParser.related_urls preprint
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def parser
|
|
49
|
+
@parser ||= PreprintParser.new preprint:, citation: get(related[:citation]), license: license
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def license
|
|
53
|
+
related[:license] && get(related[:license])
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def metadata
|
|
57
|
+
@metadata ||= parser.metadata
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def get url
|
|
61
|
+
@client.get(url).to_s
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def paper_folder
|
|
65
|
+
@paper_folder ||= DL::Core::PaperFolder.new File.join(@root, Path.new(metadata).to_s)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# evaluated after unflatten!, which can turn a flat folder into v<N>/ folders
|
|
69
|
+
def destination
|
|
70
|
+
@destination ||= paper_folder.destination_for metadata.version
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# a sibling of destination, so the rename is a single atomic step
|
|
74
|
+
def staging_dir
|
|
75
|
+
"#{destination}.partial"
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# File.basename keeps a name from the record from pointing outside the folder
|
|
79
|
+
def download_primary_file
|
|
80
|
+
file = FileParser.new(get(related[:primary_file])).file
|
|
81
|
+
body = get file.url
|
|
82
|
+
verify file, body
|
|
83
|
+
|
|
84
|
+
File.binwrite File.join(staging_dir, File.basename(file.name)), body
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def verify file, body
|
|
88
|
+
actual = Digest::SHA256.hexdigest body
|
|
89
|
+
return if actual == file.sha256
|
|
90
|
+
|
|
91
|
+
raise ChecksumMismatch, "#{file.name}: expected SHA-256 #{file.sha256}, got #{actual}"
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# OSF's own preprint record, verbatim
|
|
95
|
+
def write_preprint
|
|
96
|
+
File.write File.join(staging_dir, 'osf.json'), preprint
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def write_sidecars
|
|
100
|
+
body = MarkdownBody.new(metadata).to_s
|
|
101
|
+
key = Bibtex.new(metadata).key
|
|
102
|
+
|
|
103
|
+
DL::Core::Sidecar::Markdown.new(metadata, body:, extras: { bibtex_key: key }).write to: staging_dir
|
|
104
|
+
DL::Core::Sidecar::YAML.new(metadata).write to: staging_dir
|
|
105
|
+
DL::Core::Sidecar::JSON.new(metadata).write to: staging_dir
|
|
106
|
+
File.write File.join(staging_dir, 'metadata.bib'), Bibtex.new(metadata).to_s
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
module OSF
|
|
2
|
+
module Downloader
|
|
3
|
+
# Synthesized from the preprint metadata
|
|
4
|
+
class Bibtex
|
|
5
|
+
def initialize metadata
|
|
6
|
+
@metadata = metadata
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def to_s
|
|
10
|
+
<<~BIBTEX
|
|
11
|
+
@misc{#{key},
|
|
12
|
+
title={#{@metadata.title}},
|
|
13
|
+
author={#{authors}},
|
|
14
|
+
year={#{year}},
|
|
15
|
+
publisher={#{@metadata.provider}},
|
|
16
|
+
doi={#{@metadata.preprint_doi}},
|
|
17
|
+
url={#{@metadata.osf_url}},
|
|
18
|
+
}
|
|
19
|
+
BIBTEX
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def key
|
|
23
|
+
title_word = DL::Core::Slug.new(@metadata.title).to_s.split('-').first
|
|
24
|
+
|
|
25
|
+
"#{surname}#{year}#{title_word}"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def authors
|
|
31
|
+
@metadata.authors.map(&:name).join ' and '
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# authors are "Family, Given"
|
|
35
|
+
def surname
|
|
36
|
+
first_author = @metadata.authors.first
|
|
37
|
+
return 'anonymous' if first_author.nil?
|
|
38
|
+
|
|
39
|
+
first_author.name.split(',').first.downcase.gsub(/[^a-z]/, '')
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def year
|
|
43
|
+
@metadata.published.to_s[0, 4]
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
module OSF
|
|
2
|
+
module Downloader
|
|
3
|
+
class CLI < DL::Core::CLI
|
|
4
|
+
def program = 'osf-dl'
|
|
5
|
+
|
|
6
|
+
def target_name = 'OSF_ID_OR_URL'
|
|
7
|
+
|
|
8
|
+
# OSF_DOWNLOAD_PATH, OSF_RATE_LIMIT
|
|
9
|
+
def env_prefix = 'OSF'
|
|
10
|
+
|
|
11
|
+
def default_path = File.join(Dir.home, 'Downloads', 'OSF_Papers')
|
|
12
|
+
|
|
13
|
+
# OSF allows about 100 anonymous API requests an hour
|
|
14
|
+
def default_rate_limit = Client::DEFAULT_RATE_LIMIT
|
|
15
|
+
|
|
16
|
+
def version = VERSION
|
|
17
|
+
|
|
18
|
+
def user_agent = Client::USER_AGENT
|
|
19
|
+
|
|
20
|
+
def client_for(rate_limit:, log:) = Client.new(rate_limit:, log:)
|
|
21
|
+
|
|
22
|
+
def identifier_for(target) = Identifier.new(target)
|
|
23
|
+
|
|
24
|
+
def archive_for(identifier, root:, client:) = Archive.new(identifier, root:, client:)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module OSF
|
|
2
|
+
module Downloader
|
|
3
|
+
# DL::Core::Client with osf-dl's User-Agent filled in. OSF limits anonymous API use
|
|
4
|
+
# to about 100 requests an hour, so the default is one request every 36 seconds.
|
|
5
|
+
class Client < DL::Core::Client
|
|
6
|
+
SOURCE_URL = 'https://github.com/xoengineering/osf-dl'.freeze
|
|
7
|
+
USER_AGENT = "osf-dl/#{VERSION} (+#{SOURCE_URL})".freeze
|
|
8
|
+
DEFAULT_RATE_LIMIT = 36
|
|
9
|
+
|
|
10
|
+
def initialize log: nil, rate_limit: DEFAULT_RATE_LIMIT
|
|
11
|
+
super(user_agent: USER_AGENT, log:, rate_limit:)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
module OSF
|
|
4
|
+
module Downloader
|
|
5
|
+
# Parses an OSF file record (https://api.osf.io/v2/files/<id>)
|
|
6
|
+
class FileParser
|
|
7
|
+
PreprintFile = Data.define :name, :size, :sha256, :url
|
|
8
|
+
|
|
9
|
+
def initialize json
|
|
10
|
+
@file = JSON.parse(json).fetch 'data'
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def file
|
|
14
|
+
attributes = @file.fetch 'attributes', {}
|
|
15
|
+
|
|
16
|
+
PreprintFile.new(
|
|
17
|
+
name: attributes.fetch('name'),
|
|
18
|
+
size: attributes['size'],
|
|
19
|
+
sha256: attributes.dig('extra', 'hashes', 'sha256'),
|
|
20
|
+
url: @file.dig('links', 'download')
|
|
21
|
+
)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
require 'uri'
|
|
2
|
+
|
|
3
|
+
module OSF
|
|
4
|
+
module Downloader
|
|
5
|
+
# An OSF preprint ID (a five-character GUID like cjy8e), with its version
|
|
6
|
+
# when one was given (cjy8e_v1).
|
|
7
|
+
class Identifier
|
|
8
|
+
class Invalid < Error; end
|
|
9
|
+
|
|
10
|
+
ID = /\A(?<id>[a-z0-9]{5})(?:_v(?<version>\d+))?\z/
|
|
11
|
+
DOI = %r{\A(?:doi:)?10\.\d+/osf\.io/(?<id>[a-z0-9]{5})(?:_v(?<version>\d+))?\z}
|
|
12
|
+
|
|
13
|
+
attr_reader :id, :version, :input
|
|
14
|
+
|
|
15
|
+
def initialize input
|
|
16
|
+
@input = input
|
|
17
|
+
match = parse input.to_s.strip
|
|
18
|
+
raise Invalid, "not an OSF preprint identifier: #{input}" if match.nil?
|
|
19
|
+
|
|
20
|
+
@id = match[:id]
|
|
21
|
+
@version = match[:version] && Integer(match[:version])
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def to_s = version.nil? ? id : "#{id}_v#{version}"
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
def parse text
|
|
29
|
+
ID.match(text) || DOI.match(text) || parse_url(text)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# osf.io/<id>, osf.io/preprints/<provider>/<id>, api.osf.io/v2/preprints/<id>, and doi.org
|
|
33
|
+
def parse_url text
|
|
34
|
+
uri = URI.parse(text.include?('://') ? text : "https://#{text}")
|
|
35
|
+
|
|
36
|
+
case uri.host
|
|
37
|
+
when 'osf.io', 'www.osf.io', 'api.osf.io' then ID.match uri.path.to_s.split('/').reject(&:empty?).last.to_s
|
|
38
|
+
when 'doi.org', 'dx.doi.org' then DOI.match uri.path.to_s.delete_prefix('/')
|
|
39
|
+
end
|
|
40
|
+
rescue URI::InvalidURIError
|
|
41
|
+
nil
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module OSF
|
|
2
|
+
module Downloader
|
|
3
|
+
# The body of metadata.md
|
|
4
|
+
class MarkdownBody
|
|
5
|
+
def initialize metadata
|
|
6
|
+
@metadata = metadata
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def to_s
|
|
10
|
+
<<~MARKDOWN
|
|
11
|
+
|
|
12
|
+
# #{@metadata.title}
|
|
13
|
+
|
|
14
|
+
#{authors_list}
|
|
15
|
+
|
|
16
|
+
- Published: #{@metadata.published}
|
|
17
|
+
- Server: #{@metadata.provider}
|
|
18
|
+
- Version: #{@metadata.version}
|
|
19
|
+
- OSF: [#{@metadata.preprint_doi}](#{@metadata.osf_url})
|
|
20
|
+
- License: #{@metadata.license}
|
|
21
|
+
|
|
22
|
+
## Abstract
|
|
23
|
+
|
|
24
|
+
#{@metadata.abstract}
|
|
25
|
+
MARKDOWN
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def authors_list
|
|
31
|
+
@metadata.authors.map { |author| "- #{author.name}" }.join "\n"
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module OSF
|
|
2
|
+
module Downloader
|
|
3
|
+
# Field order is the reading order of the metadata sidecars.
|
|
4
|
+
Metadata = Data.define(
|
|
5
|
+
:osf_id,
|
|
6
|
+
:version,
|
|
7
|
+
:provider,
|
|
8
|
+
:osf_url,
|
|
9
|
+
:preprint_doi,
|
|
10
|
+
:doi,
|
|
11
|
+
:title,
|
|
12
|
+
:authors,
|
|
13
|
+
:abstract,
|
|
14
|
+
:published,
|
|
15
|
+
:subjects,
|
|
16
|
+
:tags,
|
|
17
|
+
:license
|
|
18
|
+
)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module OSF
|
|
2
|
+
module Downloader
|
|
3
|
+
# The preprint's folder, shared by all its versions
|
|
4
|
+
class Path
|
|
5
|
+
def initialize metadata
|
|
6
|
+
@metadata = metadata
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def to_s
|
|
10
|
+
[date_dir, @metadata.provider, "#{@metadata.osf_id}-#{slug}"].join '/'
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
private
|
|
14
|
+
|
|
15
|
+
# 2026-09-29 becomes 2026/09/29
|
|
16
|
+
def date_dir
|
|
17
|
+
@metadata.published.to_s.split('-').join '/'
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def slug
|
|
21
|
+
DL::Core::Slug.new(@metadata.title).to_s
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
module OSF
|
|
4
|
+
module Downloader
|
|
5
|
+
# Parses an OSF preprint (https://api.osf.io/v2/preprints/<id>), with its
|
|
6
|
+
# citation (for author names) and its license (for the license name).
|
|
7
|
+
class PreprintParser
|
|
8
|
+
DOI_URL = 'https://doi.org/'.freeze
|
|
9
|
+
|
|
10
|
+
# the URLs of the records a preprint links to, fetched before parsing
|
|
11
|
+
def self.related_urls preprint_json
|
|
12
|
+
relationships = JSON.parse(preprint_json).dig 'data', 'relationships'
|
|
13
|
+
|
|
14
|
+
{
|
|
15
|
+
primary_file: relationships.dig('primary_file', 'links', 'related', 'href'),
|
|
16
|
+
citation: relationships.dig('citation', 'links', 'related', 'href'),
|
|
17
|
+
license: relationships.dig('license', 'links', 'related', 'href')
|
|
18
|
+
}
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def initialize preprint:, citation:, license:
|
|
22
|
+
@preprint = JSON.parse(preprint).fetch 'data'
|
|
23
|
+
@citation = JSON.parse(citation).dig 'data', 'attributes'
|
|
24
|
+
@license = license && JSON.parse(license).dig('data', 'attributes', 'name')
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def metadata
|
|
28
|
+
Metadata.new(
|
|
29
|
+
osf_id: Identifier.new(@preprint.fetch('id')).id,
|
|
30
|
+
version: attributes['version'],
|
|
31
|
+
provider: @preprint.dig('relationships', 'provider', 'data', 'id'),
|
|
32
|
+
osf_url: @preprint.dig('links', 'html'),
|
|
33
|
+
preprint_doi: @preprint.dig('links', 'preprint_doi').to_s.delete_prefix(DOI_URL),
|
|
34
|
+
doi: attributes['doi'],
|
|
35
|
+
title: attributes['title'],
|
|
36
|
+
authors: authors,
|
|
37
|
+
abstract: attributes['description'],
|
|
38
|
+
published: attributes['date_published'].to_s[0, 10],
|
|
39
|
+
subjects: Array(attributes['subjects']).flatten.map { it['text'] }.uniq,
|
|
40
|
+
tags: Array(attributes['tags']),
|
|
41
|
+
license: @license
|
|
42
|
+
)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# withdrawn and unpublished preprints have nothing to archive
|
|
46
|
+
def available?
|
|
47
|
+
attributes['is_published'] == true && attributes['date_withdrawn'].nil?
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def attributes
|
|
53
|
+
@preprint.fetch 'attributes', {}
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def authors
|
|
57
|
+
Array(@citation&.fetch('author', nil)).map do |author|
|
|
58
|
+
DL::Core::Author.new name: [author['family'], author['given']].compact.join(', ')
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
require 'dl/core'
|
|
2
|
+
|
|
3
|
+
require_relative 'downloader/error' # before the errors below that subclass Error
|
|
4
|
+
require_relative 'downloader/version' # before client: Client::USER_AGENT uses VERSION
|
|
5
|
+
|
|
6
|
+
require_relative 'downloader/archive'
|
|
7
|
+
require_relative 'downloader/bibtex'
|
|
8
|
+
require_relative 'downloader/checksum_mismatch' # after error
|
|
9
|
+
require_relative 'downloader/cli'
|
|
10
|
+
require_relative 'downloader/client' # after version
|
|
11
|
+
require_relative 'downloader/file_parser'
|
|
12
|
+
require_relative 'downloader/identifier' # after error
|
|
13
|
+
require_relative 'downloader/markdown_body'
|
|
14
|
+
require_relative 'downloader/metadata'
|
|
15
|
+
require_relative 'downloader/not_available' # after error
|
|
16
|
+
require_relative 'downloader/paper_not_found' # after error
|
|
17
|
+
require_relative 'downloader/path'
|
|
18
|
+
require_relative 'downloader/preprint_parser'
|
|
19
|
+
|
|
20
|
+
module OSF
|
|
21
|
+
module Downloader
|
|
22
|
+
end
|
|
23
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: osf-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.3'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0.3'
|
|
26
|
+
description: |
|
|
27
|
+
Command line tool and Ruby library for archiving preprints from OSF Preprints and the servers it hosts
|
|
28
|
+
(PsyArXiv, SocArXiv, EarthArXiv, engrXiv, EdArXiv, and more), any version, with sidecar metadata.
|
|
29
|
+
email:
|
|
30
|
+
- veganstraightedge@gmail.com
|
|
31
|
+
executables:
|
|
32
|
+
- osf-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/osf-dl
|
|
41
|
+
- lib/osf/downloader.rb
|
|
42
|
+
- lib/osf/downloader/archive.rb
|
|
43
|
+
- lib/osf/downloader/bibtex.rb
|
|
44
|
+
- lib/osf/downloader/checksum_mismatch.rb
|
|
45
|
+
- lib/osf/downloader/cli.rb
|
|
46
|
+
- lib/osf/downloader/client.rb
|
|
47
|
+
- lib/osf/downloader/error.rb
|
|
48
|
+
- lib/osf/downloader/file_parser.rb
|
|
49
|
+
- lib/osf/downloader/identifier.rb
|
|
50
|
+
- lib/osf/downloader/markdown_body.rb
|
|
51
|
+
- lib/osf/downloader/metadata.rb
|
|
52
|
+
- lib/osf/downloader/not_available.rb
|
|
53
|
+
- lib/osf/downloader/paper_not_found.rb
|
|
54
|
+
- lib/osf/downloader/path.rb
|
|
55
|
+
- lib/osf/downloader/preprint_parser.rb
|
|
56
|
+
- lib/osf/downloader/version.rb
|
|
57
|
+
- sig/osf/downloader.rbs
|
|
58
|
+
homepage: https://github.com/xoengineering/osf-dl
|
|
59
|
+
licenses:
|
|
60
|
+
- MIT
|
|
61
|
+
metadata:
|
|
62
|
+
allowed_push_host: https://rubygems.org
|
|
63
|
+
homepage_uri: https://github.com/xoengineering/osf-dl
|
|
64
|
+
source_code_uri: https://github.com/xoengineering/osf-dl
|
|
65
|
+
bug_tracker_uri: https://github.com/xoengineering/osf-dl/issues
|
|
66
|
+
changelog_uri: https://github.com/xoengineering/osf-dl/blob/main/CHANGELOG.md
|
|
67
|
+
rubygems_mfa_required: 'true'
|
|
68
|
+
rdoc_options: []
|
|
69
|
+
require_paths:
|
|
70
|
+
- lib
|
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: 4.0.10
|
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
|
+
requirements:
|
|
78
|
+
- - ">="
|
|
79
|
+
- !ruby/object:Gem::Version
|
|
80
|
+
version: '0'
|
|
81
|
+
requirements: []
|
|
82
|
+
rubygems_version: 4.0.20
|
|
83
|
+
specification_version: 4
|
|
84
|
+
summary: Download preprints from OSF Preprints (PsyArXiv, SocArXiv, and more) for
|
|
85
|
+
offline archives.
|
|
86
|
+
test_files: []
|