Skip to content

Commit abd6232

Browse files
Merge pull request #57 from OceanNetworksCanada/issue-56-archivefile-links
Issue 56 archivefile links
2 parents 341a075 + 4cfb6e7 commit abd6232

File tree

7 files changed

+125
-11
lines changed

7 files changed

+125
-11
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ onc.downloadArchivefile("AXISQ6044PTZACCC8E334C53_20161201T000001.000Z.jpg", ove
164164
You can use the method `downloadArchivefile()` as above to download individual files or the method `downloadDirectArchivefile()`
165165
to download all the files that match your filters.
166166

167+
Alternatively, if you prefer using a download manager like [aria2](https://aria2.github.io/) or [Free Download Manager](https://www.freedownloadmanager.org/), `getArchivefileUrls` and `getArchivefileUrl` can return the download URLs of the archivefile
168+
without downloading the files.
169+
167170
Check more on the _[archive file download methods guide](https://oceannetworkscanada.github.io/Oceans3.0-API/API_Guide.html#archive-file-download-methods)_
168171
and _[code examples](https://oceannetworkscanada.github.io/api-python-client/Code_Examples/Download_Archived_Files.html)_.
169172

doc/source/Code_Examples/Download_Archived_Files.md

+55-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ onc.getArchivefile(params)
110110

111111
## Download archived files that match the parameters
112112

113-
Download all "mat" files from a hydrophone at Straight of Georgia East (_locationCode_:"**SEVIP**") using the parameter above.
113+
Download all "mat" files from a hydrophone at Straight of Georgia East (_locationCode_:"**SEVIP**").
114114

115115
```python
116116
params = {
@@ -127,3 +127,57 @@ onc.downloadDirectArchivefile(params)
127127
# onc.getDirectFiles(params)
128128

129129
```
130+
131+
## Download archived files using a download manager
132+
133+
Return the download URLs from a hydrophone at Straight of Georgia East (_locationCode_:"**SEVIP**").
134+
135+
```python
136+
params = {
137+
"deviceCategoryCode": "HYDROPHONE",
138+
"locationCode": "SEVIP",
139+
"extension": "mat",
140+
"dateFrom": "2018-10-05T00:00:00.000Z",
141+
"dateTo": "2018-10-05T00:10:00.000Z",
142+
}
143+
144+
# print is necessary to render the newline character
145+
print(onc.getArchivefileUrls(params, joinedWithNewline=True))
146+
```
147+
148+
After running the code, a list of URLs will be printed.
149+
150+
```
151+
https://data.oceannetworks.ca/api/archivefile/download?filename=ICLISTENHF1560_20181004T235903.000Z-spect.mat&token=Your_TOKEN
152+
https://data.oceannetworks.ca/api/archivefile/download?filename=ICLISTENHF1560_20181005T000403.000Z-spect.mat&token=Your_TOKEN
153+
https://data.oceannetworks.ca/api/archivefile/download?filename=ICLISTENHF1560_20181005T000903.000Z-spect.mat&token=Your_TOKEN
154+
```
155+
156+
At this point, you can open your favorite download manager, paste the URLs, and start the download.
157+
Most modern download managers support batch download, probably from a multi-line text input, the clipboard or a file.
158+
159+
Here is an example of using a popular open source download manager -- aria2.
160+
161+
### webui-aria2
162+
163+
[aria2](https://aria2.github.io/) is a lightweight multi-protocol & multi-source command-line download utility.
164+
[webui-aria2](https://github.com/ziahamza/webui-aria2) is a web interface to interact with aria2.
165+
Refer to the webui-aria2 [README](https://github.com/ziahamza/webui-aria2?tab=readme-ov-file#webui-aria2) file
166+
for more information on how to use the tool.
167+
168+
1. Install aria2 by downloading it from the [release](https://github.com/aria2/aria2/releases)
169+
(or use your package manager if you are on Linux), extract the zip file,
170+
and start the server by running
171+
172+
```shell
173+
./aria2c --enable-rpc --rpc-listen-all
174+
```
175+
176+
2. Go to <https://ziahamza.github.io/webui-aria2> (or you can download this repository and open index.html
177+
from docs folder), change "Enter the host" field to "localhost" in the setting, and save the settings.
178+
Sometimes you need to also refresh the page.
179+
![Aria2c host change.png](../_static/Code_Examples/webui-aria2c-host.png)
180+
181+
3. Click "Add" -> "By URIs" in the menu. Fill in the URLs and start the download.
182+
You can also customize the download in the "Download settings" like changing the download directory.
183+
![Aria2c add URLs.png](../_static/Code_Examples/webui-aria2c-add-uri.png)
Loading
Loading

src/onc/modules/_OncArchive.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ def getArchivefile(self, filters: dict, allPages: bool):
4141
allPages=allPages,
4242
)
4343

44+
def getArchivefileUrls(self, filters: dict, allPages: bool) -> list[str]:
45+
file_list: list[str] = self.getArchivefile(filters, allPages)["files"]
46+
return list(map(self.getArchivefileUrl, file_list))
47+
48+
def getArchivefileUrl(self, filename: str) -> str:
49+
"""
50+
Return an archivefile absolute download URL for a filename
51+
"""
52+
url = self._serviceUrl("archivefile")
53+
token = self._config("token")
54+
return f"{url}/download?filename={filename}&token={token}"
55+
4456
def downloadArchivefile(self, filename: str = "", overwrite: bool = False):
4557
url = self._serviceUrl("archivefiles")
4658

@@ -123,7 +135,7 @@ def downloadDirectArchivefile(
123135
else:
124136
print(f' Skipping "{filename}": File already exists.')
125137
downInfo = {
126-
"url": self._getDownloadUrl(filename),
138+
"url": self.getArchivefileUrl(filename),
127139
"status": "skipped",
128140
"size": 0,
129141
"downloadTime": 0,
@@ -139,15 +151,6 @@ def downloadDirectArchivefile(
139151
"stats": {"totalSize": size, "downloadTime": time, "fileCount": successes},
140152
}
141153

142-
def _getDownloadUrl(self, filename: str):
143-
"""
144-
Returns an archivefile absolute download URL for a filename
145-
"""
146-
url = self._serviceUrl("archivefiles")
147-
return "{:s}?method=getFile&filename={:s}&token={:s}".format(
148-
url, filename, self._config("token")
149-
)
150-
151154
def _getList(self, filters: dict, by: str = "location", allPages: bool = False):
152155
"""
153156
Wraps archivefiles getArchivefileByLocation and getArchivefileByDevice methods

src/onc/modules/_OncService.py

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def _serviceUrl(self, service: str):
7777
"properties",
7878
"dataProducts",
7979
"archivefiles",
80+
"archivefile",
8081
"scalardata",
8182
"rawdata",
8283
]:

src/onc/onc.py

+53
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,59 @@ def getArchivefile(self, filters: dict = None, allPages: bool = False):
12991299
""" # noqa: E501
13001300
return self.archive.getArchivefile(filters, allPages)
13011301

1302+
def getArchivefileUrls(
1303+
self,
1304+
filters: dict = None,
1305+
allPages: bool = False,
1306+
joinedWithNewline: bool = False,
1307+
) -> list[str] | str:
1308+
"""
1309+
Return a list of file URLs (or joined with a newline) available in Oceans 3.0 Archiving System by given query parameters.
1310+
1311+
A helper method for getting a list of archive files URLs without downloading them.
1312+
It can also return a single string that concatenates all the URLs in the list together with a newline,
1313+
which can be useful if you are using a download manager and it supports batch downloading multiple URLs
1314+
that expects all the URLs on a separate line from either a file or the clipboard.
1315+
1316+
Parameters
1317+
----------
1318+
filters : dict, optional
1319+
Query string parameters in the API request.
1320+
See ``getArchivefileByLocation`` and ``getArchivefileByDevice`` for more information.
1321+
allPages : bool, default False
1322+
Whether the response concatenates data on all pages if there are more than one page due to rowLimit.
1323+
joinedWithNewline: bool, default False
1324+
Whether it returns a list of URLs or a single string that concatenates the list with a newline.
1325+
1326+
Returns
1327+
-------
1328+
list[str] | str
1329+
A list of file URLs or a single joined string.
1330+
""" # noqa: E501
1331+
file_urls = self.archive.getArchivefileUrls(filters, allPages)
1332+
if joinedWithNewline:
1333+
return "\n".join(file_urls)
1334+
else:
1335+
return file_urls
1336+
1337+
def getArchivefileUrl(self, filename: str = "") -> str:
1338+
"""
1339+
Return a file URL from Oceans 3.0 Archiving System by specifying the file name.
1340+
1341+
A helper method for obtaining the archive file URL without actually downloading the file.
1342+
1343+
Parameters
1344+
----------
1345+
filename : str, default ""
1346+
A valid name of a file in DMAS Archiving System.
1347+
1348+
Returns
1349+
-------
1350+
str:
1351+
A download URL for the given archive filename.
1352+
""" # noqa: E501
1353+
return self.archive.getArchivefileUrl(filename)
1354+
13021355
def downloadArchivefile(self, filename: str = "", overwrite: bool = False):
13031356
"""
13041357
Download a file from Oceans 3.0 Archiving System by specifying the file name.

0 commit comments

Comments
 (0)