#!/usr/bin/env python3 # -*- encoding: utf-8 -*- from urllib.parse import urlencode from flask import Flask, request, render_template #from url_titles import urls from links_json import urls from url_util import encode_url_without_last_slash # set the project root directory as the static folder, you can set others. application = Flask(__name__, static_url_path='/static') # to find hyperlink title for the URL, and provide search link to Google/Baidu @application.route("/favicon.ico") def favicon(): return ""; @application.route("/robot.txt") def robot(): return ""; #Catch-All function which serves every URL including / is to chain two route filters. #One for the root path '/' and one including a path placeholder for the rest. @application.route('/', defaults={'path': ''}) @application.route('/') def root(path): #print("base url: %s" % request.base_url) #print("relative path:%s" % request.path) print("url: %s" % request.url) if request.url in urls: title = urls[request.url] return render_template('find_in_search_engine.html', title=title, url_root=request.url_root) elif request.url.endswith('/') and request.url[0:-1] in urls: title = urls[request.url[0:-1]] return render_template('find_in_search_engine.html', title=title, url_root=request.url_root) else: lower_url, upper_url = encode_url_without_last_slash(request.url) url = lower_url if lower_url in urls else upper_url if url in urls: return render_template('find_in_search_engine.html', title=urls[url], url_root=request.url_root) return render_template('404.html') if __name__ == "__main__": application.run()