Skip to content

Commit

Permalink
Media file paths can now be Path objects
Browse files Browse the repository at this point in the history
  • Loading branch information
wragge committed Jan 27, 2022
1 parent 17dd2b1 commit 63aa11f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 45 deletions.
52 changes: 31 additions & 21 deletions api.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,8 @@
" * a list of paths to the image/media files (filename is used as title)\n",
" * a list of dicts, each containing `title`, and `path` values\n",
" \n",
" The path values can either be strings or pathlib Paths.\n",
" \n",
" Returns: \n",
" * the modified payload dict\n",
" '''\n",
Expand All @@ -435,12 +437,12 @@
" for index, media_file in enumerate(media_files):\n",
" if isinstance(media_file, dict):\n",
" title = media_file['title']\n",
" path = media_file['path']\n",
" path = Path(media_file['path'])\n",
" else:\n",
" title = media_file[:-4]\n",
" path = media_file\n",
" path = Path(media_file)\n",
" title = path.name\n",
" payload['o:media'].append({'o:ingester': 'upload', 'file_index': str(index), 'o:item': {}, 'dcterms:title': [{'property_id': 1, '@value': title, 'type': 'literal'}]})\n",
" files[f'file[{index}]'] = Path(path).read_bytes()\n",
" files[f'file[{index}]'] = path.read_bytes()\n",
" files['data'] = (None, json.dumps(payload), 'application/json')\n",
" return files"
]
Expand Down Expand Up @@ -1846,9 +1848,9 @@
"data": {
"text/plain": [
"{'@context': 'http://timsherratt.org/collections/api-context',\n",
" '@id': 'http://timsherratt.org/collections/api/items/730',\n",
" '@id': 'http://timsherratt.org/collections/api/items/763',\n",
" '@type': 'o:Item',\n",
" 'o:id': 730,\n",
" 'o:id': 763,\n",
" 'o:is_public': True,\n",
" 'o:owner': {'@id': 'http://timsherratt.org/collections/api/users/1',\n",
" 'o:id': 1},\n",
Expand All @@ -1858,9 +1860,9 @@
" 'o:thumbnail': None,\n",
" 'o:title': 'The Bendigo Independent (Vic. : 1891 - 1918)',\n",
" 'thumbnail_display_urls': {'large': None, 'medium': None, 'square': None},\n",
" 'o:created': {'@value': '2022-01-26T04:46:55+00:00',\n",
" 'o:created': {'@value': '2022-01-27T05:35:36+00:00',\n",
" '@type': 'http://www.w3.org/2001/XMLSchema#dateTime'},\n",
" 'o:modified': {'@value': '2022-01-26T04:46:55+00:00',\n",
" 'o:modified': {'@value': '2022-01-27T05:35:36+00:00',\n",
" '@type': 'http://www.w3.org/2001/XMLSchema#dateTime'},\n",
" 'o:media': [],\n",
" 'o:item_set': [],\n",
Expand Down Expand Up @@ -2071,8 +2073,8 @@
" '@value': \"MR WRAGGE'S PREDICTION. RENEWAL OF CYCLONE FORETOLD.\"}],\n",
" 'schema:isPartOf': [{'property_id': 736,\n",
" 'type': 'resource:item',\n",
" '@id': 'http://timsherratt.org/collections/api/items/730',\n",
" 'value_resource_id': 730,\n",
" '@id': 'http://timsherratt.org/collections/api/items/763',\n",
" 'value_resource_id': 763,\n",
" 'value_resource_name': 'items'}],\n",
" 'schema:datePublished': [{'property_id': 928,\n",
" 'type': 'numeric:timestamp',\n",
Expand Down Expand Up @@ -2105,6 +2107,7 @@
"outputs": [],
"source": [
"# Create a list of paths pointing to media files\n",
"# The paths can be strings or pathlib Paths\n",
"media_files = ['media/nla.news-article226799674-24144902.jpg']\n",
"\n",
"# Include the media files when we upload the payload\n",
Expand All @@ -2127,9 +2130,9 @@
"data": {
"text/plain": [
"{'@context': 'http://timsherratt.org/collections/api-context',\n",
" '@id': 'http://timsherratt.org/collections/api/items/731',\n",
" '@id': 'http://timsherratt.org/collections/api/items/766',\n",
" '@type': 'o:Item',\n",
" 'o:id': 731,\n",
" 'o:id': 766,\n",
" 'o:is_public': True,\n",
" 'o:owner': {'@id': 'http://timsherratt.org/collections/api/users/1',\n",
" 'o:id': 1},\n",
Expand All @@ -2138,15 +2141,15 @@
" 'o:id': 4},\n",
" 'o:thumbnail': None,\n",
" 'o:title': \"MR WRAGGE'S PREDICTION. RENEWAL OF CYCLONE FORETOLD.\",\n",
" 'thumbnail_display_urls': {'large': 'http://timsherratt.org/collections/files/large/aa9ef3fe881ee92c46bc8f1500d7f9fa9f3d6bb4.jpg',\n",
" 'medium': 'http://timsherratt.org/collections/files/medium/aa9ef3fe881ee92c46bc8f1500d7f9fa9f3d6bb4.jpg',\n",
" 'square': 'http://timsherratt.org/collections/files/square/aa9ef3fe881ee92c46bc8f1500d7f9fa9f3d6bb4.jpg'},\n",
" 'o:created': {'@value': '2022-01-26T04:47:02+00:00',\n",
" 'thumbnail_display_urls': {'large': 'http://timsherratt.org/collections/files/large/370fd463d1743fc81fa00d71448f294e7aec7643.jpg',\n",
" 'medium': 'http://timsherratt.org/collections/files/medium/370fd463d1743fc81fa00d71448f294e7aec7643.jpg',\n",
" 'square': 'http://timsherratt.org/collections/files/square/370fd463d1743fc81fa00d71448f294e7aec7643.jpg'},\n",
" 'o:created': {'@value': '2022-01-27T05:37:15+00:00',\n",
" '@type': 'http://www.w3.org/2001/XMLSchema#dateTime'},\n",
" 'o:modified': {'@value': '2022-01-26T04:47:02+00:00',\n",
" 'o:modified': {'@value': '2022-01-27T05:37:15+00:00',\n",
" '@type': 'http://www.w3.org/2001/XMLSchema#dateTime'},\n",
" 'o:media': [{'@id': 'http://timsherratt.org/collections/api/media/732',\n",
" 'o:id': 732}],\n",
" 'o:media': [{'@id': 'http://timsherratt.org/collections/api/media/767',\n",
" 'o:id': 767}],\n",
" 'o:item_set': [],\n",
" 'o:site': [],\n",
" 'schema:name': [{'type': 'literal',\n",
Expand All @@ -2164,8 +2167,8 @@
" 'property_id': 736,\n",
" 'property_label': 'isPartOf',\n",
" 'is_public': True,\n",
" '@id': 'http://timsherratt.org/collections/api/items/730',\n",
" 'value_resource_id': 730,\n",
" '@id': 'http://timsherratt.org/collections/api/items/763',\n",
" 'value_resource_id': 763,\n",
" 'value_resource_name': 'items',\n",
" 'url': None,\n",
" 'display_title': 'The Bendigo Independent (Vic. : 1891 - 1918)'}],\n",
Expand Down Expand Up @@ -2193,6 +2196,13 @@
"\n",
"* update and delete methods"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
38 changes: 20 additions & 18 deletions docs/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,7 @@ <h4 id="OmekaAPIClient.add_media_to_payload" class="doc_header"><code>OmekaAPICl
<li>a list of paths to the image/media files (filename is used as title)</li>
<li>a list of dicts, each containing <code>title</code>, and <code>path</code> values</li>
</ul>
<p>The path values can either be strings or pathlib Paths.</p>
<p>Returns:</p>
<ul>
<li>the modified payload dict</li>
Expand Down Expand Up @@ -1865,9 +1866,9 @@ <h2 id="Example:-adding-a-newspaper-article-from-Trove">Example: adding a newspa

<div class="output_text output_subarea output_execute_result">
<pre>{&#39;@context&#39;: &#39;http://timsherratt.org/collections/api-context&#39;,
&#39;@id&#39;: &#39;http://timsherratt.org/collections/api/items/730&#39;,
&#39;@id&#39;: &#39;http://timsherratt.org/collections/api/items/763&#39;,
&#39;@type&#39;: &#39;o:Item&#39;,
&#39;o:id&#39;: 730,
&#39;o:id&#39;: 763,
&#39;o:is_public&#39;: True,
&#39;o:owner&#39;: {&#39;@id&#39;: &#39;http://timsherratt.org/collections/api/users/1&#39;,
&#39;o:id&#39;: 1},
Expand All @@ -1877,9 +1878,9 @@ <h2 id="Example:-adding-a-newspaper-article-from-Trove">Example: adding a newspa
&#39;o:thumbnail&#39;: None,
&#39;o:title&#39;: &#39;The Bendigo Independent (Vic. : 1891 - 1918)&#39;,
&#39;thumbnail_display_urls&#39;: {&#39;large&#39;: None, &#39;medium&#39;: None, &#39;square&#39;: None},
&#39;o:created&#39;: {&#39;@value&#39;: &#39;2022-01-26T04:46:55+00:00&#39;,
&#39;o:created&#39;: {&#39;@value&#39;: &#39;2022-01-27T05:35:36+00:00&#39;,
&#39;@type&#39;: &#39;http://www.w3.org/2001/XMLSchema#dateTime&#39;},
&#39;o:modified&#39;: {&#39;@value&#39;: &#39;2022-01-26T04:46:55+00:00&#39;,
&#39;o:modified&#39;: {&#39;@value&#39;: &#39;2022-01-27T05:35:36+00:00&#39;,
&#39;@type&#39;: &#39;http://www.w3.org/2001/XMLSchema#dateTime&#39;},
&#39;o:media&#39;: [],
&#39;o:item_set&#39;: [],
Expand Down Expand Up @@ -2145,8 +2146,8 @@ <h2 id="Example:-adding-a-newspaper-article-from-Trove">Example: adding a newspa
&#39;@value&#39;: &#34;MR WRAGGE&#39;S PREDICTION. RENEWAL OF CYCLONE FORETOLD.&#34;}],
&#39;schema:isPartOf&#39;: [{&#39;property_id&#39;: 736,
&#39;type&#39;: &#39;resource:item&#39;,
&#39;@id&#39;: &#39;http://timsherratt.org/collections/api/items/730&#39;,
&#39;value_resource_id&#39;: 730,
&#39;@id&#39;: &#39;http://timsherratt.org/collections/api/items/763&#39;,
&#39;value_resource_id&#39;: 763,
&#39;value_resource_name&#39;: &#39;items&#39;}],
&#39;schema:datePublished&#39;: [{&#39;property_id&#39;: 928,
&#39;type&#39;: &#39;numeric:timestamp&#39;,
Expand Down Expand Up @@ -2178,7 +2179,8 @@ <h2 id="Example:-adding-a-newspaper-article-from-Trove">Example: adding a newspa

<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">media_files</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;media/nla.news-article226799674-24144902.jpg&#39;</span><span class="p">]</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># The paths can be strings or pathlib Paths</span>
<span class="n">media_files</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;media/nla.news-article226799674-24144902.jpg&#39;</span><span class="p">]</span>

<span class="c1"># Include the media files when we upload the payload</span>
<span class="n">article_item</span> <span class="o">=</span> <span class="n">omeka_auth</span><span class="o">.</span><span class="n">add_item</span><span class="p">(</span><span class="n">article_payload</span><span class="p">,</span> <span class="n">media_files</span><span class="o">=</span><span class="n">media_files</span><span class="p">,</span> <span class="n">template_id</span><span class="o">=</span><span class="n">article_template_id</span><span class="p">)</span>
Expand Down Expand Up @@ -2221,9 +2223,9 @@ <h2 id="Example:-adding-a-newspaper-article-from-Trove">Example: adding a newspa

<div class="output_text output_subarea output_execute_result">
<pre>{&#39;@context&#39;: &#39;http://timsherratt.org/collections/api-context&#39;,
&#39;@id&#39;: &#39;http://timsherratt.org/collections/api/items/731&#39;,
&#39;@id&#39;: &#39;http://timsherratt.org/collections/api/items/766&#39;,
&#39;@type&#39;: &#39;o:Item&#39;,
&#39;o:id&#39;: 731,
&#39;o:id&#39;: 766,
&#39;o:is_public&#39;: True,
&#39;o:owner&#39;: {&#39;@id&#39;: &#39;http://timsherratt.org/collections/api/users/1&#39;,
&#39;o:id&#39;: 1},
Expand All @@ -2232,15 +2234,15 @@ <h2 id="Example:-adding-a-newspaper-article-from-Trove">Example: adding a newspa
&#39;o:id&#39;: 4},
&#39;o:thumbnail&#39;: None,
&#39;o:title&#39;: &#34;MR WRAGGE&#39;S PREDICTION. RENEWAL OF CYCLONE FORETOLD.&#34;,
&#39;thumbnail_display_urls&#39;: {&#39;large&#39;: &#39;http://timsherratt.org/collections/files/large/aa9ef3fe881ee92c46bc8f1500d7f9fa9f3d6bb4.jpg&#39;,
&#39;medium&#39;: &#39;http://timsherratt.org/collections/files/medium/aa9ef3fe881ee92c46bc8f1500d7f9fa9f3d6bb4.jpg&#39;,
&#39;square&#39;: &#39;http://timsherratt.org/collections/files/square/aa9ef3fe881ee92c46bc8f1500d7f9fa9f3d6bb4.jpg&#39;},
&#39;o:created&#39;: {&#39;@value&#39;: &#39;2022-01-26T04:47:02+00:00&#39;,
&#39;thumbnail_display_urls&#39;: {&#39;large&#39;: &#39;http://timsherratt.org/collections/files/large/370fd463d1743fc81fa00d71448f294e7aec7643.jpg&#39;,
&#39;medium&#39;: &#39;http://timsherratt.org/collections/files/medium/370fd463d1743fc81fa00d71448f294e7aec7643.jpg&#39;,
&#39;square&#39;: &#39;http://timsherratt.org/collections/files/square/370fd463d1743fc81fa00d71448f294e7aec7643.jpg&#39;},
&#39;o:created&#39;: {&#39;@value&#39;: &#39;2022-01-27T05:37:15+00:00&#39;,
&#39;@type&#39;: &#39;http://www.w3.org/2001/XMLSchema#dateTime&#39;},
&#39;o:modified&#39;: {&#39;@value&#39;: &#39;2022-01-26T04:47:02+00:00&#39;,
&#39;o:modified&#39;: {&#39;@value&#39;: &#39;2022-01-27T05:37:15+00:00&#39;,
&#39;@type&#39;: &#39;http://www.w3.org/2001/XMLSchema#dateTime&#39;},
&#39;o:media&#39;: [{&#39;@id&#39;: &#39;http://timsherratt.org/collections/api/media/732&#39;,
&#39;o:id&#39;: 732}],
&#39;o:media&#39;: [{&#39;@id&#39;: &#39;http://timsherratt.org/collections/api/media/767&#39;,
&#39;o:id&#39;: 767}],
&#39;o:item_set&#39;: [],
&#39;o:site&#39;: [],
&#39;schema:name&#39;: [{&#39;type&#39;: &#39;literal&#39;,
Expand All @@ -2258,8 +2260,8 @@ <h2 id="Example:-adding-a-newspaper-article-from-Trove">Example: adding a newspa
&#39;property_id&#39;: 736,
&#39;property_label&#39;: &#39;isPartOf&#39;,
&#39;is_public&#39;: True,
&#39;@id&#39;: &#39;http://timsherratt.org/collections/api/items/730&#39;,
&#39;value_resource_id&#39;: 730,
&#39;@id&#39;: &#39;http://timsherratt.org/collections/api/items/763&#39;,
&#39;value_resource_id&#39;: 763,
&#39;value_resource_name&#39;: &#39;items&#39;,
&#39;url&#39;: None,
&#39;display_title&#39;: &#39;The Bendigo Independent (Vic. : 1891 - 1918)&#39;}],
Expand Down
2 changes: 1 addition & 1 deletion omeka_s_tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.3"
__version__ = "0.0.4"
10 changes: 6 additions & 4 deletions omeka_s_tools/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ def add_media_to_payload(self, payload, media_files):
* a list of paths to the image/media files (filename is used as title)
* a list of dicts, each containing `title`, and `path` values
The path values can either be strings or pathlib Paths.
Returns:
* the modified payload dict
'''
Expand All @@ -399,11 +401,11 @@ def add_media_to_payload(self, payload, media_files):
for index, media_file in enumerate(media_files):
if isinstance(media_file, dict):
title = media_file['title']
path = media_file['path']
path = Path(media_file['path'])
else:
title = media_file[:-4]
path = media_file
path = Path(media_file)
title = path.name
payload['o:media'].append({'o:ingester': 'upload', 'file_index': str(index), 'o:item': {}, 'dcterms:title': [{'property_id': 1, '@value': title, 'type': 'literal'}]})
files[f'file[{index}]'] = Path(path).read_bytes()
files[f'file[{index}]'] = path.read_bytes()
files['data'] = (None, json.dumps(payload), 'application/json')
return files
2 changes: 1 addition & 1 deletion settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ author = Tim Sherratt
author_email = [email protected]
copyright = Tim Sherratt
branch = master
version = 0.0.3
version = 0.0.4
min_python = 3.6
audience = Developers
language = English
Expand Down

0 comments on commit 63aa11f

Please sign in to comment.