11import math
2- from base64 import b32decode
3- from binascii import Error
4- from html import unescape
2+ import re
53from urllib .parse import urlencode
64
75from django .conf import settings
@@ -40,12 +38,15 @@ def encode_url(params):
4038 return API_ENDPOINT .format (urlencode (params ))
4139
4240
43- def recreate_unencoded (parts ):
44- return ", " .join (parts ).capitalize ()
45-
46-
47- def decode_meta (encoded ):
48- return unescape (b32decode (encoded .upper ()).decode ("utf-8" ))
41+ def strip_title_suffix (title ):
42+ """Remove the ' | Consumer Financial Protection Bureau' from the end
43+ of search result titles. Becuase one or more words may be enclosed by
44+ \ue000 and \ue001 characters if they match a search term, we have to be
45+ a little flexible with the regex.
46+ """
47+ suffix_regex = r" \| .*"
48+ cleaned_title = re .sub (suffix_regex , "" , title )
49+ return cleaned_title
4950
5051
5152class SearchView (TranslatedTemplateView ):
@@ -72,7 +73,6 @@ def get(self, request, **kwargs):
7273 response = requests .get (
7374 encode_url (
7475 {
75- "include_facets" : "true" ,
7676 "affiliate" : affiliate ,
7777 "access_key" : api_key ,
7878 "limit" : RESULTS_PER_PAGE ,
@@ -105,21 +105,8 @@ def get(self, request, **kwargs):
105105
106106 # Post proprocess results
107107 for res in results :
108- # Strip | CFPB suffix
109- encoded_list = res .get ("searchgov_custom2" )
110- if encoded_list :
111- # Hack due to search.gov caching old data
112- try :
113- res ["description" ] = decode_meta (
114- encoded_list [0 ]
115- )
116- # binascii Error points to likely unencoded data
117- except Error :
118- res ["description" ] = recreate_unencoded (
119- encoded_list
120- )
121- else :
122- res ["description" ] = res ["snippet" ]
108+ # Strip " | CFPB" suffix
109+ res ["title" ] = strip_title_suffix (res ["title" ])
123110
124111 else :
125112 count = 0
0 commit comments