-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
210 lines (181 loc) · 6.72 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
from pathlib import Path
import random
from urllib.parse import quote
from flask import Flask, render_template, request, redirect, send_file, url_for
from pycaster.lib.image import ImageComponent, generate_app_image
from pycaster.lib.meroku import get_apps, rate_app
from pycaster.lib.middleware import check_trusted_data
from pycaster.lib.utils import app_url, setup_logger
__current_file_path__ = Path(__file__).resolve()
__current_dir__ = __current_file_path__.parent
app = Flask(__name__, template_folder='pycaster/templates')
app.logger = setup_logger(__name__)
@app.before_request
def before_request():
if not check_trusted_data():
return "Request Unauthorized", 403
@app.route('/')
def index():
apps = get_apps()
app_img_url = url_for('frame_image', app_id=apps[0]['dappId'])
image_url = f"https://{ app_url }{ app_img_url }"
post_url = f"https://{ app_url }{ url_for('action', app_id=apps[0]['dappId']) }"
return render_template('index.html', image_url=image_url, post_url=post_url)
@app.route('/action/<app_id>', methods=['POST'])
def action(app_id: str):
data = request.get_json(silent=True)
untrusted_data = data['untrustedData']
buttonIndex = untrusted_data['buttonIndex']
if buttonIndex == 1:
apps = get_apps()
idx = random.randint(0, len(apps) - 1)
app_img_url = url_for('frame_image', app_id=apps[idx]['dappId'])
image_url = f"https://{ app_url }{ app_img_url }"
post_url = f"https://{ app_url }{ url_for('action', app_id=apps[idx]['dappId']) }"
return render_template('index.html', image_url=image_url, post_url=post_url)
elif buttonIndex == 2:
user_id = f"fc_user:{ untrusted_data['fid'] }"
redirect_url = f"https://api.meroku.store/api/v1/o/view/{ app_id }?userId={ user_id }"
return redirect(redirect_url, 302)
elif buttonIndex == 3:
image_url = f"https://{ app_url }{ url_for('image', view_type='pre_rate', app_id=app_id) }"
post_url = f"https://{ app_url }{ url_for('rate', app_id=app_id) }"
return render_template('rate.html', image_url=image_url, post_url=post_url)
elif buttonIndex == 4:
redirect_url = f"https://{ app_url }{ url_for('redirect_url', app_id=app_id) }"
return redirect(redirect_url, 302)
@app.route('/rate/<app_id>', methods=['POST'])
def rate(app_id: str):
data = request.get_json(silent=True)
untrusted_data = data['untrustedData']
buttonIndex = untrusted_data['buttonIndex']
if buttonIndex == 1:
rating = 1
elif buttonIndex == 2:
rating = 3
elif buttonIndex == 3:
rating = 5
else:
rating = 3
rate_app(app_id, rating, untrusted_data['fid'])
img_url = f"https://{ app_url }{ url_for('image', view_type='post_rate', app_id=app_id) }"
next_url = f"https://{ app_url }{ url_for('thanks', app_id=app_id) }"
return render_template('thanks.html', image_url=img_url, post_url=next_url)
@app.route('/thanks/<app_id>', methods=['POST'])
def thanks(app_id: str):
data = request.get_json(silent=True)
untrusted_data = data['untrustedData']
buttonIndex = untrusted_data['buttonIndex']
app.logger.info(f"Button Index: { buttonIndex }")
try:
if buttonIndex == 1:
apps = get_apps()
idx = random.randint(0, len(apps) - 1)
app_img_url = url_for('frame_image', app_id=apps[idx]['dappId'])
image_url = f"https://{ app_url }{ app_img_url }"
post_url = f"https://{ app_url }{ url_for('action', app_id=apps[idx]['dappId']) }"
return render_template('index.html', image_url=image_url, post_url=post_url)
else:
return redirect("https://dappstore.app", 302)
except Exception as e:
app.logger.error(e)
return redirect("https://dappstore.app", 302)
@app.route('/frame/image/<app_id>')
def frame_image(app_id):
apps = get_apps()
apps = filter(lambda x: x['dappId'] == app_id, apps)
_app = next(apps, None)
app.logger.info(_app['images'])
image_stack = []
app_logo = ImageComponent(
ImageComponent.EXTERNAL_IMAGE,
position=(100, 100),
external_img_url=_app['images']['logo'],
display_type=ImageComponent.DISPLAY_TYPE_CIRCLE,
circle_radius=60
)
image_stack.append(app_logo)
app_images = _app['images']
screenshot_urls = app_images.get('screenshots', [])
if len(screenshot_urls) == 0:
screenshot_urls = app_images.get('mobileScreenshots', [])
# if len(screenshot_urls) > 0:
# screenshot_url = screenshot_urls[0]
# screenshot = ImageComponent(
# ImageComponent.EXTERNAL_IMAGE,
# position=(600, 0),
# external_img_url=screenshot_url,
# display_type=ImageComponent.DISPLAY_TYPE_RECTANGLE,
# rect_size=(300, 800)
# )
# image_stack.append(screenshot)
app_name = ImageComponent(
ImageComponent.TEXT,
position=(120, 80),
text=_app['name'],
font_size=42,
font_color=(140, 82, 255)
)
image_stack.append(app_name)
_text = f"{_app['description']}"
description = ImageComponent(
ImageComponent.TEXT,
position=(0, 150),
text=_text,
font_size=30,
font_color=(0, 0, 0)
)
image_stack.append(description)
img = generate_app_image(image_stack)
img.seek(0)
return send_file(img, mimetype='image/png')
@app.route('/image/<view_type>/<app_id>')
def image(view_type: str, app_id: str):
if view_type not in ['pre_rate', 'post_rate']:
return "Invalid view type", 400
apps = get_apps()
apps = filter(lambda x: x['dappId'] == app_id, apps)
_app = next(apps, None)
app.logger.info(_app['images'])
image_stack = []
if view_type == 'pre_rate':
app_logo = ImageComponent(
ImageComponent.EXTERNAL_IMAGE,
position=(140, 120),
external_img_url=_app['images']['logo'],
display_type=ImageComponent.DISPLAY_TYPE_CIRCLE,
circle_radius=60
)
image_stack.append(app_logo)
app_name = ImageComponent(
ImageComponent.TEXT,
position=(80, 80),
text=f"Rate {_app['name']}",
font_size=54,
font_color=(0, 0, 0)
)
image_stack.append(app_name)
base_image_path = __current_dir__ / "Pre_Rating.png"
else:
app_name = ImageComponent(
ImageComponent.TEXT,
position=(100, 80),
text=f"Thanks for rating {_app['name']}",
font_size=50,
font_color=(0, 0, 0)
)
image_stack.append(app_name)
base_image_path = __current_dir__ / "Ratings_Thanks.png"
img = generate_app_image(image_stack, base_image_path)
img.seek(0)
return send_file(img, mimetype='image/png')
@app.route('/redirect/<app_id>')
def redirect_url(app_id: str):
apps = get_apps()
apps = filter(lambda x: x['dappId'] == app_id, apps)
_app = next(apps, None)
link_url = f"https://explorer.meroku.org/dapp?id={ app_id }"
cast_text = f"Check out {_app['name']} on Meroku!"
cast_text = quote(cast_text)
cast_intent_url = f"https://warpcast.com/~/compose?text={cast_text}&embeds[]={link_url}"
return redirect(cast_intent_url, 302)