Repository with sources and generator of https://larlet.fr/david/ https://larlet.fr/david/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.py 2.1KB

4 vuotta sitten
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/python3
  2. import os
  3. import cgi
  4. import cgitb
  5. cgitb.enable()
  6. import subprocess
  7. import urllib.request
  8. form = cgi.FieldStorage()
  9. source = form.getvalue('source', None)
  10. target = form.getvalue('target', None)
  11. server = os.environ.get('SERVER_NAME', None)
  12. method = os.environ.get('REQUEST_METHOD', 'GET')
  13. def validate(source, target, server, method):
  14. status = 400
  15. if method != 'POST':
  16. message = 'Webmention MUST be performed through a POST request.'
  17. elif source is None or target is None:
  18. message = 'Please fill in the source and target fields.'
  19. elif '://' not in source or '://' not in target:
  20. message = 'Source and target fields should be URLs.'
  21. elif not target.split('://', 1)[1].startswith(server):
  22. message = 'Target should be a link to this domain: {0}'.format(server)
  23. elif not target in urllib.request.urlopen(source).read().decode('utf-8'):
  24. message = 'Source should have a link to the target: {0}'.format(target)
  25. else:
  26. message = ('Success! With source={source} and target={target}.'
  27. '<a href="{target}">Back to the target article</a>.'
  28. ).format(
  29. source=source,
  30. target=target,
  31. )
  32. status = 202
  33. return status, message, source, target
  34. def send_response(callback, status, message, source, target):
  35. print('Content-type: text/html')
  36. print('Status: {0}'.format(status))
  37. print()
  38. print("""<!doctype html><html lang=en>
  39. <head><meta charset=utf-8><title>Webmention</title></head>
  40. <body><p>{message}</p></body></html>""".format(message=message))
  41. if status == 202:
  42. callback(source, target)
  43. def on_valid_submission(source, target):
  44. commands = [
  45. 'cd /home/larlet/www',
  46. 'source /home/larlet/.virtualenvs/webmention/bin/activate',
  47. 'fab --hide=everything add_webmention:{source},{target}'.format(
  48. source=source,
  49. target=target,
  50. ),
  51. ]
  52. subprocess.call(' && '.join(commands), shell=True)
  53. send_response(on_valid_submission, *validate(source, target, server, method))