Skip to content

Commit d71989d

Browse files
authored
Merge pull request #127 from internetarchive/issue-21-fixes
IIIF search fixes
2 parents 9aca4f7 + 7f27156 commit d71989d

17 files changed

+132
-18
lines changed

iiify/app.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
app = Flask(__name__)
2020
# disabling sorting of the output json
2121
app.config['JSON_SORT_KEYS'] = False
22-
app.config['CACHE_TYPE'] = "FileSystemCache" if os.environ.get("FLASK_CACHE_DISABLE", None) != "true" else "NullCache"
22+
# To disable the cache you must call
23+
# app.config['CACHE_TYPE'] = "NullCache"
24+
# as this code is run before the test.setUp
25+
app.config['CACHE_TYPE'] = "FileSystemCache"
2326
app.config['CACHE_DIR'] = "cache"
2427
cors = CORS(app) if cors else None
2528
cache = Cache(app)
@@ -28,7 +31,6 @@
2831

2932
# cache.init_app(app)
3033

31-
3234
def cache_bust():
3335
return request.args.get("recache", "") in ["True", "true", "1"]
3436

iiify/resolver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ def create_manifest3(identifier, domain=None, page=None):
646646
# Add search service
647647
service = {
648648
"@context": "http://iiif.io/api/search/1/context.json",
649-
"@id": f"{domain.replace("https","http")}search/{identifier}",
649+
"@id": f"{domain}search/{identifier}",
650650
"@type": "SearchService1",
651651
"profile": "http://iiif.io/api/search/1/search"
652652
}

iiify/search.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ def iiif_search(identifier, query):
4141
largeResults = False
4242
soup = BeautifulSoup(match['text'], 'html.parser')
4343
match = [tag.text for tag in soup.find_all('ia_fts_match')]
44+
if len(match) != len(paragraph['boxes']):
45+
for i in range(len(paragraph['boxes']) - len(match)):
46+
match.append(query)
4447
else:
4548
text = query
4649

@@ -49,8 +52,19 @@ def iiif_search(identifier, query):
4952
for box in paragraph['boxes']:
5053
x = int(box['l'])
5154
y = int (box['t'])
52-
width = int(box['r']) - x
55+
right = 0
56+
# If r is missing then use the paragraph
57+
if 'r' in box:
58+
right = int(box['r'])
59+
else:
60+
right = paragraph['r']
61+
62+
width = right - x
5363
height = int(box['b']) - y
64+
page = int(paragraph['page']) - 1
65+
if "leaf0_missing" in ia_response and ia_response['leaf0_missing'] == False:
66+
page = int(paragraph['page'])
67+
5468
searchResponse['resources'].append({
5569
"@id": f"{URI_PRIFIX}/{identifier}/annotation/anno{count}",
5670
"@type": "oa:Annotation",
@@ -59,7 +73,7 @@ def iiif_search(identifier, query):
5973
"@type": "cnt:ContentAsText",
6074
"chars": text if largeResults else match[matchNo]
6175
},
62-
"on": f"{URI_PRIFIX}/{identifier}${int(paragraph['page']) - 1}/canvas#xywh={x},{y},{width},{height}",
76+
"on": f"{URI_PRIFIX}/{identifier}${page}/canvas#xywh={x},{y},{width},{height}",
6377
"within": {
6478
"@id": f"{URI_PRIFIX}/{identifier}/manifest.json",
6579
"type": "sc:Manifest"
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"ia": "mrr_401",
3+
"q": "top",
4+
"indexed": true,
5+
"leaf0_missing": false,
6+
"page_count": 136,
7+
"matches": [
8+
{
9+
"text": "SONYA: I have a lot going on right now. I just decided to retire all the jeans. Now I am working on a cut-and-sew line but it’s <IA_FTS_MATCH>TOP</IA_FTS_MATCH> secret so don’t tell anyone. I am currently making a jacket that is an interpretation of a Vito Acconci piece.",
10+
"par": [
11+
{
12+
"t": 1661,
13+
"b": 2351,
14+
"l": 3632,
15+
"r": 4694,
16+
"boxes": [
17+
{
18+
"t": 1927,
19+
"b": 1998,
20+
"l": 4586,
21+
"page": 78,
22+
"r": 4678
23+
},
24+
{
25+
"t": 2015,
26+
"b": 2086,
27+
"l": 3651,
28+
"r": 4678
29+
},
30+
{
31+
"t": 2104,
32+
"b": 2176,
33+
"l": 3651,
34+
"r": 4678
35+
},
36+
{
37+
"t": 2192,
38+
"b": 2263,
39+
"l": 3650,
40+
"r": 4679
41+
},
42+
{
43+
"t": 2284,
44+
"b": 2351,
45+
"l": 3650
46+
}
47+
],
48+
"page": 78,
49+
"page_width": 5105,
50+
"page_height": 6492
51+
}
52+
]
53+
}
54+
]
55+
}

tests/test_annotations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class TestAnnotations(unittest.TestCase):
88

99
def setUp(self) -> None:
10-
os.environ["FLASK_CACHE_DISABLE"] = "true"
10+
app.config['CACHE_TYPE'] = "NullCache"
1111
self.test_app = FlaskClient(app)
1212

1313
def test_v3_manifest_has_annotations(self):

tests/test_audio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class TestAudio(unittest.TestCase):
99

1010
def setUp(self) -> None:
11-
os.environ["FLASK_CACHE_DISABLE"] = "true"
11+
app.config['CACHE_TYPE'] = "NullCache"
1212
self.test_app = FlaskClient(app)
1313

1414
def test_audio_no_derivatives(self):

tests/test_basic.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
class TestBasic(unittest.TestCase):
99

1010
def setUp(self) -> None:
11-
os.environ["FLASK_CACHE_DISABLE"] = "true"
11+
app.config['CACHE_TYPE'] = "NullCache"
1212
self.test_app = FlaskClient(app)
1313

14-
1514
def test_documentation(self):
1615
resp = self.test_app.get("/iiif/documentation")
1716
self.assertEqual(resp.status_code, 200)

tests/test_cantaloupe_resolver.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
import unittest
44
from iiify.resolver import cantaloupe_resolver
5+
from iiify.app import app
56

67
class TestCantaloupeResolver(unittest.TestCase):
78

89
def setUp(self) -> None:
9-
os.environ["FLASK_CACHE_DISABLE"] = "true"
10+
app.config['CACHE_TYPE'] = "NullCache"
1011

1112
def test_single_image(self):
1213
cid = cantaloupe_resolver("img-8664_202009")

tests/test_collections.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
class TestCollections(unittest.TestCase):
77

88
def setUp(self) -> None:
9-
os.environ["FLASK_CACHE_DISABLE"] = "true"
9+
app.config['CACHE_TYPE'] = "NullCache"
1010
self.test_app = FlaskClient(app)
1111

1212
def test_v3_collection(self):

tests/test_helper.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
class TestHelper(unittest.TestCase):
99

1010
def setUp(self) -> None:
11+
app.config['CACHE_TYPE'] = "NullCache"
1112
self.test_app = FlaskClient(app)
1213

1314
def test_single_image(self):

0 commit comments

Comments
 (0)