11"""Provides the user intefaces for browse."""
22import re
3+ import geoip2 .database
4+
35from datetime import datetime
46from typing import Callable , Dict , Mapping , Union , Tuple , Any
57from flask import Blueprint , render_template , request , Response , session , \
1820from browse .controllers .year import year_page
1921
2022logger = logging .getLogger (__name__ )
23+ geoip_reader = None
2124
2225blueprint = Blueprint ('browse' , __name__ , url_prefix = '/' )
2326
2427
28+ @blueprint .before_app_first_request
29+ def load_global_data () -> None :
30+ """Load global data."""
31+ global geoip_reader
32+ try :
33+ geoip_reader = geoip2 .database .Reader ('data/GeoLite2-City.mmdb' )
34+ except Exception as ex :
35+ logger .debug (f'problem loading geoip database: { ex } ' )
36+
37+
2538@blueprint .context_processor
2639def inject_now () -> Dict :
2740 """Inject current datetime into request context."""
@@ -31,6 +44,20 @@ def inject_now() -> Dict:
3144@blueprint .before_request
3245def before_request () -> None :
3346 """Get instituional affiliation from session."""
47+ global geoip_reader
48+ if geoip_reader and 'contintent' not in session :
49+ session ['continent' ] = None
50+ try :
51+ response = geoip_reader .city (request .remote_addr )
52+ logger .debug (f'continent { response .continent .code } ' )
53+ session ['continent' ] = {
54+ 'code' : response .continent .code ,
55+ 'name' : response .continent .names ['en' ]
56+ }
57+
58+ except Exception as ex :
59+ logger .debug (f'problem getting match on IP: { ex } ' )
60+
3461 if 'institution' not in session :
3562 logger .debug ('Adding institution to session' )
3663 session ['institution' ] = get_institution (request .remote_addr )
@@ -91,11 +118,11 @@ def abstract(arxiv_id: str) -> Any:
91118 mimetype = 'text/plain' )
92119 return render_template ('abs/abs.html' , ** response ), code , headers
93120 elif code == status .HTTP_301_MOVED_PERMANENTLY :
94- return redirect (headers ['Location' ], code = code )
121+ return redirect (headers ['Location' ], code = code )
95122 elif code == status .HTTP_304_NOT_MODIFIED :
96- return '' , code , headers
123+ return '' , code , headers
97124
98- raise InternalServerError ('Unexpected error' )
125+ raise InternalServerError ('Unexpected error' )
99126
100127@blueprint .route ('category_taxonomy' , methods = ['GET' ])
101128def category_taxonomy () -> Any :
@@ -183,7 +210,6 @@ def clickthrough() -> Response:
183210def list_articles (context : str , subcontext : str ) -> Response :
184211 """
185212 List articles by context, month etc.
186-
187213 Context might be a context or an archive; Subcontext should be
188214 'recent', 'new' or a string of format YYMM.
189215 """
@@ -340,7 +366,6 @@ def archive(archive: str): # type: ignore
340366def archive_with_extra (archive : str , junk : str ): # type: ignore
341367 """
342368 Archive page with extra, 301 redirect to just the archive.
343-
344369 This handles some odd URLs that have ended up in search engines.
345370 See also ARXIVOPS-2119.
346371 """
0 commit comments