service.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env python3
  2. # -*- encoding: utf-8 -*-
  3. from urllib.parse import urlencode
  4. from flask import Flask, request, render_template
  5. #from url_titles import urls
  6. from links_json import urls
  7. from url_util import encode_url_without_last_slash
  8. # set the project root directory as the static folder, you can set others.
  9. application = Flask(__name__, static_url_path='/static')
  10. # to find hyperlink title for the URL, and provide search link to Google/Baidu
  11. @application.route("/favicon.ico")
  12. def favicon():
  13. return "";
  14. @application.route("/robot.txt")
  15. def robot():
  16. return "";
  17. #Catch-All function which serves every URL including / is to chain two route filters.
  18. #One for the root path '/' and one including a path placeholder for the rest.
  19. @application.route('/', defaults={'path': ''})
  20. @application.route('/<path:path>')
  21. def root(path):
  22. #print("base url: %s" % request.base_url)
  23. #print("relative path:%s" % request.path)
  24. print("url: %s" % request.url)
  25. if request.url in urls:
  26. title = urls[request.url]
  27. return render_template('find_in_search_engine.html', title=title, url_root=request.url_root)
  28. elif request.url.endswith('/') and request.url[0:-1] in urls:
  29. title = urls[request.url[0:-1]]
  30. return render_template('find_in_search_engine.html', title=title, url_root=request.url_root)
  31. else:
  32. lower_url, upper_url = encode_url_without_last_slash(request.url)
  33. url = lower_url if lower_url in urls else upper_url
  34. if url in urls:
  35. return render_template('find_in_search_engine.html', title=urls[url], url_root=request.url_root)
  36. return render_template('404.html')
  37. if __name__ == "__main__":
  38. application.run()