-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
106 lines (79 loc) · 2.71 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import stripe
import os
import random
import string
import boto3
from flask import Flask, request
from flask_cors import CORS
from indexgenerator import make_index
from flask_mail import Mail, Message
from worker import conn
from rq import Queue
# from config import PASSWORD, SECRET_KEY, S3_BUCKET, S3_KEY, S3_SECRET
SECRET_KEY = os.environ.get('SECRET_KEY')
PASSWORD = os.environ.get('PASSWORD')
S3_BUCKET = os.environ.get('S3_BUCKET')
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
s3 = boto3.client(
's3',
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
s3_resource = boto3.resource('s3')
my_bucket = s3_resource.Bucket(S3_BUCKET)
app = Flask(__name__)
CORS(app)
q = Queue(connection=conn)
app.config.update(
MAIL_SERVER='smtp.zoho.com',
MAIL_PORT=465,
MAIL_USE_SSL=True,
SECURITY_EMAIL_SENDER='[email protected]',
MAIL_USERNAME='[email protected]',
MAIL_PASSWORD=PASSWORD
)
mail = Mail(app)
def make_and_send(ms_name, words_name, email):
with app.app_context():
s3.download_file(S3_BUCKET, ms_name, ms_name)
s3.download_file(S3_BUCKET, words_name, words_name)
with open(words_name) as words:
make_index(ms_name, words)
msg = Message(recipients=[email],
sender="[email protected]",
body='here is your index',
subject='your index')
with app.open_resource("index.txt") as fp:
msg.attach("index.txt", "text/plain", fp.read())
mail.send(msg)
os.remove(ms_name)
os.remove(words_name)
os.remove("index.txt")
return app
@app.route('/<email>', methods=["POST"])
def send_index(email):
ms = request.files['ms']
words = request.files['words']
letters = string.ascii_lowercase
random_name = ''.join(random.choice(letters) for i in range(5))
ms.filename = f"{random_name}.pdf"
words.filename = f"{random_name}.txt"
my_bucket.Object(ms.filename).put(Body=ms)
my_bucket.Object(words.filename).put(Body=words)
q.enqueue(make_and_send, ms.filename, words.filename, email)
return {'result': "success"}
stripe.api_key = SECRET_KEY
@app.route('/pay', methods=['POST'])
def pay():
email = request.json.get('email')
amt = request.json.get('amount')
amt = (int(amt) * 100)
if not email or not amt:
return 'please submit valid email and amount', 400
intent = stripe.PaymentIntent.create(
amount=amt,
currency='usd',
payment_method_types=['card'],
receipt_email=email,
)
return {'client_secret': intent['client_secret']}