Untitled

From Morose Pelican, 3 Weeks ago, written in Python, viewed 65 times.
URL https://p.gaa.st/view/54016f26 Embed
Download Paste or View Raw
  1. #!/usr/bin/env python3
  2.  
  3. from flask import Flask, abort, redirect, request, jsonify
  4. from http import HTTPStatus
  5. from flask_httpauth import HTTPBasicAuth
  6. from bs4 import BeautifulSoup
  7.  
  8. app = Flask(__name__)
  9. auth = HTTPBasicAuth()
  10.  
  11. @auth.verify_password
  12. def verify_password(username, password):
  13.         return username == 'garmin-live' and password == 'xxx'
  14.  
  15. @app.route('/mailhook', methods=['POST'])
  16. @auth.login_required
  17. def cloudmailin_webhook():
  18.         data = request.get_json()
  19.  
  20.         import tempfile, os
  21.         t = tempfile.mkstemp()
  22.         with os.fdopen(t[0], "w") as f:
  23.                 f.write("%r" % data)
  24.        
  25.         spf = data["envelope"]["spf"]
  26. #       assert spf["result"] == "pass"
  27.         assert spf["domain"] in ("garmin.com", "gaast.net")
  28.  
  29.         soup = BeautifulSoup(data["html"])
  30.         for l in soup.find_all('a',href=True):
  31.                 url = l.get("href")
  32.                 if url.startswith("https://livetrack.garmin.com/"):
  33.                         date = data["headers"]["date"]
  34.                         open("log", "a").write(f"{url} {date}\n")
  35.                         break
  36.         else:
  37.                 abort(422)
  38.  
  39.         return jsonify(status='ok')
  40.  
  41.  
  42. @app.route('/')
  43. def root():
  44.         log = open("log", "r").readlines()
  45.        
  46.         return redirect(log[-1].split()[0], code=302)
  47.  
  48.  
  49. #import sys
  50. #if len(sys.argv) > 1:
  51. #       data = eval(open(sys.argv[1], "r").read())
  52.  
  53. if __name__ == "__main__":
  54.         app.run(host='0.0.0.0', port=1375, debug=False)
  55.  

Reply to "Untitled"

Here you can reply to the paste above