12
12
# lets aiohttp
13
13
# i wrote one recently.....
14
14
15
+ # lol jk aiohttp is more finicky than flask despite being more performant
16
+ # throws a 400 error, seems like you need another server/wsgi in front of it to validate correctly formatted html
17
+ # but my requests are relatively benign and it's still failing
18
+
15
19
creds = json .load (open ("credentials.json" ))
16
20
USERNAME = creds .get ("USERNAME" )
17
21
APP_PASSWORD = creds .get ("APP_PASSWORD" )
18
22
19
23
'''
20
24
GET / HTTP/1.1
21
25
user-agent: Mozilla/5.0 (compatible; Discordbot/2.0; +https://discordapp.com)
22
- host: 5.42.201.69:8082
23
26
'''
24
27
25
28
discord_preview_for_video_tweet = '''
97
100
cc @mr_ligi @coderobe
98
101
"""
99
102
103
+ def generate_html (full_path ):
104
+ # path = /profile/klatz.co/post/3jua5rlgrq42p
105
+ # or
106
+ # path = /profile/DID/post/3jua5rlgrq42p
107
+
108
+ session = atprototools .Session (USERNAME ,APP_PASSWORD )
109
+ post_content = session .get_bloot_by_url (full_path ).json ()
110
+
111
+ post_content = post_content .get ("posts" )[0 ]
112
+
113
+ author = post_content .get ("author" )
114
+ img_url = ""
115
+ try :
116
+ img_url = post_content .get ("embed" ).get ("images" )[0 ].get ("fullsize" )
117
+ except :
118
+ pass
119
+ record = post_content .get ("record" )
120
+ text = record .get ("text" )
121
+
122
+ html = f"""
123
+ <html lang="en">
124
+ <head>
125
+
126
+ <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
127
+ <meta content="#7FFFD4" name="theme-color" />
128
+ <meta property="og:site_name" content="psky.app" />
129
+
130
+ <meta name="twitter:card" content="summary_large_image" />
131
+ <meta name="twitter:title" content="{ author .get ("displayName" )} (@{ author .get ("handle" )} ) " />
132
+ <meta name="twitter:image" content="{ img_url } " />
133
+ <meta name="twitter:creator" content="@{ author .get ("displayName" )} " />
134
+
135
+ <meta property="og:description" content="{ text } " />
136
+
137
+ <!-- TODO what on earth is this -->
138
+ <!-- <link rel="alternate" href="https://vxtwitter.com/oembed.json?desc=Ian%20Klatzco&user=Twitter&link=https%3A//twitter.com/ian5v&ttype=photo" type="application/json+oembed" title="Ian Klatzco"> -->
139
+ <meta http-equiv="refresh" content="0; url = { post_url } " />
140
+ </head>
141
+ <body>
142
+ Redirecting you to the tweet in a moment. <a href="{ post_url } ">Or click here.</a>
143
+ </body>
144
+ """
145
+ return html
146
+
100
147
101
148
@app .route ("/" , defaults = {'path' : '' })
102
149
@app .route ("/<path:path>" )
103
150
def index (path ):
104
- session = atprototools .Session (USERNAME ,APP_PASSWORD )
105
151
106
152
print (request .url )
107
153
@@ -120,49 +166,14 @@ def index(path):
120
166
# request comes in with a url
121
167
# parse the URL, fetch the corresponding content from bsky, return it formatted
122
168
123
- # what is the aiohttp for the url?
169
+ print ("PATH: " , end = '' )
170
+ print (path )
171
+ print ("request.path: " , end = '' )
124
172
print (request .path )
125
- bsky_url = "https://bsky.app" + str (request .path )
126
- post_content = session .get_skoot_by_url (bsky_url ).json ()
127
- post_content = post_content .get ("thread" ).get ("post" )
128
-
129
- author = post_content .get ("author" )
130
- img_url = ""
131
- try :
132
- img_url = post_content .get ("embed" ).get ("images" )[0 ].get ("fullsize" )
133
- except :
134
- pass
135
- record = post_content .get ("record" )
136
- text = record .get ("text" )
137
-
138
- html = f"""
139
- <html lang="en">
140
- <head>
141
-
142
- <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
143
- <meta content="#7FFFD4" name="theme-color" />
144
- <meta property="og:site_name" content="psky.app" />
145
-
146
- <meta name="twitter:card" content="summary_large_image" />
147
- <meta name="twitter:title" content="{ author .get ("displayName" )} (@{ author .get ("handle" )} ) " />
148
- <meta name="twitter:image" content="{ img_url } " />
149
- <meta name="twitter:creator" content="@{ author .get ("displayName" )} " />
150
-
151
- <meta property="og:description" content="{ text } " />
152
-
153
- <!-- TODO what on earth is this -->
154
- <!-- <link rel="alternate" href="https://vxtwitter.com/oembed.json?desc=Ian%20Klatzco&user=Twitter&link=https%3A//twitter.com/ian5v&ttype=photo" type="application/json+oembed" title="Ian Klatzco"> -->
155
- <meta http-equiv="refresh" content="0; url = { post_url } " />
156
- </head>
157
- <body>
158
- Redirecting you to the tweet in a moment. <a href="{ post_url } ">Or click here.</a>
159
- </body>
160
- """
161
-
162
- # import pdb; pdb.set_trace()
163
173
174
+ html = generate_html ("https://bsky.app" + path )
175
+
164
176
return html
165
- # return web.Response(text=discord_preview_for_that, content_type="text/html")
166
177
167
178
if __name__ == "__main__" :
168
179
app .run (port = 8081 )
0 commit comments